Skip to content

Commit

Permalink
[formatNumber] Fix default style handling and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Jan 9, 2024
1 parent 5d5c82d commit 71f4382
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/svelte-ux/src/lib/utils/number.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest';

import { clamp, formatNumber, formatNumberWithLocale, round } from './number';
import { knownLocales } from './locale';
import { createLocaleSettings, knownLocales } from './locale';

describe('clamp()', () => {
it('no change', () => {
Expand Down Expand Up @@ -61,17 +61,21 @@ describe('formatNumber()', () => {
});

it('returns value as string for style "none"', () => {
const actual = formatNumber(1234.5678, { style: 'none' });
const actual = formatNumber(1234.5678, 'none');
expect(actual).equal('1234.5678');
});

it('formats number with integer default', () => {
const actual = formatNumber(1234.5678, { style: 'integer' });
const actual = formatNumber(1234.5678, 'integer');
expect(actual).equal('1,235');
});

it('formats number with integer fr', () => {
const actual = formatNumber(1234.5678, { style: 'integer', locales: 'fr' });
const actual = formatNumberWithLocale(
createLocaleSettings({ locale: 'fr' }),
1234.5678,
'integer'
);
expect(actual).equal('1 235');
});

Expand Down
4 changes: 3 additions & 1 deletion packages/svelte-ux/src/lib/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export function formatNumberWithLocale(
// Let's always starts with all defaults
...defaults,

style,
...(style !== 'default' && {
style,
}),

// If currency is specified, then style must be currency
...(options.currency != null && {
Expand Down

0 comments on commit 71f4382

Please sign in to comment.