Skip to content
Snippets Groups Projects
Unverified Commit 5a77051a authored by Wizmaster's avatar Wizmaster Committed by GitHub
Browse files

Make comma's and period interchangeable when entering figures (#2672)

- Only allowed for formats not using period or comma as a thousand separator
parent f8352808
No related branches found
No related tags found
No related merge requests found
// @ts-strict-ignore // @ts-strict-ignore
import { getNumberFormat } from './util'; import { currencyToAmount } from './util';
function fail(state, msg) { function fail(state, msg) {
throw new Error( throw new Error(
...@@ -37,31 +37,16 @@ function parsePrimary(state) { ...@@ -37,31 +37,16 @@ function parsePrimary(state) {
next(state); next(state);
} }
if (/\p{Sc}/u.test(char(state))) {
next(state);
}
// TODO: The regex should probably respect the number format better,
// and we should do more strict parsing
let numberStr = ''; let numberStr = '';
while (char(state) && char(state).match(/[0-9,.]/)) { while (char(state) && char(state).match(/[0-9,. ]|\p{Sc}/u)) {
const thousandsSep = getNumberFormat().separator === ',' ? '.' : ','; numberStr += next(state);
// Don't include the thousands separator
if (char(state) === thousandsSep) {
next(state);
} else {
numberStr += next(state);
}
} }
if (numberStr === '') { if (numberStr === '') {
fail(state, 'Unexpected character'); fail(state, 'Unexpected character');
} }
const number = parseFloat( const number = currencyToAmount(numberStr);
numberStr.replace(getNumberFormat().separator, '.'),
);
return isNegative ? -number : number; return isNegative ? -number : number;
} }
......
...@@ -255,13 +255,14 @@ export function getNumberFormat({ ...@@ -255,13 +255,14 @@ export function getNumberFormat({
format?: NumberFormats; format?: NumberFormats;
hideFraction: boolean; hideFraction: boolean;
} = numberFormatConfig) { } = numberFormatConfig) {
let locale, regex, separator; let locale, regex, separator, separatorRegex;
switch (format) { switch (format) {
case 'space-comma': case 'space-comma':
locale = 'en-SE'; locale = 'en-SE';
regex = /[^-0-9,]/g; regex = /[^-0-9,.]/g;
separator = ','; separator = ',';
separatorRegex = /[,.]/g;
break; break;
case 'dot-comma': case 'dot-comma':
locale = 'de-DE'; locale = 'de-DE';
...@@ -270,8 +271,9 @@ export function getNumberFormat({ ...@@ -270,8 +271,9 @@ export function getNumberFormat({
break; break;
case 'space-dot': case 'space-dot':
locale = 'dje'; locale = 'dje';
regex = /[^-0-9.]/g; regex = /[^-0-9,.]/g;
separator = '.'; separator = '.';
separatorRegex = /[,.]/g;
break; break;
case 'comma-dot-in': case 'comma-dot-in':
locale = 'en-IN'; locale = 'en-IN';
...@@ -293,6 +295,7 @@ export function getNumberFormat({ ...@@ -293,6 +295,7 @@ export function getNumberFormat({
maximumFractionDigits: hideFraction ? 0 : 2, maximumFractionDigits: hideFraction ? 0 : 2,
}), }),
regex, regex,
separatorRegex,
}; };
} }
...@@ -342,11 +345,20 @@ export function amountToCurrencyNoDecimal(n) { ...@@ -342,11 +345,20 @@ export function amountToCurrencyNoDecimal(n) {
} }
export function currencyToAmount(str: string) { export function currencyToAmount(str: string) {
const amount = parseFloat( let amount;
str if (getNumberFormat().separatorRegex) {
.replace(getNumberFormat().regex, '') amount = parseFloat(
.replace(getNumberFormat().separator, '.'), str
); .replace(getNumberFormat().regex, '')
.replace(getNumberFormat().separatorRegex, '.'),
);
} else {
amount = parseFloat(
str
.replace(getNumberFormat().regex, '')
.replace(getNumberFormat().separator, '.'),
);
}
return isNaN(amount) ? null : amount; return isNaN(amount) ? null : amount;
} }
......
---
category: Enhancements
authors: [Wizmaster]
---
Comma and period decimal separator can both be used for number format not using those as thousand separator.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment