diff --git a/modules/core/localizer.js b/modules/core/localizer.js index e87bfde21..f840874e1 100644 --- a/modules/core/localizer.js +++ b/modules/core/localizer.js @@ -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); diff --git a/test/spec/core/localizer.js b/test/spec/core/localizer.js index 8f773663f..6a77e0ef8 100644 --- a/test/spec/core/localizer.js +++ b/test/spec/core/localizer.js @@ -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');