Skip to content

Commit d462ea4

Browse files
author
Michael Jordan
committed
fix(#4967): fix logic for filtering group symbol matches within value
Addresses #6520 (comment)
1 parent 28e8b10 commit d462ea4

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/@internationalized/number/src/NumberParser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,9 @@ class NumberParserImpl {
280280
indexOfLastNumeral = abs.length - 1;
281281

282282
let decimalPartMatches = abs.match(DECIMAL_PART_REGEX);
283-
let groupSymbolMatch:Array<string> | undefined = abs.match(GROUPING_SYMBOLS_REGEX)?.filter((s: string, i: number) => i >= indexOfFirstNumeral - 1);
283+
let groupSymbolMatch:Array<string> | undefined = abs.match(GROUPING_SYMBOLS_REGEX)?.filter((s: string) => abs.indexOf(s) >= indexOfFirstNumeral - 1);
284284
if (decimalPartMatches?.groups?.symbol && groupSymbolMatch?.[groupSymbolMatch.length - 1] === decimalPartMatches.groups?.symbol) {
285-
groupSymbolMatch = groupSymbolMatch.slice(0, -1);
286-
if (groupSymbolMatch.length === 0) {
285+
if (groupSymbolMatch.length === 1) {
287286
groupSymbolMatch = undefined;
288287
} else {
289288
abs = replaceAll(abs, groupSymbolMatch[0], '');

packages/@internationalized/number/test/NumberParser.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,28 @@ describe('NumberParser', function () {
316316
expect(roundTrip).toBe(formattedOnce);
317317
expect(parsed).toBe(altParsed);
318318
});
319+
it(`decimal with
320+
minimumFractionDigits: 0,
321+
maximumSignificantDigits: 1`, () => {
322+
let options = {
323+
style: 'decimal',
324+
minimumFractionDigits: 0,
325+
maximumSignificantDigits: 1
326+
};
327+
let locale = 'ar-AE';
328+
const formatter = new Intl.NumberFormat(locale, options);
329+
const parser = new NumberParser(locale, options);
330+
const altParser = new NumberParser('en-US', options);
331+
let adjustedNumberForFractions = -950000;
332+
const formattedOnce = formatter.format(adjustedNumberForFractions);
333+
const parsed = parser.parse(formattedOnce);
334+
const roundTrip = formatter.format(parsed);
335+
const altParsed = altParser.parse(formattedOnce);
336+
console.log({locale, formattedOnce, parsed, roundTrip, altParsed});
337+
338+
expect(roundTrip).toBe(formattedOnce);
339+
expect(parsed).toBe(altParsed);
340+
});
319341
});
320342
});
321343

0 commit comments

Comments
 (0)