mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
Add Amount.from_number().
This commit is contained in:
@@ -65,6 +65,10 @@ Amount.json_rewrite = function (j) {
|
|||||||
return Amount.from_json(j).to_json();
|
return Amount.from_json(j).to_json();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Amount.from_number = function (n) {
|
||||||
|
return (new Amount()).parse_number(n);
|
||||||
|
};
|
||||||
|
|
||||||
Amount.from_json = function (j) {
|
Amount.from_json = function (j) {
|
||||||
return (new Amount()).parse_json(j);
|
return (new Amount()).parse_json(j);
|
||||||
};
|
};
|
||||||
@@ -624,6 +628,18 @@ Amount.prototype.parse_quality = function (q, c, i) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Amount.prototype.parse_number = function (n) {
|
||||||
|
this._currency = Currency.from_json(1);
|
||||||
|
this._issuer = UInt160.from_json(1);
|
||||||
|
this._is_negative = n < 0 ? 1 : 0;
|
||||||
|
this._value = new BigInteger(this._is_negative ? -n : n);
|
||||||
|
this._offset = 0;
|
||||||
|
|
||||||
|
this.canonicalize();
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
// <-> j
|
// <-> j
|
||||||
Amount.prototype.parse_json = function (j) {
|
Amount.prototype.parse_json = function (j) {
|
||||||
if ('string' === typeof j) {
|
if ('string' === typeof j) {
|
||||||
|
|||||||
@@ -52,6 +52,10 @@ Currency.prototype.parse_json = function (j) {
|
|||||||
if ("" === j || "0" === j || "XRP" === j) {
|
if ("" === j || "0" === j || "XRP" === j) {
|
||||||
this._value = 0;
|
this._value = 0;
|
||||||
}
|
}
|
||||||
|
else if ('number' === typeof j) {
|
||||||
|
// XXX This is a hack
|
||||||
|
this._value = j;
|
||||||
|
}
|
||||||
else if ('string' != typeof j || 3 !== j.length) {
|
else if ('string' != typeof j || 3 !== j.length) {
|
||||||
this._value = NaN;
|
this._value = NaN;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,10 @@ UInt160.prototype.parse_json = function (j) {
|
|||||||
if (config.accounts && j in config.accounts)
|
if (config.accounts && j in config.accounts)
|
||||||
j = config.accounts[j].account;
|
j = config.accounts[j].account;
|
||||||
|
|
||||||
if ('string' !== typeof j) {
|
if ('number' === typeof j) {
|
||||||
|
this._value = new BigInteger(j);
|
||||||
|
}
|
||||||
|
else if ('string' !== typeof j) {
|
||||||
this._value = NaN;
|
this._value = NaN;
|
||||||
}
|
}
|
||||||
else if (j[0] === "r") {
|
else if (j[0] === "r") {
|
||||||
|
|||||||
Reference in New Issue
Block a user