Skip to content
Open
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
14 changes: 6 additions & 8 deletions packages/ui/src/composables/format-number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LRUCache } from 'lru-cache'

import { injectI18n } from '../providers/i18n'
import { LOCALES } from './i18n.ts'

const formatterCache = new LRUCache<string, Intl.NumberFormat>({ max: 15 })

Expand Down Expand Up @@ -35,16 +36,13 @@ export function useCompactNumber() {
return twoDigitsCompactFormatter.format(value)
}

function formatCompactNumberPlural(value: number | bigint): string {
function formatCompactNumberPlural(value: number | bigint): number | bigint {
if (value < 10_000) {
return value.toString()
return value
}
if (value < 1_000_000) {
const oneDigitCompactFormatter = getCompactFormatter(locale.value, 1)
return oneDigitCompactFormatter.format(value)
}
const twoDigitsCompactFormatter = getCompactFormatter(locale.value, 2)
return twoDigitsCompactFormatter.format(value)
const currentLocale = locale.value
const localeDefinition = LOCALES.find((l) => l.code === currentLocale)
return localeDefinition?.compactNumberPlural ?? NaN
}

return { formatCompactNumber, formatCompactNumberPlural }
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/composables/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface LocaleDefinition {
name: string
translatedName: MessageDescriptor
numeric?: Intl.RelativeTimeFormatNumeric
compactNumberPlural?: number
dir?: 'ltr' | 'rtl'
iso?: string
file?: string
Expand Down Expand Up @@ -93,6 +94,7 @@ export const LOCALES: LocaleDefinition[] = [
code: 'fil-PH',
name: 'Filipino',
translatedName: defineMessage({ id: 'locale.fil-PH', defaultMessage: 'Filipino' }),
compactNumberPlural: 1,
},
{
code: 'fr-FR',
Expand Down