2022-12-02 09:52:00 +01:00
|
|
|
import type { MaybeRef } from '@vueuse/shared'
|
2022-11-26 09:34:24 +01:00
|
|
|
import type { MaybeComputedRef, UseTimeAgoOptions } from '@vueuse/core'
|
2022-11-25 09:57:34 +01:00
|
|
|
|
2022-12-02 09:52:00 +01:00
|
|
|
const formatter = Intl.NumberFormat()
|
|
|
|
|
|
|
|
export const formattedNumber = (num: number, useFormatter: Intl.NumberFormat = formatter) => {
|
|
|
|
return useFormatter.format(num)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useHumanReadableNumber = () => {
|
2023-01-01 15:29:11 +01:00
|
|
|
const { t, n, locale } = useI18n()
|
|
|
|
|
|
|
|
const fn = (num: number) => {
|
|
|
|
if (num < 10000)
|
|
|
|
return n(num, 'smallCounting', locale.value)
|
|
|
|
|
|
|
|
// show 1 decimal: we cannot use toFixed(1), it is a string
|
|
|
|
if (num < 1000000)
|
|
|
|
return `${n(Math.floor(num / 100) / 10, 'kiloCounting', locale.value)}${t('common.kiloSuffix')}`
|
|
|
|
|
|
|
|
// show 2 decimals: we cannot use toFixed(2), it is a string
|
|
|
|
return `${n(Math.floor(num / 10000) / 100, 'millionCounting', locale.value)}${t('common.megaSuffix')}`
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:52:00 +01:00
|
|
|
return {
|
2023-01-01 15:29:11 +01:00
|
|
|
formatHumanReadableNumber: (num: MaybeRef<number>) => fn(unref(num)),
|
|
|
|
formatNumber: (num: MaybeRef<number>) => n(unref(num), 'smallCounting', locale.value),
|
|
|
|
formatPercentage: (num: MaybeRef<number>) => n(unref(num), 'percentage', locale.value),
|
|
|
|
forSR: (num: MaybeRef<number>) => unref(num) > 10000,
|
2022-12-02 09:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 09:57:34 +01:00
|
|
|
export const useFormattedDateTime = (
|
2022-11-27 09:48:04 +01:00
|
|
|
value: MaybeComputedRef<string | Date | undefined | null>,
|
2022-11-25 09:57:34 +01:00
|
|
|
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },
|
|
|
|
) => {
|
2022-11-30 00:25:29 +01:00
|
|
|
const { locale } = useI18n()
|
2022-12-01 14:14:58 +01:00
|
|
|
const formatter = $computed(() => Intl.DateTimeFormat(locale.value, options))
|
2022-11-26 04:36:18 +01:00
|
|
|
return computed(() => {
|
|
|
|
const v = resolveUnref(value)
|
|
|
|
return v ? formatter.format(new Date(v)) : ''
|
|
|
|
})
|
2022-11-25 09:57:34 +01:00
|
|
|
}
|
2022-11-26 06:05:44 +01:00
|
|
|
|
2022-12-02 09:16:06 +01:00
|
|
|
export const useTimeAgoOptions = (short = false): UseTimeAgoOptions<false> => {
|
2023-01-01 15:29:11 +01:00
|
|
|
const { d, t, n: fnf, locale } = useI18n()
|
2022-12-02 09:16:06 +01:00
|
|
|
const prefix = short ? 'short_' : ''
|
2022-12-02 03:18:36 +01:00
|
|
|
|
2023-01-01 15:29:11 +01:00
|
|
|
const fn = (n: number, past: boolean, key: string) => {
|
|
|
|
return t(`time_ago_options.${prefix}${key}_${past ? 'past' : 'future'}`, n, {
|
|
|
|
named: {
|
|
|
|
v: fnf(n, 'smallCounting', locale.value),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-02 03:18:36 +01:00
|
|
|
return {
|
2022-12-19 22:20:32 +01:00
|
|
|
rounding: 'floor',
|
2022-12-02 09:16:06 +01:00
|
|
|
showSecond: !short,
|
|
|
|
updateInterval: short ? 60_000 : 1_000,
|
2022-12-02 03:18:36 +01:00
|
|
|
messages: {
|
|
|
|
justNow: t('time_ago_options.just_now'),
|
|
|
|
// just return the value
|
|
|
|
past: n => n,
|
|
|
|
// just return the value
|
|
|
|
future: n => n,
|
2023-01-01 15:29:11 +01:00
|
|
|
second: (n, p) => fn(n, p, 'second'),
|
|
|
|
minute: (n, p) => fn(n, p, 'minute'),
|
|
|
|
hour: (n, p) => fn(n, p, 'hour'),
|
|
|
|
day: (n, p) => fn(n, p, 'day'),
|
|
|
|
week: (n, p) => fn(n, p, 'week'),
|
|
|
|
month: (n, p) => fn(n, p, 'month'),
|
|
|
|
year: (n, p) => fn(n, p, 'year'),
|
2022-12-20 14:27:53 +01:00
|
|
|
invalid: '',
|
2022-12-02 03:18:36 +01:00
|
|
|
},
|
|
|
|
fullDateFormatter(date) {
|
2022-12-02 09:16:06 +01:00
|
|
|
return d(date, short ? 'short' : 'long')
|
2022-12-02 03:18:36 +01:00
|
|
|
},
|
|
|
|
}
|
2022-11-26 06:05:44 +01:00
|
|
|
}
|