Skip to content

Commit 1f40250

Browse files
fix(localize): add tests for percent in formatNumberToParts
1 parent 093cfa0 commit 1f40250

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/localize/src/number/formatNumberToParts.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export function formatNumberToParts(number, options) {
4848
if (!regexCurrency.test(formattedNumber[i]) && !regexMinusSign.test(formattedNumber[i])) {
4949
currency += formattedNumber[i];
5050
}
51-
// push when another character then currency or end of loop
52-
if ((regexCurrency.test(formattedNumber[i]) || formattedNumber.length === i + 1) && currency) {
51+
// push when another character then currency
52+
if (regexCurrency.test(formattedNumber[i]) && currency) {
5353
formattedParts.push({ type: 'currency', value: currency });
5454
currency = '';
5555
}
@@ -99,6 +99,11 @@ export function formatNumberToParts(number, options) {
9999
} else if (i === formattedNumber.length - 1 && numberPart) {
100100
formattedParts.push({ type: 'integer', value: numberPart });
101101
}
102+
// push currency on end of loop
103+
if (i === formattedNumber.length - 1 && currency) {
104+
formattedParts.push({ type: 'currency', value: currency });
105+
currency = '';
106+
}
102107
}
103108
formattedParts = normalizeIntl(formattedParts, options, computedLocale);
104109
return formattedParts;

packages/localize/test/number/formatNumberToParts.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,31 @@ describe('formatNumberToParts', () => {
126126
});
127127
});
128128
});
129+
130+
describe("style: 'percent'", () => {
131+
const specs = [
132+
['en-GB', 1234.5, [i('1'), g(','), i('234'), d('.'), f('50'), c('%')]],
133+
['en-GB', -1234.5, [m, i('1'), g(','), i('234'), d('.'), f('50'), c('%')]],
134+
['nl-NL', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
135+
['nl-NL', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
136+
['nl-BE', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
137+
['nl-BE', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
138+
['fr-FR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
139+
['fr-FR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
140+
['fr-BE', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
141+
['fr-BE', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
142+
];
143+
144+
specs.forEach(([locale, amount, expectedResult]) => {
145+
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
146+
expect(
147+
formatNumberToParts(amount / 100, {
148+
locale,
149+
style: 'percent',
150+
minimumFractionDigits: 2,
151+
}),
152+
).to.deep.equal(expectedResult);
153+
});
154+
});
155+
});
129156
});

0 commit comments

Comments
 (0)