mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
Strip literal (bidi) characters when parsing numbers
This commit is contained in:
@@ -439,15 +439,17 @@ export function coreLocalizer() {
|
||||
if (!('Intl' in window && 'NumberFormat' in Intl)) return polyfill;
|
||||
const format = new Intl.NumberFormat(locale, { maximumFractionDigits: 20 });
|
||||
if (!('formatToParts' in format)) return polyfill;
|
||||
const parts = format.formatToParts(12345.6);
|
||||
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 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);
|
||||
|
||||
@@ -27,6 +27,16 @@ describe('iD.coreLocalizer', function() {
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
it('roundtrips Hebrew numbers', function () {
|
||||
var localizer = iD.coreLocalizer();
|
||||
var formatFloat = localizer.floatFormatter('he');
|
||||
var parseFloat = localizer.floatParser('he');
|
||||
expect(parseFloat(formatFloat(-0.1))).to.eql(-0.1);
|
||||
expect(parseFloat(formatFloat(1.234))).to.eql(1.234);
|
||||
expect(parseFloat(formatFloat(1234))).to.eql(1234);
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
it('roundtrips Arabic numbers', function () {
|
||||
var localizer = iD.coreLocalizer();
|
||||
var formatFloat = localizer.floatFormatter('ar-EG');
|
||||
|
||||
Reference in New Issue
Block a user