Refactor: Currency parsing.

This commit is contained in:
Stefan Thomas
2013-07-29 18:28:25 -07:00
parent 006d1fadce
commit fd67ea3036
2 changed files with 20 additions and 18 deletions

View File

@@ -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;

View File

@@ -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;
}