From fd67ea3036dc7cd119b7babc50b07c29758a75d7 Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Mon, 29 Jul 2013 18:28:25 -0700 Subject: [PATCH] Refactor: Currency parsing. --- src/js/ripple/amount.js | 8 +------- src/js/ripple/currency.js | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/js/ripple/amount.js b/src/js/ripple/amount.js index 5e09e3eb..1a81369b 100644 --- a/src/js/ripple/amount.js +++ b/src/js/ripple/amount.js @@ -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; diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index 49bb5429..8bcb5445 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -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; }