mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
[FEATURE] Currency: add option force hex in json format
provide the `force_hex` flag in the options object in a `to_json` or `json_rewrite` call
This commit is contained in:
@@ -313,16 +313,18 @@ Currency.prototype.to_json = function(opts) {
|
||||
return 'XRP';
|
||||
}
|
||||
|
||||
var opts = opts || {};
|
||||
|
||||
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()) {
|
||||
if (!opts.force_hex && /^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) {
|
||||
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 (!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 {
|
||||
|
||||
@@ -37,6 +37,22 @@ describe('Currency', function() {
|
||||
assert(!r.is_valid());
|
||||
assert.strictEqual('XRP', r.to_json());
|
||||
});
|
||||
it('from_json("XAU").to_json() hex', function() {
|
||||
var r = currency.from_json("XAU");
|
||||
assert.strictEqual('0000000000000000000000005841550000000000', r.to_json({force_hex: true}));
|
||||
});
|
||||
it('from_json("XAU (0.5%pa").to_json() hex', function() {
|
||||
var r = currency.from_json("XAU (0.5%pa)");
|
||||
assert.strictEqual('015841550000000041F78E0A28CBF19200000000', r.to_json({force_hex: true}));
|
||||
});
|
||||
it('json_rewrite("015841550000000041F78E0A28CBF19200000000").to_json() hex', function() {
|
||||
var r = currency.json_rewrite('015841550000000041F78E0A28CBF19200000000');
|
||||
assert.strictEqual('XAU (0.5%pa)', r);
|
||||
});
|
||||
it('json_rewrite("015841550000000041F78E0A28CBF19200000000") hex', function() {
|
||||
var r = currency.json_rewrite('015841550000000041F78E0A28CBF19200000000', {force_hex: true});
|
||||
assert.strictEqual('015841550000000041F78E0A28CBF19200000000', r);
|
||||
});
|
||||
});
|
||||
|
||||
describe('from_human', function() {
|
||||
|
||||
Reference in New Issue
Block a user