mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
[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
This commit is contained in:
@@ -316,17 +316,17 @@ Currency.prototype.to_json = function(opts) {
|
|||||||
var opts = opts || {};
|
var opts = opts || {};
|
||||||
|
|
||||||
var currency;
|
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
|
if (!opts.force_hex && /^[A-Z0-9]{3}$/.test(this._iso_code)) {
|
||||||
// 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()) {
|
|
||||||
currency = this._iso_code + fullName;
|
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 {
|
} else {
|
||||||
|
|
||||||
// Fallback to returning the raw currency hex
|
// Fallback to returning the raw currency hex
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ describe('Currency', function() {
|
|||||||
var cur = currency.from_human('EUR (0.5361%pa)');
|
var cur = currency.from_human('EUR (0.5361%pa)');
|
||||||
assert.strictEqual(cur.to_json(), 'EUR (0.54%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: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);
|
assert.strictEqual(cur.get_interest_percentage_at(undefined, 4), 0.5361);
|
||||||
});
|
});
|
||||||
it('From human "TYX - 30-Year Treasuries (1.5%pa)"', function() {
|
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());
|
assert.strictEqual('XRP', currency.from_json(NaN).to_human());
|
||||||
});
|
});
|
||||||
it('"015841551A748AD2C1F76FF6ECB0CCCD00000000") == "015841551A748AD2C1F76FF6ECB0CCCD00000000"', function() {
|
it('"015841551A748AD2C1F76FF6ECB0CCCD00000000") == "015841551A748AD2C1F76FF6ECB0CCCD00000000"', function() {
|
||||||
assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human(),
|
assert.strictEqual(currency.from_json("015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human(), 'XAU (-0.5%pa)');
|
||||||
'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() {
|
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'}));
|
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");
|
var cur = currency.from_json("TIM");
|
||||||
assert.strictEqual(cur.to_human({full_name: null}), "TIM");
|
assert.strictEqual(cur.to_human({full_name: null}), "TIM");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('from_hex', function() {
|
describe('from_hex', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user