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:
@@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user