diff --git a/js/amount.js b/js/amount.js index 6e4d99f8..faac4e3d 100644 --- a/js/amount.js +++ b/js/amount.js @@ -37,7 +37,7 @@ UInt160.prototype.copyTo = function(d) { return d; }; -// value === NaN on error. +// value = NaN on error. UInt160.prototype.parse_json = function (j) { // Canonicalize and validate @@ -126,7 +126,7 @@ Currency.prototype.copyTo = function(d) { return d; }; -// this.value === NaN on error. +// this.value = NaN on error. Currency.prototype.parse_json = function(j) { if ("" === j || "0" === j || "XNS" === j) { this.value = 0; @@ -194,12 +194,12 @@ Amount.prototype.copyTo = function(d) { // YYY Might also provide is_valid_json. Amount.prototype.is_valid = function() { - return NaN !== this.value; + return !isNaN(this.value); }; // Convert only value to JSON wire format. Amount.prototype.to_text = function(allow_nan) { - if (NaN === this.value) { + if (isNaN(this.value)) { // Never should happen. return allow_nan ? NaN : "0"; } @@ -240,7 +240,7 @@ Amount.prototype.to_text = function(allow_nan) { }; Amount.prototype.canonicalize = function() { - if (NaN === this.value || !this.currency) { + if (isNaN(this.value) || !this.currency) { // nothing } else if (this.value.equals(BigInteger.ZERO)) { @@ -285,7 +285,7 @@ Amount.prototype.to_json = function() { }; Amount.prototype.to_text_full = function() { - return this.value === NaN + return isNaN(this.value) ? NaN : this.is_native ? this.to_text() + "/XNS" @@ -335,44 +335,55 @@ Amount.prototype.parse_native = function(j) { // Parse a non-native value. Amount.prototype.parse_value = function(j) { + this.is_native = false; + if ('number' === typeof j) { + this.is_negative = j < 0; + if (this.is_negative) j = -j; this.value = new BigInteger(j); this.offset = 0; - this.is_native = false; - this.is_negative = j < 0; this.canonicalize(); } else if ('string' === typeof j) { - var e = j.match(/^(-?\d+)e(\d+)/); - var d = j.match(/^(-?\d+)\.(\d+)/); + var i = j.match(/^(-?)(\d+)$/); + var d = !i && j.match(/^(-?)(\d+)\.(\d*)$/); + var e = !e && j.match(/^(-?)(\d+)e(\d+)$/); if (e) { // e notation - this.value = new BigInteger(e[1]); - this.offset = parseInt(e[2]); + this.value = new BigInteger(e[2]); + this.offset = parseInt(e[3]); + this.is_negative = !!e[1]; + + this.canonicalize(); } else if (d) { // float notation - var integer = new BigInteger(d[1]); - var fraction = new BigInteger(d[2]); - this.value = integer.multiply(exports.consts.bi_10.clone().pow(d[2].length)).add(fraction); - this.offset = -d[2].length; + var integer = new BigInteger(d[2]); + var fraction = new BigInteger(d[3]); + var precision = d[3].length; + + this.value = integer.multiply(exports.consts.bi_10.clone().pow(precision)).add(fraction); + this.offset = -precision; + this.is_negative = !!d[1]; + + this.canonicalize(); } - else - { + else if (i) { // integer notation - this.value = new BigInteger(j); - this.offset = 0; + this.value = new BigInteger(i[2]); + this.offset = 0; + this.is_negative = !!i[1]; + + this.canonicalize(); + } + else { + this.value = NaN; } - - this.is_native = false; - this.is_negative = undefined; - - this.canonicalize(); } else if (j.constructor == BigInteger) { this.value = j.clone(); @@ -401,7 +412,10 @@ Amount.prototype.parse_json = function(j) { this.issuer = new UInt160(); } } - else if ('object' === typeof j && j.currency) { + else if ('object' === typeof j && j.constructor == Amount) { + j.copyTo(this); + } + else if ('object' === typeof j && 'value' in j) { // Parse the passed value to sanitize and copy it. this.parse_value(j.value); @@ -409,7 +423,7 @@ Amount.prototype.parse_json = function(j) { this.issuer.parse_json(j.issuer); } else { - this.value = NaN; + this.value = NaN; } return this;