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