-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
827c185
commit b6c4f92
Showing
4 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './localeNumber.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, expect, it, test } from 'vitest' | ||
import { localeNumber, Locale } from './localeNumber.js' | ||
|
||
describe('localeNumber', () => { | ||
|
||
const numbers = [ | ||
0, 1, 1.1, -1, 0.5, -17.42, 2001, 2777227.19, 9007199254740991, -9007199254740991, | ||
] | ||
|
||
test.each(numbers)('parses numbers in German format; %j', (number) => { | ||
const locale = 'de-DE' | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
test.each(numbers)('parses numbers in Invariant Country format; %j', (number) => { | ||
const locale = 'us-US' as unknown as Locale | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
test.each(numbers)('parses numbers in French format; %j', (number) => { | ||
const locale = 'fr-FR' as unknown as Locale | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
test.each(numbers)('parses numbers in Switzerland format; %j', (number) => { | ||
const locale = 'de-CH' as unknown as Locale | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
test.each(numbers)('parses numbers in Kyrgyzstan format; %j', (number) => { | ||
const locale = 'ky-KG' as unknown as Locale | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
test.each(numbers)('parses numbers in Estonia format; %j', (number) => { | ||
const locale = 'et-EE' as unknown as Locale | ||
const formattedNumber = Intl.NumberFormat(locale).format(number) | ||
expect(localeNumber(locale)(formattedNumber)).toBe(number) | ||
}) | ||
|
||
it.each([ | ||
'1 000 172,3', '1,000,172.30', | ||
])('is undefined when number is formatted in a different locale; %j', (value) => { | ||
expect(localeNumber('de-DE')(value)).toBeUndefined() | ||
}) | ||
|
||
it.each([ | ||
'', 'NaN', 'Infinity', 'null', 'abc', | ||
])('is undefined for non-numbers; %j', (value) => { | ||
expect(localeNumber('de-DE')(value)).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
export type Locale = string // todo: ideally 'de-DE' | 'us-US' | 'fr-FR' | ... | ||
|
||
/** | ||
* Parses number formatted as a local string (must still consist of 0-9 digits) | ||
* using Intl.NumberFormat(locale) to determine decimal and group separators | ||
*/ | ||
export const localeNumber = (locale: Locale) => (number: string): number | undefined => { | ||
if (number.length === 0) return undefined | ||
|
||
const { decimal, group, minusSign } = cachedSeparators(locale) | ||
const parsed = Number( | ||
number.replace(minusSign, '-').replaceAll(group, '').replaceAll(decimal, '.') | ||
) | ||
return Number.isFinite(parsed) ? parsed : undefined | ||
} | ||
|
||
/* | ||
* Example: | ||
* const germanNumber = localeNumber('de-DE') | ||
* const n = germanNumber('-2.777.227,19') // -> -2777227.19 | ||
*/ | ||
|
||
type LocaleSeparators = { locale: Locale, decimal: string, group: string, minusSign: string } | ||
|
||
const localeSeparators = (locale: Locale): LocaleSeparators => { | ||
const parts = new Intl.NumberFormat(locale).formatToParts(-10000.1) | ||
return { | ||
locale, | ||
decimal: parts.find(p => p.type === 'decimal')!.value, | ||
group: parts.find(p => p.type === 'group')!.value, | ||
minusSign: parts.find(p => p.type === 'minusSign')!.value, | ||
} | ||
} | ||
|
||
const cachedSeparators = (locale: Locale): LocaleSeparators => | ||
separators.get(locale) ?? separators.set(locale, localeSeparators(locale)).get(locale)! | ||
|
||
const separators = new Map<Locale, LocaleSeparators>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters