fix(input-amount): prefer language while parsing

This commit is contained in:
Thomas Allmer 2019-11-26 18:00:58 +01:00
parent 8db04b8bab
commit 68030624f9
2 changed files with 38 additions and 27 deletions

View file

@ -11,9 +11,10 @@ function isDecimalSeparator(value) {
/**
* Determines the best possible parsing mode.
*
* Parsemode depends mostely on the last 4 chars.
* - 1234 => xxx1234 (heuristic)
* - If there is only one separator (withLocale)
* - 1,23 => xxx1.23 (heuristic)
* - else parse mode depends mostly on the last 4 chars
* - 1234 => xxx1234 (heuristic)
* - [space]123 => xxx123 (heuristic)
* - ,123 => unclear
* - if 1.000,123 (we find a different separator) => 1000.123 (heuristic)
@ -30,7 +31,13 @@ function isDecimalSeparator(value) {
* @param {string} value Clean number (only [0-9 ,.]) to be parsed
* @return {string} unparseable|withLocale|heuristic
*/
function getParseMode(value) {
function getParseMode(value, { mode = 'auto' } = {}) {
const separators = value.match(/[., ]/g);
if (mode === 'auto' && separators && separators.length === 1) {
return 'withLocale';
}
if (value.length > 4) {
const charAtLastSeparatorPosition = value[value.length - 4];
if (isDecimalSeparator(charAtLastSeparatorPosition)) {
@ -120,7 +127,7 @@ export function parseAmount(value, options) {
return undefined;
}
const cleanedInput = matchedInput.join('');
const parseMode = getParseMode(cleanedInput);
const parseMode = getParseMode(cleanedInput, options);
switch (parseMode) {
case 'unparseable':
return parseFloat(cleanedInput.match(/[0-9]/g).join(''));

View file

@ -43,35 +43,35 @@ describe('parseAmount()', () => {
expect(parseAmount('1 234,56789')).to.equal(1234.56789);
});
it('detects separators heuristically when there is only one e.g. 123456,78', () => {
expect(parseAmount('1.')).to.equal(1);
expect(parseAmount('1,')).to.equal(1);
expect(parseAmount('1 ')).to.equal(1);
it('detects separators heuristically when there is only one and "pasted" mode used e.g. 123456,78', () => {
expect(parseAmount('1.', { mode: 'pasted' })).to.equal(1);
expect(parseAmount('1,', { mode: 'pasted' })).to.equal(1);
expect(parseAmount('1 ', { mode: 'pasted' })).to.equal(1);
expect(parseAmount('1.2')).to.equal(1.2);
expect(parseAmount('1,2')).to.equal(1.2);
expect(parseAmount('1 2')).to.equal(12);
expect(parseAmount('1.2', { mode: 'pasted' })).to.equal(1.2);
expect(parseAmount('1,2', { mode: 'pasted' })).to.equal(1.2);
expect(parseAmount('1 2', { mode: 'pasted' })).to.equal(12);
expect(parseAmount('1.23')).to.equal(1.23);
expect(parseAmount('1,23')).to.equal(1.23);
expect(parseAmount('1 23')).to.equal(123);
expect(parseAmount('1.23', { mode: 'pasted' })).to.equal(1.23);
expect(parseAmount('1,23', { mode: 'pasted' })).to.equal(1.23);
expect(parseAmount('1 23', { mode: 'pasted' })).to.equal(123);
expect(parseAmount('1 234')).to.equal(1234);
expect(parseAmount('1 234', { mode: 'pasted' })).to.equal(1234);
expect(parseAmount('1.2345')).to.equal(1.2345);
expect(parseAmount('1,2345')).to.equal(1.2345);
expect(parseAmount('1 2345')).to.equal(12345);
expect(parseAmount('1.2345', { mode: 'pasted' })).to.equal(1.2345);
expect(parseAmount('1,2345', { mode: 'pasted' })).to.equal(1.2345);
expect(parseAmount('1 2345', { mode: 'pasted' })).to.equal(12345);
expect(parseAmount('1.23456')).to.equal(1.23456);
expect(parseAmount('1,23456')).to.equal(1.23456);
expect(parseAmount('1 23456')).to.equal(123456);
expect(parseAmount('1.23456', { mode: 'pasted' })).to.equal(1.23456);
expect(parseAmount('1,23456', { mode: 'pasted' })).to.equal(1.23456);
expect(parseAmount('1 23456', { mode: 'pasted' })).to.equal(123456);
expect(parseAmount('1.234567')).to.equal(1.234567);
expect(parseAmount('1,234567')).to.equal(1.234567);
expect(parseAmount('1 234567')).to.equal(1234567);
expect(parseAmount('1.234567', { mode: 'pasted' })).to.equal(1.234567);
expect(parseAmount('1,234567', { mode: 'pasted' })).to.equal(1.234567);
expect(parseAmount('1 234567', { mode: 'pasted' })).to.equal(1234567);
expect(parseAmount('123456,78')).to.equal(123456.78);
expect(parseAmount('123456.78')).to.equal(123456.78);
expect(parseAmount('123456,78', { mode: 'pasted' })).to.equal(123456.78);
expect(parseAmount('123456.78', { mode: 'pasted' })).to.equal(123456.78);
});
it('detects separators heuristically when there are 2 same ones e.g. 1.234.56', () => {
@ -89,12 +89,16 @@ describe('parseAmount()', () => {
expect(parseAmount('1,234,56789')).to.equal(1234.56789);
});
it('uses locale if amount can not be interpreted heuristically e.g. 1.234', () => {
it('uses locale to parse amount if there is only one separator e.g. 1.234', () => {
localize.locale = 'en-GB';
expect(parseAmount('12.34')).to.equal(12.34);
expect(parseAmount('12,34')).to.equal(1234);
expect(parseAmount('1.234')).to.equal(1.234);
expect(parseAmount('1,234')).to.equal(1234);
localize.locale = 'nl-NL';
expect(parseAmount('12.34')).to.equal(1234);
expect(parseAmount('12,34')).to.equal(12.34);
expect(parseAmount('1.234')).to.equal(1234);
expect(parseAmount('1,234')).to.equal(1.234);
});