chore(localize): make number formatting tests more scalable
This commit is contained in:
parent
2f352008b1
commit
86c9824f07
5 changed files with 228 additions and 260 deletions
|
|
@ -10,45 +10,36 @@ const currencySymbol = currency => ({ style: 'currency', currencyDisplay: 'symbo
|
||||||
describe('formatNumber', () => {
|
describe('formatNumber', () => {
|
||||||
afterEach(localizeTearDown);
|
afterEach(localizeTearDown);
|
||||||
|
|
||||||
it('displays the appropriate amount of decimal places based on currencies spec http://www.currency-iso.org/en/home/tables/table-a1.html', async () => {
|
it('displays the appropriate amount of decimal places based on currencies spec http://www.currency-iso.org/en/home/tables/table-a1.html', () => {
|
||||||
|
const clean = str => str.replace(/[a-zA-Z]+/g, '').trim();
|
||||||
|
expect(clean(formatNumber(123456.789, currencyCode('JPY')))).to.equal('123,457');
|
||||||
|
expect(clean(formatNumber(123456.789, currencyCode('EUR')))).to.equal('123,456.79');
|
||||||
|
expect(clean(formatNumber(123456.789, currencyCode('BHD')))).to.equal('123,456.789');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can display currency as code', () => {
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can display currency as symbol', () => {
|
||||||
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
||||||
localize.locale = 'nl-NL';
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
|
|
||||||
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
|
|
||||||
localize.locale = 'fr-FR';
|
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
|
|
||||||
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
|
|
||||||
localize.locale = 'de-DE';
|
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
|
|
||||||
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can display currency as code', async () => {
|
it('uses minus (and not dash) to indicate negative numbers ', () => {
|
||||||
localize.locale = 'nl-NL';
|
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
|
|
||||||
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can display currency as symbol', async () => {
|
|
||||||
localize.locale = 'nl-NL';
|
|
||||||
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
|
|
||||||
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('uses minus (and not dash) to indicate negative numbers ', async () => {
|
|
||||||
expect(formatNumber(-12, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-12');
|
expect(formatNumber(-12, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-12');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rounds (negative) numbers e.g. `roundMode: round`', async () => {
|
it('rounds (negative) numbers e.g. `roundMode: round`', () => {
|
||||||
expect(formatNumber(12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('12');
|
expect(formatNumber(12.4, { roundMode: 'round' })).to.equal('12');
|
||||||
expect(formatNumber(12.6, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('13');
|
expect(formatNumber(12.6, { roundMode: 'round' })).to.equal('13');
|
||||||
|
|
||||||
expect(formatNumber(-12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-12');
|
expect(formatNumber(-12.4, { roundMode: 'round' })).to.equal('-12');
|
||||||
expect(formatNumber(-12.6, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-13');
|
expect(formatNumber(-12.6, { roundMode: 'round' })).to.equal('-13');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rounds (negative) numbers up when `roundMode: 'ceiling'`", async () => {
|
it("rounds (negative) numbers up when `roundMode: 'ceiling'`", () => {
|
||||||
expect(formatNumber(12.4, { roundMode: 'ceiling' })).to.equal('13');
|
expect(formatNumber(12.4, { roundMode: 'ceiling' })).to.equal('13');
|
||||||
expect(formatNumber(12.6, { roundMode: 'ceiling' })).to.equal('13');
|
expect(formatNumber(12.6, { roundMode: 'ceiling' })).to.equal('13');
|
||||||
|
|
||||||
|
|
@ -56,7 +47,7 @@ describe('formatNumber', () => {
|
||||||
expect(formatNumber(-12.6, { roundMode: 'ceiling' })).to.equal('-12');
|
expect(formatNumber(-12.6, { roundMode: 'ceiling' })).to.equal('-12');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rounds (negative) numbers down when `roundMode: floor`', async () => {
|
it('rounds (negative) numbers down when `roundMode: floor`', () => {
|
||||||
expect(formatNumber(12.4, { roundMode: 'floor' })).to.equal('12');
|
expect(formatNumber(12.4, { roundMode: 'floor' })).to.equal('12');
|
||||||
expect(formatNumber(12.6, { roundMode: 'floor' })).to.equal('12');
|
expect(formatNumber(12.6, { roundMode: 'floor' })).to.equal('12');
|
||||||
|
|
||||||
|
|
@ -64,24 +55,28 @@ describe('formatNumber', () => {
|
||||||
expect(formatNumber(-12.6, { roundMode: 'floor' })).to.equal('-13');
|
expect(formatNumber(-12.6, { roundMode: 'floor' })).to.equal('-13');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns empty string when NaN', async () => {
|
it('returns empty string when NaN', () => {
|
||||||
expect(formatNumber('foo')).to.equal('');
|
expect(formatNumber('foo')).to.equal('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns empty string when number is undefined', async () => {
|
it('returns empty string when number is undefined', () => {
|
||||||
expect(formatNumber(undefined)).to.equal('');
|
expect(formatNumber(undefined)).to.equal('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uses `localize.formatNumberOptions.returnIfNaN`', async () => {
|
it('uses `localize.formatNumberOptions.returnIfNaN`', () => {
|
||||||
|
const savedReturnIfNaN = localize.formatNumberOptions.returnIfNaN;
|
||||||
|
|
||||||
localize.formatNumberOptions.returnIfNaN = '-';
|
localize.formatNumberOptions.returnIfNaN = '-';
|
||||||
expect(formatNumber('foo')).to.equal('-');
|
expect(formatNumber('foo')).to.equal('-');
|
||||||
|
|
||||||
|
localize.formatNumberOptions.returnIfNaN = savedReturnIfNaN;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can set what to returns when NaN via `returnIfNaN: 'foo'`", async () => {
|
it("can set what to returns when NaN via `returnIfNaN: 'foo'`", () => {
|
||||||
expect(formatNumber('foo', { returnIfNaN: '-' })).to.equal('-');
|
expect(formatNumber('foo', { returnIfNaN: '-' })).to.equal('-');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uses `localize.locale`', async () => {
|
it('uses `localize.locale`', () => {
|
||||||
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 2 })).to.equal(
|
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 2 })).to.equal(
|
||||||
'123,456.79',
|
'123,456.79',
|
||||||
);
|
);
|
||||||
|
|
@ -91,7 +86,7 @@ describe('formatNumber', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can set locale to use', async () => {
|
it('can set locale to use', () => {
|
||||||
expect(
|
expect(
|
||||||
formatNumber(123456.789, { locale: 'en-GB', style: 'decimal', maximumFractionDigits: 2 }),
|
formatNumber(123456.789, { locale: 'en-GB', style: 'decimal', maximumFractionDigits: 2 }),
|
||||||
).to.equal('123,456.79');
|
).to.equal('123,456.79');
|
||||||
|
|
@ -100,7 +95,7 @@ describe('formatNumber', () => {
|
||||||
).to.equal('123.456,79');
|
).to.equal('123.456,79');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can specify max decimal places by `maximumFractionDigits: 3`', async () => {
|
it('can specify max decimal places by `maximumFractionDigits: 3`', () => {
|
||||||
expect(formatNumber(123456.789)).to.equal('123,456.789');
|
expect(formatNumber(123456.789)).to.equal('123,456.789');
|
||||||
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 3 })).to.equal(
|
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 3 })).to.equal(
|
||||||
'123,456.789',
|
'123,456.789',
|
||||||
|
|
@ -110,7 +105,7 @@ describe('formatNumber', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can specify min decimal places by `minimumFractionDigits: 3`', async () => {
|
it('can specify min decimal places by `minimumFractionDigits: 3`', () => {
|
||||||
expect(formatNumber(12.3)).to.equal('12.3');
|
expect(formatNumber(12.3)).to.equal('12.3');
|
||||||
expect(formatNumber(12.3456, { style: 'decimal', minimumFractionDigits: 3 })).to.equal(
|
expect(formatNumber(12.3456, { style: 'decimal', minimumFractionDigits: 3 })).to.equal(
|
||||||
'12.346',
|
'12.346',
|
||||||
|
|
@ -118,16 +113,16 @@ describe('formatNumber', () => {
|
||||||
expect(formatNumber(12.3, { style: 'decimal', minimumFractionDigits: 3 })).to.equal('12.300');
|
expect(formatNumber(12.3, { style: 'decimal', minimumFractionDigits: 3 })).to.equal('12.300');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can specify to show at least x digits by `minimumIntegerDigits: 5`', async () => {
|
it('can specify to show at least x digits by `minimumIntegerDigits: 5`', () => {
|
||||||
expect(formatNumber(123)).to.equal('123');
|
expect(formatNumber(123)).to.equal('123');
|
||||||
expect(formatNumber(123, { minimumIntegerDigits: 5 })).to.equal('00,123');
|
expect(formatNumber(123, { minimumIntegerDigits: 5 })).to.equal('00,123');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can display 0 decimal places', async () => {
|
it('can display 0 decimal places', () => {
|
||||||
expect(formatNumber(12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('12');
|
expect(formatNumber(12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('12');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('formats numbers correctly', async () => {
|
it('formats numbers correctly', () => {
|
||||||
localize.locale = 'nl-NL';
|
localize.locale = 'nl-NL';
|
||||||
expect(formatNumber(0, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,00');
|
expect(formatNumber(0, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,00');
|
||||||
expect(formatNumber(0.1, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,10');
|
expect(formatNumber(0.1, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,10');
|
||||||
|
|
@ -172,7 +167,7 @@ describe('formatNumber', () => {
|
||||||
).to.equal('112.345.678,00');
|
).to.equal('112.345.678,00');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('formats 2-digit decimals correctly', async () => {
|
it('formats 2-digit decimals correctly', () => {
|
||||||
localize.locale = 'nl-NL';
|
localize.locale = 'nl-NL';
|
||||||
Array.from(new Array(100), (val, index) => index).forEach(i => {
|
Array.from(new Array(100), (val, index) => index).forEach(i => {
|
||||||
const iString = `${i}`;
|
const iString = `${i}`;
|
||||||
|
|
@ -185,32 +180,118 @@ describe('formatNumber', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('normalization', () => {
|
describe('normalization', () => {
|
||||||
it('supports British locale', async () => {
|
describe('en-GB', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'en-GB';
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
||||||
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
|
||||||
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
||||||
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports US locale', async () => {
|
describe('en-US', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
localize.locale = 'en-US';
|
localize.locale = 'en-US';
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
||||||
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
|
||||||
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
||||||
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports Bulgarian locale', async () => {
|
describe('en-AU', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'en-AU';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
|
||||||
|
// expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79'); // TODO: fix
|
||||||
|
// expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79'); // TODO: fix
|
||||||
|
// expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457'); // TODO: fix
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('en-PH', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'en-PH';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('nl-NL', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'nl-NL';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123.457 JPY');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('nl-BE', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'nl-BE';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123.457 JPY');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('fr-FR', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'fr-FR';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('fr-BE', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
|
localize.locale = 'fr-FR';
|
||||||
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
|
||||||
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $');
|
||||||
|
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('bg-BG', () => {
|
||||||
|
it('supports basics', () => {
|
||||||
localize.locale = 'bg-BG';
|
localize.locale = 'bg-BG';
|
||||||
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
|
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
|
||||||
expect(formatNumber(1234567890.789, currencyCode('USD'))).to.equal('1 234 567 890,79 USD');
|
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
|
||||||
expect(formatNumber(12.789, currencyCode('EUR'))).to.equal('12,79 EUR');
|
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
|
||||||
expect(formatNumber(12, currencyCode('USD'))).to.equal('12,00 USD');
|
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
|
||||||
expect(formatNumber(12.789, { style: 'decimal' })).to.equal('12,789');
|
// expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $'); // TODO: fix
|
||||||
expect(formatNumber(12, { style: 'decimal', minimumFractionDigits: 3 })).to.equal('12,000');
|
// expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥'); // TODO: fix
|
||||||
expect(formatNumber(20000, { style: 'decimal', minimumFractionDigits: 3 })).to.equal(
|
});
|
||||||
'20 000,000',
|
|
||||||
);
|
it('normalizes group separator', () => {
|
||||||
|
localize.locale = 'bg-BG';
|
||||||
|
expect(formatNumber(1.234, currencyCode('EUR'))).to.equal('1,23 EUR');
|
||||||
|
expect(formatNumber(1234.567, currencyCode('EUR'))).to.equal('1 234,57 EUR');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -4,212 +4,99 @@ import { localizeTearDown } from '../../test-helpers.js';
|
||||||
|
|
||||||
import { formatNumberToParts } from '../../src/number/formatNumberToParts.js';
|
import { formatNumberToParts } from '../../src/number/formatNumberToParts.js';
|
||||||
|
|
||||||
|
const c = v => ({ type: 'currency', value: v });
|
||||||
|
const d = v => ({ type: 'decimal', value: v });
|
||||||
|
const i = v => ({ type: 'integer', value: v });
|
||||||
|
const f = v => ({ type: 'fraction', value: v });
|
||||||
|
const g = v => ({ type: 'group', value: v });
|
||||||
|
const l = v => ({ type: 'literal', value: v });
|
||||||
|
const m = { type: 'minusSign', value: '-' };
|
||||||
|
|
||||||
|
const stringifyParts = parts => parts.map(part => part.value).join('');
|
||||||
|
|
||||||
describe('formatNumberToParts', () => {
|
describe('formatNumberToParts', () => {
|
||||||
afterEach(localizeTearDown);
|
afterEach(localizeTearDown);
|
||||||
|
|
||||||
describe('formats based on ISO standards', () => {
|
describe("style: 'currency'", () => {
|
||||||
const specs = [
|
const specs = [
|
||||||
['nl-NL', 'EUR', 1234.5, '1.234,50 EUR'],
|
['en-GB', 'EUR', 1234.5, [c('EUR'), l(' '), i('1'), g(','), i('234'), d('.'), f('50')]],
|
||||||
['nl-NL', 'USD', 1234.5, '1.234,50 USD'],
|
['en-GB', 'EUR', -1234.5, [m, c('EUR'), l(' '), i('1'), g(','), i('234'), d('.'), f('50')]],
|
||||||
['nl-NL', 'EUR', -1234.5, '-1.234,50 EUR'],
|
['nl-NL', 'EUR', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['nl-BE', 'EUR', 1234.5, '1.234,50 EUR'],
|
['nl-NL', 'EUR', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['nl-BE', 'USD', 1234.5, '1.234,50 USD'],
|
['nl-BE', 'EUR', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['nl-BE', 'EUR', -1234.5, '-1.234,50 EUR'],
|
['nl-BE', 'EUR', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['en-GB', 'EUR', 1234.5, 'EUR 1,234.50'],
|
['fr-FR', 'EUR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['en-GB', 'USD', 1234.5, 'USD 1,234.50'],
|
['fr-FR', 'EUR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['en-GB', 'EUR', -1234.5, '-EUR 1,234.50'],
|
['fr-BE', 'EUR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['de-DE', 'EUR', 1234.5, '1.234,50 EUR'],
|
['fr-BE', 'EUR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
|
||||||
['de-DE', 'USD', 1234.5, '1.234,50 USD'],
|
|
||||||
['de-DE', 'EUR', -1234.5, '-1.234,50 EUR'],
|
|
||||||
['fr-BE', 'EUR', 1234.5, '1 234,50 EUR'],
|
|
||||||
['fr-BE', 'USD', 1234.5, '1 234,50 USD'],
|
|
||||||
['fr-BE', 'EUR', -1234.5, '-1 234,50 EUR'],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
specs.forEach(spec => {
|
specs.forEach(([locale, currency, amount, expectedResult]) => {
|
||||||
const [locale, currency, amount, expectedResult] = spec;
|
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||||
|
|
||||||
it(`formats ${locale} ${currency} ${amount} as ${expectedResult}`, () => {
|
|
||||||
localize.locale = locale;
|
localize.locale = locale;
|
||||||
const parts = formatNumberToParts(amount, {
|
expect(
|
||||||
|
formatNumberToParts(amount, {
|
||||||
style: 'currency',
|
style: 'currency',
|
||||||
|
currencyDisplay: 'code',
|
||||||
currency,
|
currency,
|
||||||
currencyDisplay: 'code',
|
}),
|
||||||
});
|
).to.deep.equal(expectedResult);
|
||||||
const joinedParts = parts.map(p => p.value).join('');
|
|
||||||
expect(joinedParts).to.equal(expectedResult);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports currency symbol with dutch locale', async () => {
|
describe("style: 'decimal'", () => {
|
||||||
localize.locale = 'nl-NL';
|
describe('no minimumFractionDigits', () => {
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
const specs = [
|
||||||
style: 'currency',
|
['en-GB', 3500, [i('3'), g(','), i('500')]],
|
||||||
currency: 'EUR',
|
['en-GB', -3500, [m, i('3'), g(','), i('500')]],
|
||||||
currencyDisplay: 'symbol',
|
['nl-NL', 3500, [i('3'), g('.'), i('500')]],
|
||||||
|
['nl-NL', -3500, [m, i('3'), g('.'), i('500')]],
|
||||||
|
['nl-BE', 3500, [i('3'), g('.'), i('500')]],
|
||||||
|
['nl-BE', -3500, [m, i('3'), g('.'), i('500')]],
|
||||||
|
['fr-FR', 3500, [i('3'), g(' '), i('500')]],
|
||||||
|
['fr-FR', -3500, [m, i('3'), g(' '), i('500')]],
|
||||||
|
['fr-BE', 3500, [i('3'), g(' '), i('500')]],
|
||||||
|
['fr-BE', -3500, [m, i('3'), g(' '), i('500')]],
|
||||||
|
];
|
||||||
|
|
||||||
|
specs.forEach(([locale, amount, expectedResult]) => {
|
||||||
|
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||||
|
localize.locale = locale;
|
||||||
|
expect(
|
||||||
|
formatNumberToParts(amount, {
|
||||||
|
style: 'decimal',
|
||||||
|
}),
|
||||||
|
).to.deep.equal(expectedResult);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
expect(formattedToParts).to.eql([
|
|
||||||
{ type: 'integer', value: '3' },
|
|
||||||
{ type: 'group', value: '.' },
|
|
||||||
{ type: 'integer', value: '500' },
|
|
||||||
{ type: 'decimal', value: ',' },
|
|
||||||
{ type: 'fraction', value: '00' },
|
|
||||||
{ type: 'literal', value: ' ' },
|
|
||||||
{ type: 'currency', value: '€' },
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports currency symbol with french locale', async () => {
|
describe('minimumFractionDigits: 2', () => {
|
||||||
localize.locale = 'fr-FR';
|
const specs = [
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
['en-GB', 3500, [i('3'), g(','), i('500'), d('.'), f('00')]],
|
||||||
style: 'currency',
|
['en-GB', -3500, [m, i('3'), g(','), i('500'), d('.'), f('00')]],
|
||||||
currency: 'EUR',
|
['nl-NL', 3500, [i('3'), g('.'), i('500'), d(','), f('00')]],
|
||||||
currencyDisplay: 'symbol',
|
['nl-NL', -3500, [m, i('3'), g('.'), i('500'), d(','), f('00')]],
|
||||||
});
|
['nl-BE', 3500, [i('3'), g('.'), i('500'), d(','), f('00')]],
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(7);
|
['nl-BE', -3500, [m, i('3'), g('.'), i('500'), d(','), f('00')]],
|
||||||
expect(formattedToParts[0].type).to.equal('integer');
|
['fr-FR', 3500, [i('3'), g(' '), i('500'), d(','), f('00')]],
|
||||||
expect(formattedToParts[0].value).to.equal('3');
|
['fr-FR', -3500, [m, i('3'), g(' '), i('500'), d(','), f('00')]],
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
['fr-BE', 3500, [i('3'), g(' '), i('500'), d(','), f('00')]],
|
||||||
expect(formattedToParts[1].value).to.equal(' ');
|
['fr-BE', -3500, [m, i('3'), g(' '), i('500'), d(','), f('00')]],
|
||||||
expect(formattedToParts[5].type).to.equal('literal');
|
];
|
||||||
expect(formattedToParts[5].value).to.equal(' ');
|
|
||||||
expect(formattedToParts[6].type).to.equal('currency');
|
|
||||||
expect(formattedToParts[6].value).to.equal('€');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency symbol with British locale', async () => {
|
specs.forEach(([locale, amount, expectedResult]) => {
|
||||||
localize.locale = 'en-GB';
|
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
localize.locale = locale;
|
||||||
style: 'currency',
|
expect(
|
||||||
currency: 'EUR',
|
formatNumberToParts(amount, {
|
||||||
currencyDisplay: 'symbol',
|
|
||||||
});
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(6);
|
|
||||||
expect(formattedToParts[2].type).to.equal('group');
|
|
||||||
expect(formattedToParts[2].value).to.equal(',');
|
|
||||||
expect(formattedToParts[4].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[4].value).to.equal('.');
|
|
||||||
expect(formattedToParts[5].type).to.equal('fraction');
|
|
||||||
expect(formattedToParts[5].value).to.equal('00');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency code with dutch locale', async () => {
|
|
||||||
localize.locale = 'nl-NL';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'currency',
|
|
||||||
currency: 'EUR',
|
|
||||||
currencyDisplay: 'code',
|
|
||||||
});
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(7);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal('.');
|
|
||||||
expect(formattedToParts[3].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[3].value).to.equal(',');
|
|
||||||
expect(formattedToParts[5].type).to.equal('literal');
|
|
||||||
expect(formattedToParts[5].value).to.equal(' ');
|
|
||||||
expect(formattedToParts[6].type).to.equal('currency');
|
|
||||||
expect(formattedToParts[6].value).to.equal('EUR');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency code with french locale', async () => {
|
|
||||||
localize.locale = 'fr-FR';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'currency',
|
|
||||||
currency: 'EUR',
|
|
||||||
currencyDisplay: 'code',
|
|
||||||
});
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(7);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal(' ');
|
|
||||||
expect(formattedToParts[3].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[3].value).to.equal(',');
|
|
||||||
expect(formattedToParts[4].type).to.equal('fraction');
|
|
||||||
expect(formattedToParts[4].value).to.equal('00');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency code with British locale', async () => {
|
|
||||||
localize.locale = 'en-GB';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'currency',
|
|
||||||
currency: 'EUR',
|
|
||||||
currencyDisplay: 'code',
|
|
||||||
});
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(7);
|
|
||||||
expect(formattedToParts[3].type).to.equal('group');
|
|
||||||
expect(formattedToParts[3].value).to.equal(',');
|
|
||||||
expect(formattedToParts[5].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[5].value).to.equal('.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency with dutch locale and 2 decimals', async () => {
|
|
||||||
localize.locale = 'nl-NL';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'decimal',
|
style: 'decimal',
|
||||||
minimumFractionDigits: 2,
|
minimumFractionDigits: 2,
|
||||||
|
}),
|
||||||
|
).to.deep.equal(expectedResult);
|
||||||
});
|
});
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(5);
|
|
||||||
expect(formattedToParts[0].type).to.equal('integer');
|
|
||||||
expect(formattedToParts[0].value).to.equal('3');
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal('.');
|
|
||||||
expect(formattedToParts[2].type).to.equal('integer');
|
|
||||||
expect(formattedToParts[2].value).to.equal('500');
|
|
||||||
expect(formattedToParts[3].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[3].value).to.equal(',');
|
|
||||||
expect(formattedToParts[4].type).to.equal('fraction');
|
|
||||||
expect(formattedToParts[4].value).to.equal('00');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports currency with french locale and 2 decimals', async () => {
|
|
||||||
localize.locale = 'fr-FR';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'decimal',
|
|
||||||
minimumFractionDigits: 2,
|
|
||||||
});
|
});
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(5);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal(' ');
|
|
||||||
expect(formattedToParts[3].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[3].value).to.equal(',');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency with british locale and 2 decimals', async () => {
|
|
||||||
localize.locale = 'en-GB';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, {
|
|
||||||
style: 'decimal',
|
|
||||||
minimumFractionDigits: 2,
|
|
||||||
});
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(5);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal(',');
|
|
||||||
expect(formattedToParts[3].type).to.equal('decimal');
|
|
||||||
expect(formattedToParts[3].value).to.equal('.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency with dutch locale without decimals', async () => {
|
|
||||||
localize.locale = 'nl-NL';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(3);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal('.');
|
|
||||||
expect(formattedToParts[2].type).to.equal('integer');
|
|
||||||
expect(formattedToParts[2].value).to.equal('500');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency with french locale without decimals', async () => {
|
|
||||||
localize.locale = 'fr-FR';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(3);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal(' ');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports currency with british locale without decimals', async () => {
|
|
||||||
localize.locale = 'en-GB';
|
|
||||||
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
|
|
||||||
expect(Object.keys(formattedToParts).length).to.equal(3);
|
|
||||||
expect(formattedToParts[1].type).to.equal('group');
|
|
||||||
expect(formattedToParts[1].value).to.equal(',');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
|
||||||
import { getDecimalSeparator } from '../../src/number/getDecimalSeparator.js';
|
import { getDecimalSeparator } from '../../src/number/getDecimalSeparator.js';
|
||||||
|
|
||||||
describe('getDecimalSeparator', () => {
|
describe('getDecimalSeparator', () => {
|
||||||
it('returns decimal separator for locale', async () => {
|
it('returns decimal separator for locale', () => {
|
||||||
expect(getDecimalSeparator('en-GB')).to.equal('.');
|
expect(getDecimalSeparator('en-GB')).to.equal('.');
|
||||||
expect(getDecimalSeparator('nl-NL')).to.equal(',');
|
expect(getDecimalSeparator('nl-NL')).to.equal(',');
|
||||||
expect(getDecimalSeparator('fr-FR')).to.equal(',');
|
expect(getDecimalSeparator('fr-FR')).to.equal(',');
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
|
||||||
import { getFractionDigits } from '../../src/number/getFractionDigits.js';
|
import { getFractionDigits } from '../../src/number/getFractionDigits.js';
|
||||||
|
|
||||||
describe('getFractionDigits', () => {
|
describe('getFractionDigits', () => {
|
||||||
it('returns number of fraction digits for currency', async () => {
|
it('returns number of fraction digits for currency', () => {
|
||||||
expect(getFractionDigits('JPY')).to.equal(0);
|
expect(getFractionDigits('JPY')).to.equal(0);
|
||||||
expect(getFractionDigits('EUR')).to.equal(2);
|
expect(getFractionDigits('EUR')).to.equal(2);
|
||||||
expect(getFractionDigits('BHD')).to.equal(3);
|
expect(getFractionDigits('BHD')).to.equal(3);
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
|
||||||
import { getGroupSeparator } from '../../src/number/getGroupSeparator.js';
|
import { getGroupSeparator } from '../../src/number/getGroupSeparator.js';
|
||||||
|
|
||||||
describe('getGroupSeparator', () => {
|
describe('getGroupSeparator', () => {
|
||||||
it('returns group separator for locale', async () => {
|
it('returns group separator for locale', () => {
|
||||||
expect(getGroupSeparator('en-GB')).to.equal(',');
|
expect(getGroupSeparator('en-GB')).to.equal(',');
|
||||||
expect(getGroupSeparator('nl-NL')).to.equal('.');
|
expect(getGroupSeparator('nl-NL')).to.equal('.');
|
||||||
expect(getGroupSeparator('fr-FR')).to.equal(' ');
|
expect(getGroupSeparator('fr-FR')).to.equal(' ');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue