[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:
Geert Weening
2014-06-23 14:48:54 -07:00
parent 9527d6ed22
commit a573465e41
2 changed files with 20 additions and 2 deletions

View File

@@ -313,16 +313,18 @@ Currency.prototype.to_json = function(opts) {
return 'XRP'; return 'XRP';
} }
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 : "";
// 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 (!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 there is interest, append the annual interest to the full currency name // 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; var decimals = opts ? opts.decimals : undefined;
currency = this._iso_code + fullName + " (" + this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, decimals) + "%pa)"; currency = this._iso_code + fullName + " (" + this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, decimals) + "%pa)";
} else { } else {

View File

@@ -37,6 +37,22 @@ describe('Currency', function() {
assert(!r.is_valid()); assert(!r.is_valid());
assert.strictEqual('XRP', r.to_json()); 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() { describe('from_human', function() {