mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
[FIX] Handle invalid input in parse_human
This commit is contained in:
@@ -466,25 +466,29 @@ Amount.prototype.parse_human = function(j, opts) {
|
|||||||
|
|
||||||
var words = j.split(' ').filter(function(word) { return word !== ''; });
|
var words = j.split(' ').filter(function(word) { return word !== ''; });
|
||||||
|
|
||||||
|
function isNumber(s) {
|
||||||
|
return isFinite(s) && s !== '';
|
||||||
|
}
|
||||||
|
|
||||||
if (words.length === 1) {
|
if (words.length === 1) {
|
||||||
if (isFinite(words[0])) {
|
if (isNumber(words[0])) {
|
||||||
value = words[0];
|
value = words[0];
|
||||||
currency = 'XRP';
|
currency = 'XRP';
|
||||||
} else {
|
} else {
|
||||||
value = words[0].slice(0, -3);
|
value = words[0].slice(0, -3);
|
||||||
currency = words[0].slice(-3);
|
currency = words[0].slice(-3);
|
||||||
if (!(isFinite(value) && currency.match(currency_RE))) {
|
if (!(isNumber(value) && currency.match(currency_RE))) {
|
||||||
return Amount.NaN();
|
return Amount.NaN();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (words.length === 2) {
|
} else if (words.length === 2) {
|
||||||
if (isFinite(words[0]) && words[1].match(hex_RE)) {
|
if (isNumber(words[0]) && words[1].match(hex_RE)) {
|
||||||
value = words[0];
|
value = words[0];
|
||||||
currency = words[1];
|
currency = words[1];
|
||||||
} else if (words[0].match(currency_RE) && isFinite(words[1])) {
|
} else if (words[0].match(currency_RE) && isNumber(words[1])) {
|
||||||
value = words[1];
|
value = words[1];
|
||||||
currency = words[0];
|
currency = words[0];
|
||||||
} else if (isFinite(words[0]) && words[1].match(currency_RE)) {
|
} else if (isNumber(words[0]) && words[1].match(currency_RE)) {
|
||||||
value = words[0];
|
value = words[0];
|
||||||
currency = words[1];
|
currency = words[1];
|
||||||
} else {
|
} else {
|
||||||
@@ -826,7 +830,7 @@ Amount.prototype.to_human = function(opts) {
|
|||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
if (!this.is_valid()) {
|
if (!this.is_valid()) {
|
||||||
return '';
|
return 'NaN';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
@@ -960,6 +964,9 @@ Amount.prototype.to_json = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Amount.prototype.to_text_full = function(opts) {
|
Amount.prototype.to_text_full = function(opts) {
|
||||||
|
if (!this.is_valid()) {
|
||||||
|
return 'NaN';
|
||||||
|
}
|
||||||
return this._is_native
|
return this._is_native
|
||||||
? this.to_human() + '/XRP'
|
? this.to_human() + '/XRP'
|
||||||
: this.to_text() + '/' + this._currency.to_json() + '/' + this._issuer.to_json(opts);
|
: this.to_text() + '/' + this._currency.to_json() + '/' + this._issuer.to_json(opts);
|
||||||
|
|||||||
@@ -120,6 +120,12 @@ describe('Amount', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('from_human', function() {
|
describe('from_human', function() {
|
||||||
|
it('empty string', function() {
|
||||||
|
assert.strictEqual(Amount.from_human('').to_text_full(), 'NaN');
|
||||||
|
});
|
||||||
|
it('missing value', function() {
|
||||||
|
assert.strictEqual(Amount.from_human('USD').to_text_full(), 'NaN');
|
||||||
|
});
|
||||||
it('1 XRP', function() {
|
it('1 XRP', function() {
|
||||||
assert.strictEqual(Amount.from_human("1 XRP").to_text_full(), '1/XRP');
|
assert.strictEqual(Amount.from_human("1 XRP").to_text_full(), '1/XRP');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user