elk/composables/numbers.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-12-02 03:18:36 +01:00
import type { MaybeRef } from '@vueuse/shared'
const formatter = Intl.NumberFormat()
2022-12-02 03:18:36 +01:00
export const humanReadableNumber = (
num: number,
{ k, m }: { k: string; m: string } = { k: 'K', m: 'M' },
useFormatter: Intl.NumberFormat = formatter,
) => {
if (num < 10000)
2022-12-02 03:18:36 +01:00
return useFormatter.format(num)
if (num < 1000000)
2022-12-02 03:18:36 +01:00
return `${Math.floor(num / 1000)}${k}`
return `${Math.floor(num / 1000000)}${m}`
}
2022-12-02 03:18:36 +01:00
export const formattedNumber = (num: number, useFormatter: Intl.NumberFormat = formatter) => {
return useFormatter.format(num)
}
2022-12-02 03:18:36 +01:00
export const useHumanReadableNumber = () => {
const i18n = useI18n()
const numberFormatter = $computed(() => Intl.NumberFormat(i18n.locale.value))
return {
formatHumanReadableNumber: (num: MaybeRef<number>) => {
return humanReadableNumber(
unref(num),
{ k: i18n.t('common.kiloSuffix'), m: i18n.t('common.megaSuffix') },
numberFormatter,
)
},
formatNumber: (num: MaybeRef<number>) => {
return formattedNumber(
unref(num),
numberFormatter,
)
},
forSR: (num: MaybeRef<number>) => {
return unref(num) > 10000
},
}
}