Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 69 additions & 65 deletions packages/use-i18n/src/formatUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ const formatShortUnit = (
shortenedUnit = symbols.short.octet
}

return `${exponent.symbol}${shortenedUnit}${compoundUnit ? compoundUnitsSymbols[compoundUnit] : ''
}`
return `${exponent.symbol}${shortenedUnit}${
compoundUnit ? compoundUnitsSymbols[compoundUnit] : ''
}`
}

const formatLongUnit = (
Expand All @@ -99,21 +100,22 @@ const formatLongUnit = (
) {
translation =
localesWhoFavorOctetOverByte[
locale as keyof typeof localesWhoFavorOctetOverByte
locale as keyof typeof localesWhoFavorOctetOverByte
]
}

return `${exponent.name}${formatters
.getTranslationFormat(
`{amount, plural,
return `${exponent.name}${
formatters
.getTranslationFormat(
`{amount, plural,
=0 {${translation.singular}}
=1 {${translation.singular}}
other {${translation.plural}}
}`,
locale,
)
.format({ amount }) as string
}`
locale,
)
.format({ amount }) as string
}`
}

const format =
Expand All @@ -128,71 +130,73 @@ const format =
exponent?: Exponent
humanize?: boolean
}) =>
(
locale: string,
amount: number,
{
maximumFractionDigits,
minimumFractionDigits,
short = true,
base = 10,
}: {
maximumFractionDigits?: number
minimumFractionDigits?: number
short?: boolean
base?: 2 | 10
},
): string => {
let computedExponent = exponent
let computedValue = amount
(
locale: string,
amount: number,
{
maximumFractionDigits,
minimumFractionDigits,
short = true,
base = 10,
}: {
maximumFractionDigits?: number
minimumFractionDigits?: number
short?: boolean
base?: 2 | 10
},
): string => {
let computedExponent = exponent
let computedValue = amount

if (humanize) {
if (computedExponent) {
const value = filesize(amount, {
base,
exponent: exponents.findIndex(
exp => exp.name === (computedExponent as Exponent).name,
),
output: 'object',
round: maximumFractionDigits,
})
if (humanize) {
if (computedExponent) {
const value = filesize(amount, {
base,
exponent: exponents.findIndex(
exp => exp.name === (computedExponent as Exponent).name,
),
output: 'object',
round: maximumFractionDigits,
})

computedValue = Number.parseFloat(value.value.toString())
} else {
const value = filesize(amount, {
base,
output: 'object',
round: maximumFractionDigits,
})
computedValue = Number.parseFloat(value.value.toString())
} else {
const value = filesize(amount, {
base,
output: 'object',
round: maximumFractionDigits,
})

computedExponent = exponents[value.exponent]
computedValue = Number.parseFloat(value.value.toString())
}
computedExponent = exponents[value.exponent]
computedValue = Number.parseFloat(value.value.toString())
}
}

return `${new Intl.NumberFormat(locale, {
maximumFractionDigits,
minimumFractionDigits,
}).format(computedValue)} ${short
return `${new Intl.NumberFormat(locale, {
maximumFractionDigits,
minimumFractionDigits,
}).format(computedValue)} ${
short
? formatShortUnit(
locale,
computedExponent as Exponent,
unit,
compoundUnit,
)
locale,
computedExponent as Exponent,
unit,
compoundUnit,
)
: formatLongUnit(
locale,
computedExponent as Exponent,
unit,
computedValue,
)
}`
}
locale,
computedExponent as Exponent,
unit,
computedValue,
)
}`
}

type SimpleUnits = `${ExponentName}${Unit}${'-humanized' | ''}`
type ComplexUnits = `${Unit}${'s' | ''}${'-humanized' | ''}`
// eslint-disable-next-line @stylistic/space-infix-ops
type PerSecondUnit = `${ExponentName | ''}bit${'s' | ''}${'-per-second' | ''}${| '-humanized' | ''}`

type PerSecondUnit =
`${ExponentName | ''}bit${'s' | ''}${'-per-second' | ''}${'-humanized' | ''}`
type SupportedUnits = SimpleUnits | ComplexUnits | PerSecondUnit

export const supportedUnits: Partial<
Expand Down
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ catalog:
'@babel/preset-env': 7.28.3
'@babel/preset-react': 7.27.1
'@babel/preset-typescript': 7.27.1
'@biomejs/biome': 2.1.2
'@biomejs/biome': 2.2.2
'@changesets/changelog-github': 0.5.1
'@changesets/cli': 2.29.6
'@commitlint/cli': 19.8.1
Expand Down
Loading