mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
[FIX] decimal precision amount parsing
- add non-grouping operator to regex - add tests to cover the problematic cases
This commit is contained in:
@@ -602,14 +602,14 @@ Amount.prototype.invert = function() {
|
|||||||
* \s* // any amount of whitespace
|
* \s* // any amount of whitespace
|
||||||
* (-)? // optional dash
|
* (-)? // optional dash
|
||||||
* (\d+) // 1 or more digits
|
* (\d+) // 1 or more digits
|
||||||
* (\.(\d*))? // optional . character with any amount of digits
|
* (?:\.(\d*))? // optional . character with any amount of digits
|
||||||
* \s* // any amount of whitespace
|
* \s* // any amount of whitespace
|
||||||
* ([a-f0-9]{40}|[a-z0-9]{3})? // optional 40 character hex string OR 3 letters
|
* ([a-f0-9]{40}|[a-z0-9]{3})? // optional 40 character hex string OR 3 letters
|
||||||
* \s* // any amount of whitespace
|
* \s* // any amount of whitespace
|
||||||
* $ // end of string
|
* $ // end of string
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
Amount.human_RE = /^\s*([a-z]{3}|[0-9]{3})?\s*(-)?(\d+)(\.(\d*))?\s*([a-f0-9]{40}|[a-z0-9]{3})?\s*$/i;
|
Amount.human_RE = /^\s*([a-z]{3}|[0-9]{3})?\s*(-)?(\d+)(?:\.(\d*))?\s*([a-f0-9]{40}|[a-z0-9]{3})?\s*$/i;
|
||||||
|
|
||||||
Amount.prototype.parse_human = function(j, opts) {
|
Amount.prototype.parse_human = function(j, opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|||||||
@@ -16,6 +16,22 @@ describe('Amount', function() {
|
|||||||
assert(Amount.from_json('1').is_positive());
|
assert(Amount.from_json('1').is_positive());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('from_human', function() {
|
||||||
|
it('1 XRP', function() {
|
||||||
|
assert.strictEqual(Amount.from_human("1 XRP").to_text_full(), '1/XRP');
|
||||||
|
});
|
||||||
|
it('0.1 XRP', function() {
|
||||||
|
assert.strictEqual(Amount.from_human("0.1 XRP").to_text_full(), '0.1/XRP');
|
||||||
|
});
|
||||||
|
it('0.1 USD', function() {
|
||||||
|
assert.strictEqual(Amount.from_human("0.1 USD").to_text_full(), '0.1/USD/NaN');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('from_json', function() {
|
||||||
|
it('1 XRP', function() {
|
||||||
|
assert.strictEqual(Amount.from_json("1/XRP").to_text_full(), "1/XRP/NaN");
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('from_number', function() {
|
describe('from_number', function() {
|
||||||
it('Number 1', function() {
|
it('Number 1', function() {
|
||||||
assert.strictEqual(Amount.from_number(1).to_text_full(), '1/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
|
assert.strictEqual(Amount.from_number(1).to_text_full(), '1/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
|
||||||
|
|||||||
Reference in New Issue
Block a user