Refactor currency parsing

This commit is contained in:
wltsmrz
2013-09-12 14:17:51 -07:00
parent 8dd1b62d78
commit bab9fec836

View File

@@ -51,26 +51,32 @@ 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 instanceof Currency) { var result = NaN;
j.copyTo(this);
} else if (typeof j === 'string') { switch (typeof j) {
if (j === "" || j === "0" || j === "XRP") { case 'string':
// XRP is never allowed as a Currency object if (!j || /^(0|XRP)$/.test(j)) {
this._value = 0; result = 0;
} else if (j.length === 3) { } else if (/^[a-zA-Z0-9]{3}$/.test(j)) {
this._value = j; result = j;
} else { }
this._value = NaN; break;
}
} else if (typeof j === 'number' && !isNaN(j)) { case 'number':
// XXX This is a hack if (!isNaN(j)) {
this._value = j; result = j;
} else if (typeof j !== 'string'|| j.length !== 3) { }
this._value = NaN; break;
} else {
this._value = j; case 'object':
if (j instanceof Currency) {
result = j.copyTo({})._value;
}
break;
} }
this._value = result;
return this; return this;
}; };