[FIX] Currency constructor with Currency object

clone the given Currency instance in `parse_json`
This commit is contained in:
Geert Weening
2014-11-06 21:36:57 -08:00
parent 9025e8bfa8
commit a8ef614b81
2 changed files with 18 additions and 8 deletions

View File

@@ -53,11 +53,7 @@ Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
Currency.prototype.human_RE = /^\s*([a-zA-Z0-9]{3})(\s*-\s*[- \w]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/; Currency.prototype.human_RE = /^\s*([a-zA-Z0-9]{3})(\s*-\s*[- \w]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/;
Currency.from_json = function(j, shouldInterpretXrpAsIou) { Currency.from_json = function(j, shouldInterpretXrpAsIou) {
if (j instanceof this) { return (new Currency()).parse_json(j, shouldInterpretXrpAsIou);
return j.clone();
} else {
return (new this()).parse_json(j, shouldInterpretXrpAsIou);
}
}; };
Currency.from_human = function(j, opts) { Currency.from_human = function(j, opts) {
@@ -66,6 +62,10 @@ Currency.from_human = function(j, opts) {
// this._value = NaN on error. // this._value = NaN on error.
Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) { Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) {
if (j instanceof Currency) {
return j.clone();
}
this._value = NaN; this._value = NaN;
switch (typeof j) { switch (typeof j) {

View File

@@ -162,10 +162,20 @@ describe('Currency', function() {
assert.strictEqual(cur.to_json(), cur.to_human()); assert.strictEqual(cur.to_json(), cur.to_human());
}); });
}); });
describe('parse_json(currency obj)', function() { describe('parse_json', function() {
assert.strictEqual('USD', new currency().parse_json(currency.from_json('USD')).to_json()); it('should parse a currency object', function() {
assert.strictEqual('USD', new currency().parse_json(currency.from_json('USD')).to_json());
assert.strictEqual('USD (0.5%pa)', new currency().parse_json(currency.from_json('USD (0.5%pa)')).to_json());
});
it('should clone for parse_json on itself', function() {
var cur = currency.from_json('USD');
var cur2 = currency.from_json(cur);
assert.strictEqual(cur.to_json(), cur2.to_json());
assert.strictEqual('USD (0.5%pa)', new currency().parse_json(currency.from_json('USD (0.5%pa)')).to_json()); cur = currency.from_hex('015841551A748AD2C1F76FF6ECB0CCCD00000000');
cur2 = currency.from_json(cur);
assert.strictEqual(cur.to_json(), cur2.to_json());
});
}); });
describe('is_valid', function() { describe('is_valid', function() {