mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
[FEATURE] support full_name in to_human() and to_json()
This commit is contained in:
@@ -307,40 +307,43 @@ Currency.prototype.get_interest_percentage_at = function(referenceDate, decimals
|
|||||||
// return this._value instanceof BigInteger && ...;
|
// return this._value instanceof BigInteger && ...;
|
||||||
//};
|
//};
|
||||||
|
|
||||||
Currency.prototype.to_json = function(percentageDecimals) {
|
Currency.prototype.to_json = function(opts) {
|
||||||
if (!this.is_valid()) {
|
if (!this.is_valid()) {
|
||||||
// XXX This is backwards compatible behavior, but probably not very good.
|
// XXX This is backwards compatible behavior, but probably not very good.
|
||||||
return 'XRP';
|
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
|
// Any currency with standard properties and a valid code can be abbreviated
|
||||||
// in the JSON wire format as the three character code.
|
// in the JSON wire format as the three character code.
|
||||||
if (/^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) {
|
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()) {
|
} 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
|
return currency;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Currency.prototype.to_human = function(percentageDecimals) {
|
Currency.prototype.to_human = function(opts) {
|
||||||
// to_human() will always print the human-readable currency code if available.
|
// 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.to_json(opts);
|
||||||
return this._iso_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.to_json(percentageDecimals);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.Currency = Currency;
|
exports.Currency = Currency;
|
||||||
|
|||||||
@@ -43,7 +43,13 @@ describe('Currency', function() {
|
|||||||
it('From human "EUR (0.5361%pa)", test decimals', function() {
|
it('From human "EUR (0.5361%pa)", test decimals', 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(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);
|
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() {
|
||||||
@@ -76,6 +82,12 @@ describe('Currency', 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('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() {
|
describe('from_hex', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user