Merge pull request #310 from ing-bank/fix/formatNumber
Fix/format number
This commit is contained in:
commit
74d9fe5220
2 changed files with 38 additions and 6 deletions
|
|
@ -48,8 +48,8 @@ export function formatNumberToParts(number, options) {
|
||||||
if (!regexCurrency.test(formattedNumber[i]) && !regexMinusSign.test(formattedNumber[i])) {
|
if (!regexCurrency.test(formattedNumber[i]) && !regexMinusSign.test(formattedNumber[i])) {
|
||||||
currency += formattedNumber[i];
|
currency += formattedNumber[i];
|
||||||
}
|
}
|
||||||
// push when another character then currency or end of loop
|
// push when another character then currency
|
||||||
if ((regexCurrency.test(formattedNumber[i]) || formattedNumber.length === i + 1) && currency) {
|
if (regexCurrency.test(formattedNumber[i]) && currency) {
|
||||||
formattedParts.push({ type: 'currency', value: currency });
|
formattedParts.push({ type: 'currency', value: currency });
|
||||||
currency = '';
|
currency = '';
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +61,7 @@ export function formatNumberToParts(number, options) {
|
||||||
formattedParts.push({ type: 'integer', value: numberPart });
|
formattedParts.push({ type: 'integer', value: numberPart });
|
||||||
numberPart = '';
|
numberPart = '';
|
||||||
}
|
}
|
||||||
const decimal = getDecimalSeparator();
|
const decimal = getDecimalSeparator(computedLocale);
|
||||||
if (formattedNumber[i] === decimal) {
|
if (formattedNumber[i] === decimal) {
|
||||||
formattedParts.push({ type: 'decimal', value: formattedNumber[i] });
|
formattedParts.push({ type: 'decimal', value: formattedNumber[i] });
|
||||||
fraction = true;
|
fraction = true;
|
||||||
|
|
@ -71,7 +71,7 @@ export function formatNumberToParts(number, options) {
|
||||||
}
|
}
|
||||||
// detect literals (empty spaces) or space group separator
|
// detect literals (empty spaces) or space group separator
|
||||||
if (regexSpace.test(formattedNumber[i])) {
|
if (regexSpace.test(formattedNumber[i])) {
|
||||||
const group = getGroupSeparator();
|
const group = getGroupSeparator(computedLocale);
|
||||||
const hasNumberPart = !!numberPart;
|
const hasNumberPart = !!numberPart;
|
||||||
// Write number grouping
|
// Write number grouping
|
||||||
if (numberPart && !fraction) {
|
if (numberPart && !fraction) {
|
||||||
|
|
@ -99,6 +99,11 @@ export function formatNumberToParts(number, options) {
|
||||||
} else if (i === formattedNumber.length - 1 && numberPart) {
|
} else if (i === formattedNumber.length - 1 && numberPart) {
|
||||||
formattedParts.push({ type: 'integer', value: 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);
|
formattedParts = normalizeIntl(formattedParts, options, computedLocale);
|
||||||
return formattedParts;
|
return formattedParts;
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,9 @@ describe('formatNumberToParts', () => {
|
||||||
|
|
||||||
specs.forEach(([locale, currency, amount, expectedResult]) => {
|
specs.forEach(([locale, currency, amount, expectedResult]) => {
|
||||||
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||||
localize.locale = locale;
|
|
||||||
expect(
|
expect(
|
||||||
formatNumberToParts(amount, {
|
formatNumberToParts(amount, {
|
||||||
|
locale,
|
||||||
style: 'currency',
|
style: 'currency',
|
||||||
currency,
|
currency,
|
||||||
}),
|
}),
|
||||||
|
|
@ -60,9 +60,9 @@ describe('formatNumberToParts', () => {
|
||||||
|
|
||||||
specs.forEach(([locale, currency, amount, expectedResult]) => {
|
specs.forEach(([locale, currency, amount, expectedResult]) => {
|
||||||
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||||
localize.locale = locale;
|
|
||||||
expect(
|
expect(
|
||||||
formatNumberToParts(amount, {
|
formatNumberToParts(amount, {
|
||||||
|
locale,
|
||||||
style: 'currency',
|
style: 'currency',
|
||||||
currencyDisplay: 'code',
|
currencyDisplay: 'code',
|
||||||
currency,
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue