Fixed numeric fields in locales without literals

This commit is contained in:
Minh Nguyễn
2022-05-21 03:25:15 -07:00
parent 5d224a41cd
commit 6419def20f
+11 -8
View File
@@ -442,17 +442,20 @@ export function coreLocalizer() {
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 literal = new RegExp(`[${parts.find(d => d.type === 'literal').value}]`, 'g');
const group = new RegExp(`[${parts.find(d => d.type === 'group').value}]`, 'g');
const decimal = new RegExp(`[${parts.find(d => d.type === 'decimal').value}]`);
const literalPart = parts.find(d => d.type === 'literal');
const literal = literalPart && new RegExp(`[${literalPart.value}]`, 'g');
const groupPart = parts.find(d => d.type === 'group');
const group = groupPart && new RegExp(`[${groupPart.value}]`, 'g');
const decimalPart = parts.find(d => d.type === 'decimal');
const decimal = decimalPart && new RegExp(`[${decimalPart.value}]`);
const numeral = new RegExp(`[${numerals.join('')}]`, 'g');
const getIndex = d => index.get(d);
return (string) => {
string = string.trim()
.replace(literal, '')
.replace(group, '')
.replace(decimal, '.')
.replace(numeral, getIndex);
string = string.trim();
if (literal) string = string.replace(literal, '');
if (group) string = string.replace(group, '');
if (decimal) string = string.replace(decimal, '.');
string = string.replace(numeral, getIndex);
return string ? +string : NaN;
};
};