From 1ad6e5a15f7e7d591e140f1d179bb26a34efce53 Mon Sep 17 00:00:00 2001 From: Geert Weening Date: Wed, 5 Nov 2014 16:38:18 -0800 Subject: [PATCH] [FIX] from_human 'XRP' with full name from_human 'XRP - Ripples' should result in native XRP --- src/js/ripple/currency.js | 20 ++++++++++++++------ test/currency-test.js | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index e370f8e1..88ecc7ab 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -71,12 +71,9 @@ Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) { switch (typeof j) { case 'string': - if (!j || /^(0|XRP)$/.test(j)) { - if (shouldInterpretXrpAsIou) { - this.parse_hex(Currency.HEX_CURRENCY_BAD); - } else { - this.parse_hex(Currency.HEX_ZERO); - } + // if an empty string is given, fall back to XRP + if (!j) { + this.parse_hex(shouldInterpretXrpAsIou ? Currency.HEX_CURRENCY_BAD : Currency.HEX_ZERO); break; } @@ -86,6 +83,17 @@ Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) { if (matches) { var currencyCode = matches[1]; + + // for the currency 'XRP' case + // we drop everything else that could have been provided + // e.g. 'XRP - Ripple' + if (!currencyCode || /^(0|XRP)$/.test(currencyCode)) { + this.parse_hex(shouldInterpretXrpAsIou ? Currency.HEX_CURRENCY_BAD : Currency.HEX_ZERO); + + // early break, we can't have interest on XRP + break; + } + // the full currency is matched as it is part of the valid currency format, but not stored // var full_currency = matches[2] || ''; var interest = matches[3] || ''; diff --git a/test/currency-test.js b/test/currency-test.js index ca2093fc..fe0f1ed0 100644 --- a/test/currency-test.js +++ b/test/currency-test.js @@ -16,11 +16,22 @@ describe('Currency', function() { }); }); describe('from_json', function() { + it('from_json().to_json() == "XRP"', function() { + var r = currency.from_json(); + assert(!r.is_valid()); + assert.strictEqual('XRP', r.to_json()); + }); it('from_json(NaN).to_json() == "XRP"', function() { var r = currency.from_json(NaN); assert(!r.is_valid()); assert.strictEqual('XRP', r.to_json()); }); + it('from_json().to_json("") == "XRP"', function() { + var r = currency.from_json(''); + assert(r.is_valid()); + assert(r.is_native()); + assert.strictEqual('XRP', r.to_json()); + }); it('from_json("XRP").to_json() == "XRP"', function() { var r = currency.from_json('XRP'); assert(r.is_valid()); @@ -103,6 +114,16 @@ describe('Currency', function() { var cur = currency.from_human('INR - 30 Indian Rupees'); assert.strictEqual(cur.to_json(), 'INR'); }); + it('From human "XRP"', function() { + var cur = currency.from_human('XRP'); + assert.strictEqual(cur.to_json(), 'XRP'); + assert(cur.is_native(), true); + }); + it('From human "XRP - Ripples"', function() { + var cur = currency.from_human('XRP - Ripples'); + assert.strictEqual(cur.to_json(), 'XRP'); + assert(cur.is_native(), true); + }); });