fix(localize): add tests for percent in formatNumberToParts
This commit is contained in:
parent
093cfa090a
commit
1f4025032f
2 changed files with 34 additions and 2 deletions
|
|
@ -48,8 +48,8 @@ export function formatNumberToParts(number, options) {
|
|||
if (!regexCurrency.test(formattedNumber[i]) && !regexMinusSign.test(formattedNumber[i])) {
|
||||
currency += formattedNumber[i];
|
||||
}
|
||||
// push when another character then currency or end of loop
|
||||
if ((regexCurrency.test(formattedNumber[i]) || formattedNumber.length === i + 1) && currency) {
|
||||
// push when another character then currency
|
||||
if (regexCurrency.test(formattedNumber[i]) && currency) {
|
||||
formattedParts.push({ type: 'currency', value: currency });
|
||||
currency = '';
|
||||
}
|
||||
|
|
@ -99,6 +99,11 @@ export function formatNumberToParts(number, options) {
|
|||
} else if (i === formattedNumber.length - 1 && numberPart) {
|
||||
formattedParts.push({ type: 'integer', value: numberPart });
|
||||
}
|
||||
// push currency on end of loop
|
||||
if (i === formattedNumber.length - 1 && currency) {
|
||||
formattedParts.push({ type: 'currency', value: currency });
|
||||
currency = '';
|
||||
}
|
||||
}
|
||||
formattedParts = normalizeIntl(formattedParts, options, computedLocale);
|
||||
return formattedParts;
|
||||
|
|
|
|||
|
|
@ -126,4 +126,31 @@ describe('formatNumberToParts', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("style: 'percent'", () => {
|
||||
const specs = [
|
||||
['en-GB', 1234.5, [i('1'), g(','), i('234'), d('.'), f('50'), c('%')]],
|
||||
['en-GB', -1234.5, [m, i('1'), g(','), i('234'), d('.'), f('50'), c('%')]],
|
||||
['nl-NL', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
|
||||
['nl-NL', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
|
||||
['nl-BE', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
|
||||
['nl-BE', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), c('%')]],
|
||||
['fr-FR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
|
||||
['fr-FR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
|
||||
['fr-BE', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
|
||||
['fr-BE', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('%')]],
|
||||
];
|
||||
|
||||
specs.forEach(([locale, amount, expectedResult]) => {
|
||||
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||
expect(
|
||||
formatNumberToParts(amount / 100, {
|
||||
locale,
|
||||
style: 'percent',
|
||||
minimumFractionDigits: 2,
|
||||
}),
|
||||
).to.deep.equal(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue