mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Refactor: Currency parsing.
This commit is contained in:
@@ -795,13 +795,7 @@ Amount.prototype.parse_value = function (j) {
|
||||
};
|
||||
|
||||
Amount.prototype.set_currency = function (c) {
|
||||
if ('string' === typeof c) {
|
||||
this._currency = Currency.from_json(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._currency = c;
|
||||
}
|
||||
this._currency = Currency.from_json(c);
|
||||
this._is_native = this._currency.is_native();
|
||||
|
||||
return this;
|
||||
|
||||
@@ -22,9 +22,11 @@ Currency.json_rewrite = function (j) {
|
||||
};
|
||||
|
||||
Currency.from_json = function (j) {
|
||||
if (j instanceof Currency) return j.clone();
|
||||
else if ('string' === typeof j || 'number' === typeof j) return (new Currency()).parse_json(j);
|
||||
else return new Currency(); // NaN
|
||||
if (j instanceof Currency) {
|
||||
return j.clone();
|
||||
} else {
|
||||
return new Currency().parse_json(j);
|
||||
}
|
||||
};
|
||||
|
||||
Currency.is_valid = function (j) {
|
||||
@@ -49,17 +51,23 @@ Currency.prototype.equals = function (d) {
|
||||
|
||||
// this._value = NaN on error.
|
||||
Currency.prototype.parse_json = function (j) {
|
||||
if ("" === j || "0" === j || "XRP" === j) {
|
||||
this._value = 0;
|
||||
}
|
||||
else if ('number' === typeof j) {
|
||||
if (j instanceof Currency) {
|
||||
this._value = j;
|
||||
} else if ('string' === typeof j) {
|
||||
if (j === "" || j === "0" || j === "XRP") {
|
||||
// XRP is never allowed as a Currency object
|
||||
this._value = 0;
|
||||
} else if (j.length === 3) {
|
||||
this._value = j;
|
||||
} else {
|
||||
this._value = NaN;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
this._value = j;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user