fix(input-amount): prefer language while parsing
This commit is contained in:
parent
8db04b8bab
commit
68030624f9
2 changed files with 38 additions and 27 deletions
|
|
@ -11,9 +11,10 @@ function isDecimalSeparator(value) {
|
||||||
/**
|
/**
|
||||||
* Determines the best possible parsing mode.
|
* Determines the best possible parsing mode.
|
||||||
*
|
*
|
||||||
* Parsemode depends mostely on the last 4 chars.
|
* - If there is only one separator (withLocale)
|
||||||
* - 1234 => xxx1234 (heuristic)
|
|
||||||
* - 1,23 => xxx1.23 (heuristic)
|
* - 1,23 => xxx1.23 (heuristic)
|
||||||
|
* - else parse mode depends mostly on the last 4 chars
|
||||||
|
* - 1234 => xxx1234 (heuristic)
|
||||||
* - [space]123 => xxx123 (heuristic)
|
* - [space]123 => xxx123 (heuristic)
|
||||||
* - ,123 => unclear
|
* - ,123 => unclear
|
||||||
* - if 1.000,123 (we find a different separator) => 1000.123 (heuristic)
|
* - 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
|
* @param {string} value Clean number (only [0-9 ,.]) to be parsed
|
||||||
* @return {string} unparseable|withLocale|heuristic
|
* @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) {
|
if (value.length > 4) {
|
||||||
const charAtLastSeparatorPosition = value[value.length - 4];
|
const charAtLastSeparatorPosition = value[value.length - 4];
|
||||||
if (isDecimalSeparator(charAtLastSeparatorPosition)) {
|
if (isDecimalSeparator(charAtLastSeparatorPosition)) {
|
||||||
|
|
@ -120,7 +127,7 @@ export function parseAmount(value, options) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const cleanedInput = matchedInput.join('');
|
const cleanedInput = matchedInput.join('');
|
||||||
const parseMode = getParseMode(cleanedInput);
|
const parseMode = getParseMode(cleanedInput, options);
|
||||||
switch (parseMode) {
|
switch (parseMode) {
|
||||||
case 'unparseable':
|
case 'unparseable':
|
||||||
return parseFloat(cleanedInput.match(/[0-9]/g).join(''));
|
return parseFloat(cleanedInput.match(/[0-9]/g).join(''));
|
||||||
|
|
|
||||||
|
|
@ -43,35 +43,35 @@ describe('parseAmount()', () => {
|
||||||
expect(parseAmount('1 234,56789')).to.equal(1234.56789);
|
expect(parseAmount('1 234,56789')).to.equal(1234.56789);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detects separators heuristically when there is only one e.g. 123456,78', () => {
|
it('detects separators heuristically when there is only one and "pasted" mode used e.g. 123456,78', () => {
|
||||||
expect(parseAmount('1.')).to.equal(1);
|
expect(parseAmount('1.', { mode: 'pasted' })).to.equal(1);
|
||||||
expect(parseAmount('1,')).to.equal(1);
|
expect(parseAmount('1,', { mode: 'pasted' })).to.equal(1);
|
||||||
expect(parseAmount('1 ')).to.equal(1);
|
expect(parseAmount('1 ', { mode: 'pasted' })).to.equal(1);
|
||||||
|
|
||||||
expect(parseAmount('1.2')).to.equal(1.2);
|
expect(parseAmount('1.2', { mode: 'pasted' })).to.equal(1.2);
|
||||||
expect(parseAmount('1,2')).to.equal(1.2);
|
expect(parseAmount('1,2', { mode: 'pasted' })).to.equal(1.2);
|
||||||
expect(parseAmount('1 2')).to.equal(12);
|
expect(parseAmount('1 2', { mode: 'pasted' })).to.equal(12);
|
||||||
|
|
||||||
expect(parseAmount('1.23')).to.equal(1.23);
|
expect(parseAmount('1.23', { mode: 'pasted' })).to.equal(1.23);
|
||||||
expect(parseAmount('1,23')).to.equal(1.23);
|
expect(parseAmount('1,23', { mode: 'pasted' })).to.equal(1.23);
|
||||||
expect(parseAmount('1 23')).to.equal(123);
|
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', { mode: 'pasted' })).to.equal(1.2345);
|
||||||
expect(parseAmount('1,2345')).to.equal(1.2345);
|
expect(parseAmount('1,2345', { mode: 'pasted' })).to.equal(1.2345);
|
||||||
expect(parseAmount('1 2345')).to.equal(12345);
|
expect(parseAmount('1 2345', { mode: 'pasted' })).to.equal(12345);
|
||||||
|
|
||||||
expect(parseAmount('1.23456')).to.equal(1.23456);
|
expect(parseAmount('1.23456', { mode: 'pasted' })).to.equal(1.23456);
|
||||||
expect(parseAmount('1,23456')).to.equal(1.23456);
|
expect(parseAmount('1,23456', { mode: 'pasted' })).to.equal(1.23456);
|
||||||
expect(parseAmount('1 23456')).to.equal(123456);
|
expect(parseAmount('1 23456', { mode: 'pasted' })).to.equal(123456);
|
||||||
|
|
||||||
expect(parseAmount('1.234567')).to.equal(1.234567);
|
expect(parseAmount('1.234567', { mode: 'pasted' })).to.equal(1.234567);
|
||||||
expect(parseAmount('1,234567')).to.equal(1.234567);
|
expect(parseAmount('1,234567', { mode: 'pasted' })).to.equal(1.234567);
|
||||||
expect(parseAmount('1 234567')).to.equal(1234567);
|
expect(parseAmount('1 234567', { mode: 'pasted' })).to.equal(1234567);
|
||||||
|
|
||||||
expect(parseAmount('123456,78')).to.equal(123456.78);
|
expect(parseAmount('123456,78', { mode: 'pasted' })).to.equal(123456.78);
|
||||||
expect(parseAmount('123456.78')).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', () => {
|
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);
|
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';
|
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(1.234);
|
||||||
expect(parseAmount('1,234')).to.equal(1234);
|
expect(parseAmount('1,234')).to.equal(1234);
|
||||||
|
|
||||||
localize.locale = 'nl-NL';
|
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(1234);
|
||||||
expect(parseAmount('1,234')).to.equal(1.234);
|
expect(parseAmount('1,234')).to.equal(1.234);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue