mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
[FEATURE] allow numbers, hyphens and spaces in full currency
This commit is contained in:
@@ -36,17 +36,21 @@ Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
|
||||
* XAU (-0.5%pa) => XAU with 0.5% effective demurrage rate per year
|
||||
* XAU - Gold (-0.5%pa) => Optionally allowed full currency name
|
||||
* 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:
|
||||
*
|
||||
* ^\s* // start with any amount of whitespace
|
||||
* ([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*$ // 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) {
|
||||
if (j instanceof this) {
|
||||
|
||||
@@ -36,18 +36,32 @@ describe('Currency', function() {
|
||||
assert.strictEqual(cur.to_hex(), '0155534400000000C19A22BC51297F0B00000000');
|
||||
assert.strictEqual(cur.to_json(), cur.to_human());
|
||||
});
|
||||
|
||||
it('From human "EUR (-0.5%pa)', function() {
|
||||
var cur = currency.from_human('EUR (-0.5%pa)');
|
||||
assert.strictEqual(cur.to_json(), 'EUR (-0.5%pa)');
|
||||
});
|
||||
|
||||
it('From human "EUR (0.5361%pa)", test decimals', function() {
|
||||
var cur = currency.from_human('EUR (0.5361%pa)');
|
||||
assert.strictEqual(cur.to_json(), 'EUR (0.54%pa)');
|
||||
assert.strictEqual(cur.to_json(4), 'EUR (0.5361%pa)');
|
||||
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');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user