Fix toLocaleString for v22.12 (#1965)

* Fix toLocaleString for v22.12

* fix ci

* Update tests, and cleanup solution

* remove ar locale
This commit is contained in:
Tarik Gul
2025-01-02 17:37:40 +02:00
committed by GitHub
parent 2261862097
commit 5d03be1084
3 changed files with 38 additions and 59 deletions
+1 -1
View File
@@ -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
+35 -56
View File
@@ -6,69 +6,48 @@
import { getSeparator } from './getSeparator.js';
describe('getSeparator', (): void => {
it('uses system locale', (): void => {
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('falls back for invalid locale', (): void => {
expect(
getSeparator('invalid-locale')
).toEqual({ decimal: '.', thousand: ',' }); // Default fallback
});
it('matches system locale dynamically', (): void => {
const n = 1000.1;
const locale = {
const expected = {
decimal: n.toLocaleString().substring(5, 6),
thousand: n.toLocaleString().substring(1, 2)
};
expect(
getSeparator()
).toEqual(locale);
expect(getSeparator()).toEqual(expected);
});
it('uses en locale', (): void => {
expect(
getSeparator('en')
).toEqual({
decimal: '.',
thousand: ','
});
});
it('handles locales with unique separators', (): void => {
const result = getSeparator('fr-FR');
it('uses en-gb locale', (): void => {
expect(
getSeparator('en-gb')
).toEqual({
decimal: '.',
thousand: ','
});
});
it('uses sl locale', (): void => {
expect(
getSeparator('sl')
).toEqual({
decimal: ',',
thousand: '.'
});
});
it('uses sl-si locale', (): void => {
expect(
getSeparator('sl-si')
).toEqual({
decimal: ',',
thousand: '.'
});
});
it('uses it-it locale', (): void => {
expect(
getSeparator('it-it')
).toEqual({
decimal: ',',
thousand: '.'
});
});
it('uses ja-jp locale', (): void => {
expect(
getSeparator('ja-jp')
).toEqual({
decimal: '.',
thousand: ','
});
expect(normalizeSeparator(result.thousand)).toBe(' ');
expect(result.decimal).toBe(',');
});
});
+2 -2
View File
@@ -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)
};
}