1
1
import filesize from 'filesize'
2
- import { Options } from 'intl-messageformat'
3
2
import formatters from './formatters'
4
3
5
4
// We are on base 10, so we should use IEC standard here ...
@@ -16,6 +15,7 @@ const exponents = [
16
15
]
17
16
18
17
type Exponent = typeof exponents [ number ]
18
+ type ExponentName = '' | 'kilo' | 'mega' | 'giga' | 'tera' | 'peta' | 'exa' | 'zetta' | 'yotta'
19
19
20
20
const frOctet = {
21
21
plural : 'octets' ,
@@ -56,7 +56,6 @@ const compoundUnitsSymbols = {
56
56
57
57
type Unit = 'bit' | 'byte'
58
58
type CompoundUnit = 'second'
59
- type FormatPlural = ( message : string , locales ?: string | string [ ] | undefined , overrideFormats ?: undefined , opts ?: Options | undefined ) => { format : ( { amount } : { amount : number } ) => string }
60
59
61
60
const formatShortUnit = ( locale : string , exponent : Exponent , unit : Unit , compoundUnit ?: CompoundUnit ) => {
62
61
let shortenedUnit = symbols . short [ unit ]
@@ -145,7 +144,12 @@ const format =
145
144
} `
146
145
}
147
146
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 > > > = {
149
153
// bits
150
154
'bits-humanized' : format ( { humanize : true , unit : 'bit' } ) ,
151
155
'bits-per-second-humanized' : format ( {
@@ -194,7 +198,7 @@ export const supportedUnits = {
194
198
}
195
199
196
200
export interface FormatUnitOptions {
197
- unit : keyof typeof supportedUnits
201
+ unit : SupportedUnits
198
202
short ?: boolean
199
203
}
200
204
0 commit comments