Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toLocaleString for v22.12 #1965

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr-any.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.18'
node-version: 'lts/*'
- uses: denoland/setup-deno@v1
with:
# Deno v2 throws errors with specific types therefore we set it to the last version before v2
Expand Down
87 changes: 33 additions & 54 deletions packages/util/src/format/getSeparator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,48 @@
import { getSeparator } from './getSeparator.js';

describe('getSeparator', (): void => {
it('uses system locale', (): void => {
const n = 1000.1;
const locale = {
decimal: n.toLocaleString().substring(5, 6),
thousand: n.toLocaleString().substring(1, 2)
};

expect(
getSeparator()
).toEqual(locale);
});

it('uses en locale', (): void => {
expect(
getSeparator('en')
).toEqual({
decimal: '.',
thousand: ','
const normalizeSeparator = (separator: string) =>
separator === '\u202F' ? ' ' : separator;

const testLocales = [
{ decimal: '.', locale: 'en', thousand: ',' },
{ decimal: '.', locale: 'en-gb', thousand: ',' },
{ decimal: ',', locale: 'sl', thousand: '.' },
{ decimal: ',', locale: 'sl-si', thousand: '.' },
{ decimal: ',', locale: 'it-it', thousand: '.' },
{ decimal: '.', locale: 'ja-jp', thousand: ',' },
{ decimal: '.', locale: 'hi-IN', thousand: ',' },
{ decimal: '.', locale: undefined, thousand: ',' } // Fallback test
];

testLocales.forEach(({ decimal, locale, thousand }) => {
it(`uses ${locale || 'system default'} locale`, (): void => {
expect(
getSeparator(locale)
).toEqual({ decimal, thousand });
});
});

it('uses en-gb locale', (): void => {
it('falls back for invalid locale', (): void => {
expect(
getSeparator('en-gb')
).toEqual({
decimal: '.',
thousand: ','
});
getSeparator('invalid-locale')
).toEqual({ decimal: '.', thousand: ',' }); // Default fallback
});

it('uses sl locale', (): void => {
expect(
getSeparator('sl')
).toEqual({
decimal: ',',
thousand: '.'
});
});
it('matches system locale dynamically', (): void => {
const n = 1000.1;
const expected = {
decimal: n.toLocaleString().substring(5, 6),
thousand: n.toLocaleString().substring(1, 2)
};

it('uses sl-si locale', (): void => {
expect(
getSeparator('sl-si')
).toEqual({
decimal: ',',
thousand: '.'
});
expect(getSeparator()).toEqual(expected);
});

it('uses it-it locale', (): void => {
expect(
getSeparator('it-it')
).toEqual({
decimal: ',',
thousand: '.'
});
});
it('handles locales with unique separators', (): void => {
const result = getSeparator('fr-FR');

it('uses ja-jp locale', (): void => {
expect(
getSeparator('ja-jp')
).toEqual({
decimal: '.',
thousand: ','
});
expect(normalizeSeparator(result.thousand)).toBe(' ');
expect(result.decimal).toBe(',');
});
});
4 changes: 2 additions & 2 deletions packages/util/src/format/getSeparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
export function getSeparator (locale?: string):
{ thousand: string, decimal: string } {
return {
decimal: (0.1).toLocaleString(locale).substring(1, 2),
thousand: (1000).toLocaleString(locale).substring(1, 2)
decimal: (0.1).toLocaleString(locale, { useGrouping: false }).charAt(1),
thousand: (1000).toLocaleString(locale, { useGrouping: true }).replace(/\d/g, '').charAt(0)
};
}
Loading