Skip to content

Commit 7a23510

Browse files
committed
fix: correct type
1 parent c8ceb38 commit 7a23510

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import memoizeIntlConstructor from 'intl-format-cache'
2-
import IntlTranslationFormat from 'intl-messageformat'
31
import formatUnit, { FormatUnitOptions, supportedUnits } from '../formatUnit'
42

53
const locales = ['en', 'fr', 'ro']
64

7-
const getTranslationFormat = memoizeIntlConstructor(IntlTranslationFormat)
8-
95
const tests = [
106
...Object.keys(supportedUnits).map(unit => [
117
'should work with',
@@ -57,13 +53,13 @@ describe('formatUnit', () => {
5753
test('should return empty string for unknown unit', () => {
5854
expect(
5955
// @ts-expect-error We test the use case when unit is unknown
60-
formatUnit('fr', 123, { unit: 'unknown' }, getTranslationFormat),
56+
formatUnit('fr', 123, { unit: 'unknown' }),
6157
).toMatchSnapshot()
6258
})
6359

6460
test.each(tests)('%s %o', (_, options, locale, amount) => {
6561
expect(
66-
formatUnit(locale as string, amount as number, options as FormatUnitOptions, getTranslationFormat),
62+
formatUnit(locale as string, amount as number, options as FormatUnitOptions),
6763
).toMatchSnapshot()
6864
})
6965
})

packages/use-i18n/src/formatUnit.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import filesize from 'filesize'
2-
import { Options } from 'intl-messageformat'
32
import formatters from './formatters'
43

54
// We are on base 10, so we should use IEC standard here ...
@@ -16,6 +15,7 @@ const exponents = [
1615
]
1716

1817
type Exponent = typeof exponents[number]
18+
type ExponentName = '' | 'kilo' | 'mega' | 'giga' | 'tera' | 'peta' | 'exa' | 'zetta' | 'yotta'
1919

2020
const frOctet = {
2121
plural: 'octets',
@@ -56,7 +56,6 @@ const compoundUnitsSymbols = {
5656

5757
type Unit = 'bit' | 'byte'
5858
type CompoundUnit = 'second'
59-
type FormatPlural = (message: string, locales?: string | string[] | undefined, overrideFormats?: undefined, opts?: Options | undefined) => { format: ({ amount }: { amount: number}) => string}
6059

6160
const formatShortUnit = (locale: string, exponent: Exponent, unit: Unit, compoundUnit?: CompoundUnit) => {
6261
let shortenedUnit = symbols.short[unit]
@@ -145,7 +144,12 @@ const format =
145144
}`
146145
}
147146

148-
export const supportedUnits = {
147+
type SimpleUnits = `${ExponentName}${Unit}${'-humanized' | ''}`
148+
type ComplexUnits = `${Unit}${'s' | ''}${'-humanized' | ''}`
149+
type PerSecondUnit = `bit${'s' | ''}${'-per-second' | ''}${'-humanized' | ''}`
150+
type SupportedUnits = SimpleUnits | ComplexUnits | PerSecondUnit
151+
152+
export const supportedUnits: Partial<Record<SupportedUnits, ReturnType<typeof format>>> = {
149153
// bits
150154
'bits-humanized': format({ humanize: true, unit: 'bit' }),
151155
'bits-per-second-humanized': format({
@@ -194,7 +198,7 @@ export const supportedUnits = {
194198
}
195199

196200
export interface FormatUnitOptions {
197-
unit: keyof typeof supportedUnits
201+
unit: SupportedUnits
198202
short?: boolean
199203
}
200204

0 commit comments

Comments
 (0)