From 947ec3edc2e7c8f1ef097e496bf552c74366e749 Mon Sep 17 00:00:00 2001 From: Geert Weening Date: Thu, 9 Oct 2014 13:59:52 -0700 Subject: [PATCH] [FEATURE] add flag to show or hide interest in to_human/to_json The show_interest flag will default to true for interest bearing currencies and false for currencies without interest --- src/js/ripple/currency.js | 16 ++++++++-------- test/currency-test.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index f7dd9ce9..e370f8e1 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -316,17 +316,17 @@ Currency.prototype.to_json = function(opts) { var opts = opts || {}; var currency; - var fullName = opts && opts.full_name ? " - " + opts.full_name : ""; + var fullName = opts && opts.full_name ? ' - ' + opts.full_name : ''; + opts.show_interest = opts.show_interest !== void(0) ? opts.show_interest : this.has_interest(); - // Any currency with standard properties and a valid code can be abbreviated - // in the JSON wire format as the three character code. - if (!opts.force_hex && /^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) { + if (!opts.force_hex && /^[A-Z0-9]{3}$/.test(this._iso_code)) { currency = this._iso_code + fullName; + if (opts.show_interest) { + var decimals = !isNaN(opts.decimals) ? opts.decimals : void(0); + var interestPercentage = this.has_interest() ? this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, decimals) : 0; + currency += ' (' + interestPercentage + '%pa)'; + } - // If there is interest, append the annual interest to the full currency name - } else if (!opts.force_hex && this.has_interest()) { - 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 diff --git a/test/currency-test.js b/test/currency-test.js index 7fbda951..ca2093fc 100644 --- a/test/currency-test.js +++ b/test/currency-test.js @@ -82,6 +82,9 @@ describe('Currency', 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.to_json({decimals:void(0), full_name:'Euro'}), 'EUR - Euro (0.54%pa)'); + assert.strictEqual(cur.to_json({decimals:undefined, full_name:'Euro'}), 'EUR - Euro (0.54%pa)'); + assert.strictEqual(cur.to_json({decimals:'henk', full_name:'Euro'}), 'EUR - Euro (0.54%pa)'); assert.strictEqual(cur.get_interest_percentage_at(undefined, 4), 0.5361); }); it('From human "TYX - 30-Year Treasuries (1.5%pa)"', function() { @@ -111,8 +114,28 @@ describe('Currency', function() { assert.strictEqual('XRP', currency.from_json(NaN).to_human()); }); it('"015841551A748AD2C1F76FF6ECB0CCCD00000000") == "015841551A748AD2C1F76FF6ECB0CCCD00000000"', function() { - assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human(), - 'XAU (-0.5%pa)'); + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human(), 'XAU (-0.5%pa)'); + }); + it('"015841551A748AD2C1F76FF6ECB0CCCD00000000") == "015841551A748AD2C1F76FF6ECB0CCCD00000000"', function() { + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human({full_name:'Gold'}), 'XAU - Gold (-0.5%pa)'); + }); + it('to_human interest XAU with full name, do not show interest', function() { + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human({full_name:'Gold', show_interest:false}), 'XAU - Gold'); + }); + it('to_human interest XAU with full name, show interest', function() { + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human({full_name:'Gold', show_interest:true}), 'XAU - Gold (-0.5%pa)'); + }); + it('to_human interest XAU, do show interest', function() { + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human({show_interest:true}), 'XAU (-0.5%pa)'); + }); + it('to_human interest XAU, do not show interest', function() { + assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human({show_interest:false}), 'XAU'); + }); + it('to_human with full_name "USD - US Dollar show interest"', function() { + assert.strictEqual(currency.from_json('USD').to_human({full_name:'US Dollar', show_interest:true}), 'USD - US Dollar (0%pa)'); + }); + it('to_human with full_name "USD - US Dollar do not show interest"', function() { + assert.strictEqual(currency.from_json('USD').to_human({full_name:'US Dollar', show_interest:false}), 'USD - US Dollar'); }); 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'})); @@ -128,6 +151,7 @@ describe('Currency', function() { var cur = currency.from_json("TIM"); assert.strictEqual(cur.to_human({full_name: null}), "TIM"); }); + }); describe('from_hex', function() {