[FIX] from_human 'XRP' with full name

from_human 'XRP - Ripples' should result in native XRP
This commit is contained in:
Geert Weening
2014-11-05 16:38:18 -08:00
parent 3554572db7
commit 1ad6e5a15f
2 changed files with 35 additions and 6 deletions

View File

@@ -71,12 +71,9 @@ Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) {
switch (typeof j) { switch (typeof j) {
case 'string': case 'string':
if (!j || /^(0|XRP)$/.test(j)) { // if an empty string is given, fall back to XRP
if (shouldInterpretXrpAsIou) { if (!j) {
this.parse_hex(Currency.HEX_CURRENCY_BAD); this.parse_hex(shouldInterpretXrpAsIou ? Currency.HEX_CURRENCY_BAD : Currency.HEX_ZERO);
} else {
this.parse_hex(Currency.HEX_ZERO);
}
break; break;
} }
@@ -86,6 +83,17 @@ Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) {
if (matches) { if (matches) {
var currencyCode = matches[1]; var currencyCode = matches[1];
// for the currency 'XRP' case
// we drop everything else that could have been provided
// e.g. 'XRP - Ripple'
if (!currencyCode || /^(0|XRP)$/.test(currencyCode)) {
this.parse_hex(shouldInterpretXrpAsIou ? Currency.HEX_CURRENCY_BAD : Currency.HEX_ZERO);
// early break, we can't have interest on XRP
break;
}
// the full currency is matched as it is part of the valid currency format, but not stored // the full currency is matched as it is part of the valid currency format, but not stored
// var full_currency = matches[2] || ''; // var full_currency = matches[2] || '';
var interest = matches[3] || ''; var interest = matches[3] || '';

View File

@@ -16,11 +16,22 @@ describe('Currency', function() {
}); });
}); });
describe('from_json', function() { describe('from_json', function() {
it('from_json().to_json() == "XRP"', function() {
var r = currency.from_json();
assert(!r.is_valid());
assert.strictEqual('XRP', r.to_json());
});
it('from_json(NaN).to_json() == "XRP"', function() { it('from_json(NaN).to_json() == "XRP"', function() {
var r = currency.from_json(NaN); var r = currency.from_json(NaN);
assert(!r.is_valid()); assert(!r.is_valid());
assert.strictEqual('XRP', r.to_json()); assert.strictEqual('XRP', r.to_json());
}); });
it('from_json().to_json("") == "XRP"', function() {
var r = currency.from_json('');
assert(r.is_valid());
assert(r.is_native());
assert.strictEqual('XRP', r.to_json());
});
it('from_json("XRP").to_json() == "XRP"', function() { it('from_json("XRP").to_json() == "XRP"', function() {
var r = currency.from_json('XRP'); var r = currency.from_json('XRP');
assert(r.is_valid()); assert(r.is_valid());
@@ -103,6 +114,16 @@ describe('Currency', function() {
var cur = currency.from_human('INR - 30 Indian Rupees'); var cur = currency.from_human('INR - 30 Indian Rupees');
assert.strictEqual(cur.to_json(), 'INR'); assert.strictEqual(cur.to_json(), 'INR');
}); });
it('From human "XRP"', function() {
var cur = currency.from_human('XRP');
assert.strictEqual(cur.to_json(), 'XRP');
assert(cur.is_native(), true);
});
it('From human "XRP - Ripples"', function() {
var cur = currency.from_human('XRP - Ripples');
assert.strictEqual(cur.to_json(), 'XRP');
assert(cur.is_native(), true);
});
}); });