From 3ef105e0774f0447990dc32faa3cdfb52e54f866 Mon Sep 17 00:00:00 2001 From: Geert Weening Date: Fri, 30 May 2014 11:23:04 -0700 Subject: [PATCH] [FEATURE] support full_name in to_human() and to_json() --- src/js/ripple/currency.js | 43 +++++++++++++++++++++------------------ test/currency-test.js | 14 ++++++++++++- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index 98e3be20..a026b6cc 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -307,40 +307,43 @@ Currency.prototype.get_interest_percentage_at = function(referenceDate, decimals // return this._value instanceof BigInteger && ...; //}; -Currency.prototype.to_json = function(percentageDecimals) { +Currency.prototype.to_json = function(opts) { if (!this.is_valid()) { // XXX This is backwards compatible behavior, but probably not very good. return 'XRP'; } + var currency; + var fullName = opts && opts.full_name ? " - " + opts.full_name : ""; + // Any currency with standard properties and a valid code can be abbreviated // in the JSON wire format as the three character code. if (/^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) { - return this._iso_code; + currency = this._iso_code + fullName; + + // If there is interest, append the annual interest to the full currency name } else if (this.has_interest()) { - return this._iso_code + " (" + this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, percentageDecimals) + "%pa)"; + var decimals = opts ? opts.decimals : undefined; + currency = this._iso_code + fullName + " (" + this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, decimals) + "%pa)"; + } else { + + // Fallback to returning the raw currency hex + currency = this.to_hex(); + + // XXX This is to maintain backwards compatibility, but it is very, very odd + // behavior, so we should deprecate it and get rid of it as soon as + // possible. + if (currency === Currency.HEX_ONE) { + currency = 1; + } } - // Fallback to returning the raw currency hex - var currencyHex = this.to_hex(); - - // XXX This is to maintain backwards compatibility, but it is very, very odd - // behavior, so we should deprecate it and get rid of it as soon as - // possible. - if (currencyHex === Currency.HEX_ONE) { - return 1; - } - - return currencyHex; + return currency; }; -Currency.prototype.to_human = function(percentageDecimals) { +Currency.prototype.to_human = function(opts) { // to_human() will always print the human-readable currency code if available. - if (/^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) { - return this._iso_code; - } - - return this.to_json(percentageDecimals); + return this.to_json(opts); }; exports.Currency = Currency; diff --git a/test/currency-test.js b/test/currency-test.js index a1680910..3625c33d 100644 --- a/test/currency-test.js +++ b/test/currency-test.js @@ -43,7 +43,13 @@ describe('Currency', function() { it('From human "EUR (0.5361%pa)", test decimals', function() { var cur = currency.from_human('EUR (0.5361%pa)'); assert.strictEqual(cur.to_json(), 'EUR (0.54%pa)'); - assert.strictEqual(cur.to_json(4), 'EUR (0.5361%pa)'); + assert.strictEqual(cur.to_json({decimals:4}), 'EUR (0.5361%pa)'); + assert.strictEqual(cur.get_interest_percentage_at(undefined, 4), 0.5361); + }); + it('From human "EUR - Euro (0.5361%pa)", test decimals and full_name', function() { + var cur = currency.from_human('EUR (0.5361%pa)'); + assert.strictEqual(cur.to_json(), 'EUR (0.54%pa)'); + assert.strictEqual(cur.to_json({decimals:4, full_name:'Euro'}), 'EUR - Euro (0.5361%pa)'); assert.strictEqual(cur.get_interest_percentage_at(undefined, 4), 0.5361); }); it('From human "TYX - 30-Year Treasuries (1.5%pa)"', function() { @@ -76,6 +82,12 @@ describe('Currency', function() { assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human(), 'XAU (-0.5%pa)'); }); + it('to_human with full_name "USD - US Dollar"', function() { + assert.strictEqual('USD - US Dollar', currency.from_json('USD').to_human({full_name:'US Dollar'})); + }); + it('to_human with full_name "XRP - Ripples"', function() { + assert.strictEqual('XRP - Ripples', currency.from_json('XRP').to_human({full_name:'Ripples'})); + }); }); describe('from_hex', function() {