mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +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) {
|
Amount.prototype.set_currency = function (c) {
|
||||||
if ('string' === typeof c) {
|
|
||||||
this._currency = Currency.from_json(c);
|
this._currency = Currency.from_json(c);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this._currency = c;
|
|
||||||
}
|
|
||||||
this._is_native = this._currency.is_native();
|
this._is_native = this._currency.is_native();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -22,9 +22,11 @@ Currency.json_rewrite = function (j) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Currency.from_json = function (j) {
|
Currency.from_json = function (j) {
|
||||||
if (j instanceof Currency) return j.clone();
|
if (j instanceof Currency) {
|
||||||
else if ('string' === typeof j || 'number' === typeof j) return (new Currency()).parse_json(j);
|
return j.clone();
|
||||||
else return new Currency(); // NaN
|
} else {
|
||||||
|
return new Currency().parse_json(j);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Currency.is_valid = function (j) {
|
Currency.is_valid = function (j) {
|
||||||
@@ -49,17 +51,23 @@ Currency.prototype.equals = function (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 || "XRP" === j) {
|
if (j instanceof Currency) {
|
||||||
this._value = 0;
|
|
||||||
}
|
|
||||||
else if ('number' === typeof j) {
|
|
||||||
// XXX This is a hack
|
|
||||||
this._value = j;
|
this._value = j;
|
||||||
}
|
} else if ('string' === typeof j) {
|
||||||
else if ('string' != typeof j || 3 !== j.length) {
|
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;
|
this._value = NaN;
|
||||||
}
|
}
|
||||||
else {
|
} else if ('number' === typeof j) {
|
||||||
|
// XXX This is a hack
|
||||||
|
this._value = j;
|
||||||
|
} else if ('string' != typeof j || 3 !== j.length) {
|
||||||
|
this._value = NaN;
|
||||||
|
} else {
|
||||||
this._value = j;
|
this._value = j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user