From a8ef614b8116b54e9d02f6f2434ecd5ac4d88974 Mon Sep 17 00:00:00 2001 From: Geert Weening Date: Thu, 6 Nov 2014 21:36:57 -0800 Subject: [PATCH] [FIX] Currency constructor with Currency object clone the given Currency instance in `parse_json` --- src/js/ripple/currency.js | 10 +++++----- test/currency-test.js | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index 8d179f85..10dfeef0 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -53,11 +53,7 @@ Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000'; Currency.prototype.human_RE = /^\s*([a-zA-Z0-9]{3})(\s*-\s*[- \w]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/; Currency.from_json = function(j, shouldInterpretXrpAsIou) { - if (j instanceof this) { - return j.clone(); - } else { - return (new this()).parse_json(j, shouldInterpretXrpAsIou); - } + return (new Currency()).parse_json(j, shouldInterpretXrpAsIou); }; Currency.from_human = function(j, opts) { @@ -66,6 +62,10 @@ Currency.from_human = function(j, opts) { // this._value = NaN on error. Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) { + if (j instanceof Currency) { + return j.clone(); + } + this._value = NaN; switch (typeof j) { diff --git a/test/currency-test.js b/test/currency-test.js index ca2093fc..ef1a36c5 100644 --- a/test/currency-test.js +++ b/test/currency-test.js @@ -162,10 +162,20 @@ describe('Currency', function() { assert.strictEqual(cur.to_json(), cur.to_human()); }); }); - describe('parse_json(currency obj)', function() { - assert.strictEqual('USD', new currency().parse_json(currency.from_json('USD')).to_json()); + describe('parse_json', function() { + it('should parse a currency object', function() { + assert.strictEqual('USD', new currency().parse_json(currency.from_json('USD')).to_json()); + assert.strictEqual('USD (0.5%pa)', new currency().parse_json(currency.from_json('USD (0.5%pa)')).to_json()); + }); + it('should clone for parse_json on itself', function() { + var cur = currency.from_json('USD'); + var cur2 = currency.from_json(cur); + assert.strictEqual(cur.to_json(), cur2.to_json()); - assert.strictEqual('USD (0.5%pa)', new currency().parse_json(currency.from_json('USD (0.5%pa)')).to_json()); + cur = currency.from_hex('015841551A748AD2C1F76FF6ECB0CCCD00000000'); + cur2 = currency.from_json(cur); + assert.strictEqual(cur.to_json(), cur2.to_json()); + }); }); describe('is_valid', function() {