mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
JS: Lots of fix for Amount parsing.
This commit is contained in:
committed by
Stefan Thomas
parent
09b29f7ab3
commit
c020f14c19
68
js/amount.js
68
js/amount.js
@@ -37,7 +37,7 @@ UInt160.prototype.copyTo = function(d) {
|
|||||||
return d;
|
return d;
|
||||||
};
|
};
|
||||||
|
|
||||||
// value === NaN on error.
|
// value = NaN on error.
|
||||||
UInt160.prototype.parse_json = function (j) {
|
UInt160.prototype.parse_json = function (j) {
|
||||||
// Canonicalize and validate
|
// Canonicalize and validate
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ Currency.prototype.copyTo = function(d) {
|
|||||||
return d;
|
return d;
|
||||||
};
|
};
|
||||||
|
|
||||||
// this.value === NaN on error.
|
// this.value = NaN on error.
|
||||||
Currency.prototype.parse_json = function(j) {
|
Currency.prototype.parse_json = function(j) {
|
||||||
if ("" === j || "0" === j || "XNS" === j) {
|
if ("" === j || "0" === j || "XNS" === j) {
|
||||||
this.value = 0;
|
this.value = 0;
|
||||||
@@ -194,12 +194,12 @@ Amount.prototype.copyTo = function(d) {
|
|||||||
|
|
||||||
// YYY Might also provide is_valid_json.
|
// YYY Might also provide is_valid_json.
|
||||||
Amount.prototype.is_valid = function() {
|
Amount.prototype.is_valid = function() {
|
||||||
return NaN !== this.value;
|
return !isNaN(this.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Convert only value to JSON wire format.
|
// Convert only value to JSON wire format.
|
||||||
Amount.prototype.to_text = function(allow_nan) {
|
Amount.prototype.to_text = function(allow_nan) {
|
||||||
if (NaN === this.value) {
|
if (isNaN(this.value)) {
|
||||||
// Never should happen.
|
// Never should happen.
|
||||||
return allow_nan ? NaN : "0";
|
return allow_nan ? NaN : "0";
|
||||||
}
|
}
|
||||||
@@ -240,7 +240,7 @@ Amount.prototype.to_text = function(allow_nan) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Amount.prototype.canonicalize = function() {
|
Amount.prototype.canonicalize = function() {
|
||||||
if (NaN === this.value || !this.currency) {
|
if (isNaN(this.value) || !this.currency) {
|
||||||
// nothing
|
// nothing
|
||||||
}
|
}
|
||||||
else if (this.value.equals(BigInteger.ZERO)) {
|
else if (this.value.equals(BigInteger.ZERO)) {
|
||||||
@@ -285,7 +285,7 @@ Amount.prototype.to_json = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Amount.prototype.to_text_full = function() {
|
Amount.prototype.to_text_full = function() {
|
||||||
return this.value === NaN
|
return isNaN(this.value)
|
||||||
? NaN
|
? NaN
|
||||||
: this.is_native
|
: this.is_native
|
||||||
? this.to_text() + "/XNS"
|
? this.to_text() + "/XNS"
|
||||||
@@ -335,44 +335,55 @@ Amount.prototype.parse_native = function(j) {
|
|||||||
|
|
||||||
// Parse a non-native value.
|
// Parse a non-native value.
|
||||||
Amount.prototype.parse_value = function(j) {
|
Amount.prototype.parse_value = function(j) {
|
||||||
|
this.is_native = false;
|
||||||
|
|
||||||
if ('number' === typeof j) {
|
if ('number' === typeof j) {
|
||||||
|
this.is_negative = j < 0;
|
||||||
|
if (this.is_negative) j = -j;
|
||||||
this.value = new BigInteger(j);
|
this.value = new BigInteger(j);
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
this.is_native = false;
|
|
||||||
this.is_negative = j < 0;
|
|
||||||
|
|
||||||
this.canonicalize();
|
this.canonicalize();
|
||||||
}
|
}
|
||||||
else if ('string' === typeof j) {
|
else if ('string' === typeof j) {
|
||||||
var e = j.match(/^(-?\d+)e(\d+)/);
|
var i = j.match(/^(-?)(\d+)$/);
|
||||||
var d = j.match(/^(-?\d+)\.(\d+)/);
|
var d = !i && j.match(/^(-?)(\d+)\.(\d*)$/);
|
||||||
|
var e = !e && j.match(/^(-?)(\d+)e(\d+)$/);
|
||||||
|
|
||||||
if (e) {
|
if (e) {
|
||||||
// e notation
|
// e notation
|
||||||
|
|
||||||
this.value = new BigInteger(e[1]);
|
this.value = new BigInteger(e[2]);
|
||||||
this.offset = parseInt(e[2]);
|
this.offset = parseInt(e[3]);
|
||||||
|
this.is_negative = !!e[1];
|
||||||
|
|
||||||
|
this.canonicalize();
|
||||||
}
|
}
|
||||||
else if (d) {
|
else if (d) {
|
||||||
// float notation
|
// float notation
|
||||||
|
|
||||||
var integer = new BigInteger(d[1]);
|
var integer = new BigInteger(d[2]);
|
||||||
var fraction = new BigInteger(d[2]);
|
var fraction = new BigInteger(d[3]);
|
||||||
this.value = integer.multiply(exports.consts.bi_10.clone().pow(d[2].length)).add(fraction);
|
var precision = d[3].length;
|
||||||
this.offset = -d[2].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
|
// integer notation
|
||||||
|
|
||||||
this.value = new BigInteger(j);
|
this.value = new BigInteger(i[2]);
|
||||||
this.offset = 0;
|
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) {
|
else if (j.constructor == BigInteger) {
|
||||||
this.value = j.clone();
|
this.value = j.clone();
|
||||||
@@ -401,7 +412,10 @@ Amount.prototype.parse_json = function(j) {
|
|||||||
this.issuer = new UInt160();
|
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.
|
// Parse the passed value to sanitize and copy it.
|
||||||
|
|
||||||
this.parse_value(j.value);
|
this.parse_value(j.value);
|
||||||
@@ -409,7 +423,7 @@ Amount.prototype.parse_json = function(j) {
|
|||||||
this.issuer.parse_json(j.issuer);
|
this.issuer.parse_json(j.issuer);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.value = NaN;
|
this.value = NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user