2023-05-02 08:58:55 +09:00
|
|
|
import type { MaybeRef, MaybeRefOrGetter, UseTimeAgoOptions } from '@vueuse/core'
|
2022-11-25 17:57:34 +09:00
|
|
|
|
2022-12-02 17:52:00 +09:00
|
|
|
const formatter = Intl.NumberFormat()
|
|
|
|
|
2023-01-08 17:21:25 +09:00
|
|
|
export function formattedNumber(num: number, useFormatter: Intl.NumberFormat = formatter) {
|
2022-12-02 17:52:00 +09:00
|
|
|
return useFormatter.format(num)
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:21:25 +09:00
|
|
|
export function useHumanReadableNumber() {
|
2023-01-02 04:31:14 +09:00
|
|
|
const { n, locale } = useI18n()
|
2023-01-01 23:29:11 +09:00
|
|
|
|
|
|
|
const fn = (num: number) => {
|
2023-01-02 04:31:14 +09:00
|
|
|
return n(
|
|
|
|
num,
|
|
|
|
num < 10000
|
|
|
|
? 'smallCounting'
|
|
|
|
: num < 1000000
|
|
|
|
? 'kiloCounting'
|
|
|
|
: 'millionCounting',
|
|
|
|
locale.value,
|
|
|
|
)
|
2023-01-01 23:29:11 +09:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:52:00 +09:00
|
|
|
return {
|
2023-01-01 23:29:11 +09: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 17:52:00 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 23:48:58 +09:00
|
|
|
export function useFormattedDateTime(
|
|
|
|
value: MaybeRefOrGetter<string | number | Date | undefined | null>,
|
|
|
|
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },
|
|
|
|
) {
|
2022-11-30 08:25:29 +09:00
|
|
|
const { locale } = useI18n()
|
2024-02-22 00:20:08 +09:00
|
|
|
const formatter = computed(() => Intl.DateTimeFormat(locale.value, options))
|
2022-11-26 12:36:18 +09:00
|
|
|
return computed(() => {
|
|
|
|
const v = resolveUnref(value)
|
2024-02-22 00:20:08 +09:00
|
|
|
return v ? formatter.value.format(new Date(v)) : ''
|
2022-11-26 12:36:18 +09:00
|
|
|
})
|
2022-11-25 17:57:34 +09:00
|
|
|
}
|
2022-11-26 14:05:44 +09:00
|
|
|
|
2023-01-08 17:21:25 +09:00
|
|
|
export function useTimeAgoOptions(short = false): UseTimeAgoOptions<false> {
|
2023-01-01 23:29:11 +09:00
|
|
|
const { d, t, n: fnf, locale } = useI18n()
|
2022-12-02 17:16:06 +09:00
|
|
|
const prefix = short ? 'short_' : ''
|
2022-12-02 11:18:36 +09:00
|
|
|
|
2023-01-01 23:29:11 +09: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 11:18:36 +09:00
|
|
|
return {
|
2022-12-20 06:20:32 +09:00
|
|
|
rounding: 'floor',
|
2022-12-02 17:16:06 +09:00
|
|
|
showSecond: !short,
|
2023-01-08 17:21:25 +09:00
|
|
|
updateInterval: short ? 60000 : 1000,
|
2022-12-02 11:18:36 +09: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 23:29:11 +09: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 22:27:53 +09:00
|
|
|
invalid: '',
|
2022-12-02 11:18:36 +09:00
|
|
|
},
|
|
|
|
fullDateFormatter(date) {
|
2022-12-02 17:16:06 +09:00
|
|
|
return d(date, short ? 'short' : 'long')
|
2022-12-02 11:18:36 +09:00
|
|
|
},
|
|
|
|
}
|
2022-11-26 14:05:44 +09:00
|
|
|
}
|
2024-04-04 19:28:18 +09:00
|
|
|
|
|
|
|
export function useFileSizeFormatter() {
|
|
|
|
const { locale } = useI18n()
|
|
|
|
|
|
|
|
const formatters = computed(() => ([
|
|
|
|
Intl.NumberFormat(locale.value, {
|
|
|
|
style: 'unit',
|
|
|
|
unit: 'megabyte',
|
|
|
|
unitDisplay: 'narrow',
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
}),
|
|
|
|
Intl.NumberFormat(locale.value, {
|
|
|
|
style: 'unit',
|
|
|
|
unit: 'kilobyte',
|
|
|
|
unitDisplay: 'narrow',
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
}),
|
|
|
|
]))
|
|
|
|
|
|
|
|
const megaByte = 1024 * 1024
|
|
|
|
|
|
|
|
function formatFileSize(size: number) {
|
|
|
|
return size >= megaByte
|
|
|
|
? formatters.value[0].format(size / megaByte)
|
|
|
|
: formatters.value[1].format(size / 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
return { formatFileSize }
|
|
|
|
}
|