|
| 1 | +import type { UseTimeAgoOptions } from '@vueuse/core' |
| 2 | + |
| 3 | +const formatter = Intl.NumberFormat() |
| 4 | + |
| 5 | +export function formattedNumber(num: number, useFormatter: Intl.NumberFormat = formatter) { |
| 6 | + return useFormatter.format(num) |
| 7 | +} |
| 8 | + |
| 9 | +export function useHumanReadableNumber() { |
| 10 | + const { n, locale } = useI18n() |
| 11 | + |
| 12 | + const fn = (num: number) => { |
| 13 | + return n( |
| 14 | + num, |
| 15 | + num < 10000 ? 'smallCounting' : num < 1000000 ? 'kiloCounting' : 'millionCounting', |
| 16 | + locale.value, |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + return { |
| 21 | + formatHumanReadableNumber: (num: MaybeRef<number>) => fn(unref(num)), |
| 22 | + formatNumber: (num: MaybeRef<number>) => n(unref(num), 'smallCounting', locale.value), |
| 23 | + formatPercentage: (num: MaybeRef<number>) => n(unref(num), 'percentage', locale.value), |
| 24 | + forSR: (num: MaybeRef<number>) => unref(num) > 10000, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export function useFormattedDateTime( |
| 29 | + value: MaybeRefOrGetter<string | number | Date | undefined | null>, |
| 30 | + options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' }, |
| 31 | +) { |
| 32 | + const { locale } = useI18n() |
| 33 | + const formatter = computed(() => Intl.DateTimeFormat(locale.value, options)) |
| 34 | + return computed(() => { |
| 35 | + const v = toValue(value) |
| 36 | + return v ? formatter.value.format(new Date(v)) : '' |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +export function useTimeAgoOptions(short = false): UseTimeAgoOptions<false> { |
| 41 | + const { d, t, n: fnf, locale } = useI18n() |
| 42 | + const prefix = short ? 'short_' : '' |
| 43 | + |
| 44 | + const fn = (n: number, past: boolean, key: string) => { |
| 45 | + return t(`time_ago_options.${prefix}${key}_${past ? 'past' : 'future'}`, n, { |
| 46 | + named: { |
| 47 | + v: fnf(n, 'smallCounting', locale.value), |
| 48 | + }, |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + rounding: 'floor', |
| 54 | + showSecond: !short, |
| 55 | + updateInterval: short ? 60000 : 1000, |
| 56 | + messages: { |
| 57 | + justNow: t('time_ago_options.just_now'), |
| 58 | + // just return the value |
| 59 | + past: n => n, |
| 60 | + // just return the value |
| 61 | + future: n => n, |
| 62 | + second: (n, p) => fn(n, p, 'second'), |
| 63 | + minute: (n, p) => fn(n, p, 'minute'), |
| 64 | + hour: (n, p) => fn(n, p, 'hour'), |
| 65 | + day: (n, p) => fn(n, p, 'day'), |
| 66 | + week: (n, p) => fn(n, p, 'week'), |
| 67 | + month: (n, p) => fn(n, p, 'month'), |
| 68 | + year: (n, p) => fn(n, p, 'year'), |
| 69 | + invalid: '', |
| 70 | + }, |
| 71 | + fullDateFormatter(date) { |
| 72 | + return d(date, short ? 'short' : 'long') |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export function useFileSizeFormatter() { |
| 78 | + const { locale } = useI18n() |
| 79 | + |
| 80 | + const formatters = computed( |
| 81 | + () => |
| 82 | + [ |
| 83 | + Intl.NumberFormat(locale.value, { |
| 84 | + style: 'unit', |
| 85 | + unit: 'megabyte', |
| 86 | + unitDisplay: 'narrow', |
| 87 | + maximumFractionDigits: 0, |
| 88 | + }), |
| 89 | + Intl.NumberFormat(locale.value, { |
| 90 | + style: 'unit', |
| 91 | + unit: 'kilobyte', |
| 92 | + unitDisplay: 'narrow', |
| 93 | + maximumFractionDigits: 0, |
| 94 | + }), |
| 95 | + ] as const, |
| 96 | + ) |
| 97 | + |
| 98 | + const megaByte = 1024 * 1024 |
| 99 | + |
| 100 | + function formatFileSize(size: number) { |
| 101 | + return size >= megaByte |
| 102 | + ? formatters.value[0].format(size / megaByte) |
| 103 | + : formatters.value[1].format(size / 1024) |
| 104 | + } |
| 105 | + |
| 106 | + return { formatFileSize } |
| 107 | +} |
0 commit comments