[FEATURE] allow numbers, hyphens and spaces in full currency

This commit is contained in:
Geert Weening
2014-05-29 13:34:27 -07:00
parent 2a832777a7
commit decebe3d2e
2 changed files with 27 additions and 9 deletions

View File

@@ -31,22 +31,26 @@ Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
* *
* Examples: * Examples:
* *
* USD => currency * USD => currency
* USD - Dollar => currency with optional full currency name * USD - Dollar => currency with optional full currency name
* XAU (-0.5%pa) => XAU with 0.5% effective demurrage rate per year * XAU (-0.5%pa) => XAU with 0.5% effective demurrage rate per year
* XAU - Gold (-0.5%pa) => Optionally allowed full currency name * XAU - Gold (-0.5%pa) => Optionally allowed full currency name
* USD (1%pa) => US dollars with 1% effective interest per year * USD (1%pa) => US dollars with 1% effective interest per year
* INR - Indian Rupees => Optional full currency name with spaces
* TYX - 30-Year Treasuries => Optional full currency with numbers and a dash
* TYX - 30-Year Treasuries (1.5%pa) => Optional full currency with numbers, dash and interest rate
* *
* The regular expression below matches above cases, broken down for better understanding: * The regular expression below matches above cases, broken down for better understanding:
* *
* ^\s* // start with any amount of whitespace * ^\s* // start with any amount of whitespace
* ([a-zA-Z]{3}) // any 3 letter currency code * ([a-zA-Z]{3}) // any 3 letter currency code
* (\s*-\s*[a-zA-z]+)? // optional full currency name following the dash after currency code * (\s*-\s*[- \w]+) // optional full currency name following the dash after currency code,
* full currency code can contain letters, numbers and dashes
* (\s*\(-?\d+\.?\d*%pa\))? // optional demurrage rate, has optional - and . notation (-0.5%pa) * (\s*\(-?\d+\.?\d*%pa\))? // optional demurrage rate, has optional - and . notation (-0.5%pa)
* \s*$ // end with any amount of whitespace * \s*$ // end with any amount of whitespace
* *
*/ */
Currency.prototype.human_RE = /^\s*([a-zA-Z]{3})(\s*-\s*[a-zA-z]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/; Currency.prototype.human_RE = /^\s*([a-zA-Z]{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) { if (j instanceof this) {

View File

@@ -36,18 +36,32 @@ describe('Currency', function() {
assert.strictEqual(cur.to_hex(), '0155534400000000C19A22BC51297F0B00000000'); assert.strictEqual(cur.to_hex(), '0155534400000000C19A22BC51297F0B00000000');
assert.strictEqual(cur.to_json(), cur.to_human()); assert.strictEqual(cur.to_json(), cur.to_human());
}); });
it('From human "EUR (-0.5%pa)', function() { it('From human "EUR (-0.5%pa)', function() {
var cur = currency.from_human('EUR (-0.5%pa)'); var cur = currency.from_human('EUR (-0.5%pa)');
assert.strictEqual(cur.to_json(), 'EUR (-0.5%pa)'); assert.strictEqual(cur.to_json(), 'EUR (-0.5%pa)');
}); });
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(4), 'EUR (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() {
var cur = currency.from_human('TYX - 30-Year Treasuries (1.5%pa)');
assert.strictEqual(cur.to_json(), 'TYX (1.5%pa)');
});
it('From human "TYX - 30-Year Treasuries"', function() {
var cur = currency.from_human('TYX - 30-Year Treasuries');
assert.strictEqual(cur.to_json(), 'TYX');
});
it('From human "INR - Indian Rupees (-0.5%)"', function() {
var cur = currency.from_human('INR - Indian Rupees (-0.5%pa)');
assert.strictEqual(cur.to_json(), 'INR (-0.5%pa)');
});
it('From human "INR - 30 Indian Rupees"', function() {
var cur = currency.from_human('INR - 30 Indian Rupees');
assert.strictEqual(cur.to_json(), 'INR');
});
}); });