Specify parseInt base

This commit is contained in:
wltsmrz
2013-09-10 11:33:05 -07:00
parent 7e1e8a9503
commit 24ef629da3

View File

@@ -8,31 +8,31 @@ var jsbn = require('./jsbn');
var BigInteger = jsbn.BigInteger; var BigInteger = jsbn.BigInteger;
var UInt160 = require('./uint160').UInt160, var UInt160 = require('./uint160').UInt160;
Seed = require('./seed').Seed, var Seed = require('./seed').Seed;
Currency = require('./currency').Currency; var Currency = require('./currency').Currency;
var consts = exports.consts = { var consts = exports.consts = {
'currency_xns' : 0, currency_xns: 0,
'currency_one' : 1, currency_one: 1,
'xns_precision' : 6, xns_precision: 6,
// BigInteger values prefixed with bi_. // BigInteger values prefixed with bi_.
'bi_5' : new BigInteger('5'), bi_5: new BigInteger('5'),
'bi_7' : new BigInteger('7'), bi_7: new BigInteger('7'),
'bi_10' : new BigInteger('10'), bi_10: new BigInteger('10'),
'bi_1e14' : new BigInteger(String(1e14)), bi_1e14: new BigInteger(String(1e14)),
'bi_1e16' : new BigInteger(String(1e16)), bi_1e16: new BigInteger(String(1e16)),
'bi_1e17' : new BigInteger(String(1e17)), bi_1e17: new BigInteger(String(1e17)),
'bi_1e32' : new BigInteger('100000000000000000000000000000000'), bi_1e32: new BigInteger('100000000000000000000000000000000'),
'bi_man_max_value' : new BigInteger('9999999999999999'), bi_man_max_value: new BigInteger('9999999999999999'),
'bi_man_min_value' : new BigInteger('1000000000000000'), bi_man_min_value: new BigInteger('1000000000000000'),
'bi_xns_max' : new BigInteger("9000000000000000000"), // Json wire limit. bi_xns_max: new BigInteger("9000000000000000000"), // Json wire limit.
'bi_xns_min' : new BigInteger("-9000000000000000000"), // Json wire limit. bi_xns_min: new BigInteger("-9000000000000000000"),// Json wire limit.
'bi_xns_unit' : new BigInteger('1000000'), bi_xns_unit: new BigInteger('1000000'),
'cMinOffset' : -96, cMinOffset: -96,
'cMaxOffset' : 80, cMaxOffset: 80,
}; };
@@ -41,7 +41,7 @@ var consts = exports.consts = {
// http://docs.oracle.com/javase/1.3/docs/api/java/math/BigInteger.html // http://docs.oracle.com/javase/1.3/docs/api/java/math/BigInteger.html
// //
var Amount = function () { function Amount() {
// Json format: // Json format:
// integer : XRP // integer : XRP
// { 'value' : ..., 'currency' : ..., 'issuer' : ...} // { 'value' : ..., 'currency' : ..., 'issuer' : ...}
@@ -50,7 +50,6 @@ var Amount = function () {
this._offset = 0; // Always 0 for XRP. this._offset = 0; // Always 0 for XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN. this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._is_negative = false; this._is_negative = false;
this._currency = new Currency(); this._currency = new Currency();
this._issuer = new UInt160(); this._issuer = new UInt160();
}; };
@@ -58,7 +57,7 @@ var Amount = function () {
// Given "100/USD/mtgox" return the a string with mtgox remapped. // Given "100/USD/mtgox" return the a string with mtgox remapped.
Amount.text_full_rewrite = function (j) { Amount.text_full_rewrite = function (j) {
return Amount.from_json(j).to_text_full(); return Amount.from_json(j).to_text_full();
} };
// Given "100/USD/mtgox" return the json. // Given "100/USD/mtgox" return the json.
Amount.json_rewrite = function (j) { Amount.json_rewrite = function (j) {
@@ -91,9 +90,7 @@ Amount.is_valid_full = function (j) {
Amount.NaN = function () { Amount.NaN = function () {
var result = new Amount(); var result = new Amount();
result._value = NaN; result._value = NaN;
return result; return result;
}; };
@@ -110,17 +107,14 @@ Amount.prototype.add = function (v) {
if (!this.is_comparable(v)) { if (!this.is_comparable(v)) {
result = Amount.NaN(); result = Amount.NaN();
} } else if (v.is_zero()) {
else if (v.is_zero()) {
result = this; result = this;
} } else if (this.is_zero()) {
else if (this.is_zero()) {
result = v.clone(); result = v.clone();
result._is_native = this._is_native; result._is_native = this._is_native;
result._currency = this._currency; result._currency = this._currency;
result._issuer = this._issuer; result._issuer = this._issuer;
} } else if (this._is_native) {
else if (this._is_native) {
result = new Amount(); result = new Amount();
var v1 = this._is_negative ? this._value.negate() : this._value; var v1 = this._is_negative ? this._value.negate() : this._value;
@@ -131,9 +125,7 @@ Amount.prototype.add = function (v) {
result._value = result._is_negative ? s.negate() : s; result._value = result._is_negative ? s.negate() : s;
result._currency = this._currency; result._currency = this._currency;
result._issuer = this._issuer; result._issuer = this._issuer;
} } else {
else
{
var v1 = this._is_negative ? this._value.negate() : this._value; var v1 = this._is_negative ? this._value.negate() : this._value;
var o1 = this._offset; var o1 = this._offset;
var v2 = v._is_negative ? v._value.negate() : v._value; var v2 = v._is_negative ? v._value.negate() : v._value;
@@ -169,19 +161,15 @@ Amount.prototype.add = function (v) {
}; };
Amount.prototype.canonicalize = function () { Amount.prototype.canonicalize = function () {
if (!(this._value instanceof BigInteger)) if (!(this._value instanceof BigInteger)) {
{
// NaN. // NaN.
// nothing // nothing
} } else if (this._is_native) {
else if (this._is_native) {
// Native. // Native.
if (this._value.equals(BigInteger.ZERO)) { if (this._value.equals(BigInteger.ZERO)) {
this._offset = 0; this._offset = 0;
this._is_negative = false; this._is_negative = false;
} } else {
else {
// Normalize _offset to 0. // Normalize _offset to 0.
while (this._offset < 0) { while (this._offset < 0) {
@@ -196,13 +184,10 @@ Amount.prototype.canonicalize = function () {
} }
// XXX Make sure not bigger than supported. Throw if so. // XXX Make sure not bigger than supported. Throw if so.
} } else if (this.is_zero()) {
else if (this.is_zero()) {
this._offset = -100; this._offset = -100;
this._is_negative = false; this._is_negative = false;
} } else {
else
{
// Normalize mantissa to valid range. // Normalize mantissa to valid range.
while (this._value.compareTo(consts.bi_man_min_value) < 0) { while (this._value.compareTo(consts.bi_man_min_value) < 0) {
@@ -228,33 +213,27 @@ Amount.prototype.compareTo = function (v) {
if (!this.is_comparable(v)) { if (!this.is_comparable(v)) {
result = Amount.NaN(); result = Amount.NaN();
} } else if (this._is_negative !== v._is_negative) {
else if (this._is_negative !== v._is_negative) {
// Different sign. // Different sign.
result = this._is_negative ? -1 : 1; result = this._is_negative ? -1 : 1;
} } else if (this._value.equals(BigInteger.ZERO)) {
else if (this._value.equals(BigInteger.ZERO)) {
// Same sign: positive. // Same sign: positive.
result = v._value.equals(BigInteger.ZERO) ? 0 : -1; result = v._value.equals(BigInteger.ZERO) ? 0 : -1;
} } else if (v._value.equals(BigInteger.ZERO)) {
else if (v._value.equals(BigInteger.ZERO)) {
// Same sign: positive. // Same sign: positive.
result = 1; result = 1;
} } else if (!this._is_native && this._offset > v._offset) {
else if (!this._is_native && this._offset > v._offset) {
result = this._is_negative ? -1 : 1; result = this._is_negative ? -1 : 1;
} } else if (!this._is_native && this._offset < v._offset) {
else if (!this._is_native && this._offset < v._offset) {
result = this._is_negative ? 1 : -1; result = this._is_negative ? 1 : -1;
} } else {
else {
result = this._value.compareTo(v._value); result = this._value.compareTo(v._value);
if (result > 0) {
if (result > 0)
result = this._is_negative ? -1 : 1; result = this._is_negative ? -1 : 1;
else if (result < 0) } else if (result < 0) {
result = this._is_negative ? 1 : -1; result = this._is_negative ? 1 : -1;
} }
}
return result; return result;
}; };
@@ -262,12 +241,9 @@ Amount.prototype.compareTo = function (v) {
// Make d a copy of this. Returns d. // Make d a copy of this. Returns d.
// Modification of objects internally refered to is not allowed. // Modification of objects internally refered to is not allowed.
Amount.prototype.copyTo = function (d, negate) { Amount.prototype.copyTo = function (d, negate) {
if ('object' === typeof this._value) if (typeof this._value === 'object') {
{
this._value.copyTo(d._value); this._value.copyTo(d._value);
} } else {
else
{
d._value = this._value; d._value = this._value;
} }
@@ -281,7 +257,9 @@ Amount.prototype.copyTo = function (d, negate) {
d._issuer = this._issuer; d._issuer = this._issuer;
// Prevent negative zero // Prevent negative zero
if (d.is_zero()) d._is_negative = false; if (d.is_zero()) {
d._is_negative = false;
}
return d; return d;
}; };
@@ -291,28 +269,19 @@ Amount.prototype.currency = function () {
}; };
Amount.prototype.equals = function (d, ignore_issuer) { Amount.prototype.equals = function (d, ignore_issuer) {
if ("string" === typeof d) { if (typeof d === 'string') {
return this.equals(Amount.from_json(d)); return this.equals(Amount.from_json(d));
} }
if (this === d) return true; var result = true;
if (d instanceof Amount) { result = !((!this.is_valid() || !d.is_valid())
if (!this.is_valid() || !d.is_valid()) return false; || (this._is_native !== d._is_native)
if (this._is_native !== d._is_native) return false; || (!this._value.equals(d._value) || this._offset !== d._offset)
|| (this._is_negative !== d._is_negative)
|| (!this._is_native && (!this._currency.equals(d._currency) || !ignore_issuer && !this._issuer.equals(d._issuer))))
if (!this._value.equals(d._value) || this._offset !== d._offset) { return result;
return false;
}
if (this._is_negative !== d._is_negative) return false;
if (!this._is_native) {
if (!this._currency.equals(d._currency)) return false;
if (!ignore_issuer && !this._issuer.equals(d._issuer)) return false;
}
return true;
} else return false;
}; };
// Result in terms of this' currency and issuer. // Result in terms of this' currency and issuer.
@@ -386,7 +355,7 @@ Amount.prototype.divide = function (d) {
* @return {Amount} The resulting ratio. Unit will be the same as numerator. * @return {Amount} The resulting ratio. Unit will be the same as numerator.
*/ */
Amount.prototype.ratio_human = function (denominator) { Amount.prototype.ratio_human = function (denominator) {
if ("number" === typeof denominator && parseInt(denominator) === denominator) { if (typeof denominator === 'number' && parseInt(denominator, 10) === denominator) {
// Special handling of integer arguments // Special handling of integer arguments
denominator = Amount.from_json("" + denominator + ".0"); denominator = Amount.from_json("" + denominator + ".0");
} else { } else {
@@ -439,7 +408,7 @@ Amount.prototype.ratio_human = function (denominator) {
* @return {Amount} The product. Unit will be the same as the first factor. * @return {Amount} The product. Unit will be the same as the first factor.
*/ */
Amount.prototype.product_human = function (factor) { Amount.prototype.product_human = function (factor) {
if ("number" === typeof factor && parseInt(factor) === factor) { if (typeof factor === 'number' && parseInt(factor, 10) === factor) {
// Special handling of integer arguments // Special handling of integer arguments
factor = Amount.from_json("" + factor + ".0"); factor = Amount.from_json("" + factor + ".0");
} else { } else {