Merge pull request #310 from ing-bank/fix/formatNumber

Fix/format number
This commit is contained in:
Erik Kroes 2019-10-09 10:11:14 +02:00 committed by GitHub
commit 74d9fe5220
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 6 deletions

View file

@ -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 = '';
}
@ -61,7 +61,7 @@ export function formatNumberToParts(number, options) {
formattedParts.push({ type: 'integer', value: numberPart });
numberPart = '';
}
const decimal = getDecimalSeparator();
const decimal = getDecimalSeparator(computedLocale);
if (formattedNumber[i] === decimal) {
formattedParts.push({ type: 'decimal', value: formattedNumber[i] });
fraction = true;
@ -71,7 +71,7 @@ export function formatNumberToParts(number, options) {
}
// detect literals (empty spaces) or space group separator
if (regexSpace.test(formattedNumber[i])) {
const group = getGroupSeparator();
const group = getGroupSeparator(computedLocale);
const hasNumberPart = !!numberPart;
// Write number grouping
if (numberPart && !fraction) {
@ -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;

View file

@ -33,9 +33,9 @@ describe('formatNumberToParts', () => {
specs.forEach(([locale, currency, amount, expectedResult]) => {
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
localize.locale = locale;
expect(
formatNumberToParts(amount, {
locale,
style: 'currency',
currency,
}),
@ -60,9 +60,9 @@ describe('formatNumberToParts', () => {
specs.forEach(([locale, currency, amount, expectedResult]) => {
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
localize.locale = locale;
expect(
formatNumberToParts(amount, {
locale,
style: 'currency',
currencyDisplay: 'code',
currency,
@ -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);
});
});
});
});