Skip to content

Commit 9697775

Browse files
authored
fix(use-i18n): correct formatUnit type (#1128)
* fix(use-i18n): correct formatUnit type * fix: feedback
1 parent ba1af6b commit 9697775

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

packages/use-i18n/src/__tests__/formatUnit.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,42 @@ import formatUnit, { supportedUnits } from '../formatUnit'
33

44
const locales = ['en', 'fr', 'ro']
55

6-
const tests = [
7-
...Object.keys(supportedUnits).map(unit => [
6+
type TestType = [string, FormatUnitOptions, string, number]
7+
8+
const SUPPORTED_UNITS = Object.keys(
9+
supportedUnits,
10+
) as FormatUnitOptions['unit'][]
11+
12+
const tests: TestType[] = [
13+
...SUPPORTED_UNITS.map<TestType>(unit => [
814
'should work with',
915
{ unit },
1016
'fr',
1117
12.56,
1218
]),
13-
...Object.keys(supportedUnits)
14-
.map(unit =>
15-
locales.map(locale => [
16-
`should work with locale ${locale} and`,
17-
{ unit },
18-
locale,
19-
12.56,
20-
]),
21-
)
22-
.flat(),
23-
...Object.keys(supportedUnits)
24-
.map(unit =>
25-
locales.map(locale => [
26-
`should work with long format, locale ${locale} and`,
27-
{ short: false, unit },
28-
locale,
29-
12.56,
30-
]),
31-
)
32-
.flat(),
33-
...Object.keys(supportedUnits).map(unit => [
19+
...SUPPORTED_UNITS.map(unit =>
20+
locales.map<TestType>(locale => [
21+
`should work with locale ${locale} and`,
22+
{ unit },
23+
locale,
24+
12.56,
25+
]),
26+
).flat(),
27+
...SUPPORTED_UNITS.map(unit =>
28+
locales.map<TestType>(locale => [
29+
`should work with long format, locale ${locale} and`,
30+
{ short: false, unit },
31+
locale,
32+
12.56,
33+
]),
34+
).flat(),
35+
...SUPPORTED_UNITS.map<TestType>(unit => [
3436
'should work with maximumFractionDigits',
3537
{ maximumFractionDigits: 3, unit },
3638
'fr',
3739
12.56,
3840
]),
39-
...Object.keys(supportedUnits).map(unit => [
41+
...SUPPORTED_UNITS.map<TestType>(unit => [
4042
'should work with minimumFractionDigits',
4143
{ minimumFractionDigits: 3, unit },
4244
'fr',
@@ -53,12 +55,6 @@ describe('formatUnit', () => {
5355
})
5456

5557
test.each(tests)('%s %o', (_, options, locale, amount) => {
56-
expect(
57-
formatUnit(
58-
locale as string,
59-
amount as number,
60-
options as FormatUnitOptions,
61-
),
62-
).toMatchSnapshot()
58+
expect(formatUnit(locale, amount, options)).toMatchSnapshot()
6359
})
6460
})

packages/use-i18n/src/formatUnit.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ const format =
190190

191191
type SimpleUnits = `${ExponentName}${Unit}${'-humanized' | ''}`
192192
type ComplexUnits = `${Unit}${'s' | ''}${'-humanized' | ''}`
193-
type PerSecondUnit = `bit${'s' | ''}${'-per-second' | ''}${'-humanized' | ''}`
193+
type PerSecondUnit = `${ExponentName | ''}bit${'s' | ''}${'-per-second' | ''}${
194+
| '-humanized'
195+
| ''}`
194196
type SupportedUnits = SimpleUnits | ComplexUnits | PerSecondUnit
195197

196198
export const supportedUnits: Partial<

0 commit comments

Comments
 (0)