Localize numbers in numeric fields

This commit is contained in:
Minh Nguyễn
2021-10-26 01:08:36 -07:00
parent f818cfdd0d
commit 252828bb4b
6 changed files with 135 additions and 42 deletions
+19
View File
@@ -424,5 +424,24 @@ export function coreLocalizer() {
return code; // if not found, use the code
};
localizer.floatParser = (locale) => {
// https://stackoverflow.com/a/55366435/4585461
const format = new Intl.NumberFormat(locale);
const parts = format.formatToParts(12345.6);
const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
const index = new Map(numerals.map((d, i) => [d, i]));
const group = new RegExp(`[${parts.find(d => d.type === 'group').value}]`, 'g');
const decimal = new RegExp(`[${parts.find(d => d.type === 'decimal').value}]`);
const numeral = new RegExp(`[${numerals.join('')}]`, 'g');
const getIndex = d => index.get(d);
return (string) => {
string = string.trim()
.replace(group, '')
.replace(decimal, '.')
.replace(numeral, getIndex);
return string ? +string : NaN;
};
};
return localizer;
}