forked from qifi-dev/qrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintlNumberFormat.ts
50 lines (45 loc) · 1.17 KB
/
intlNumberFormat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export function useNumberFormat(
value: MaybeRef<number | string> | ComputedRef<number | string>,
options?: {
locales?: Intl.LocalesArgument
} & Intl.NumberFormatOptions,
) {
const opts = {
locales: options?.locales ?? 'en-US',
...options,
}
const formatter = new Intl.NumberFormat(opts.locales, opts)
const val = toRef(value)
return computed(() => {
const parsedNum = Number.parseFloat(val.value as string)
if (Number.isNaN(parsedNum)) {
return formatter.format(0)
}
return formatter.format(parsedNum)
})
}
export function useKiloBytesNumberFormat(
value: MaybeRef<number | string> | ComputedRef<number | string>,
options?: {
locales?: Intl.LocalesArgument
} & Intl.NumberFormatOptions,
) {
return useNumberFormat(value, {
...options,
style: 'unit',
unit: 'kilobyte',
})
}
export function useKiloBytesPerSecondNumberFormat(
value: MaybeRef<number | string> | ComputedRef<number | string>,
options?: {
locales?: Intl.LocalesArgument
} & Intl.NumberFormatOptions,
) {
return useNumberFormat(value, {
...options,
style: 'unit',
unit: 'kilobyte-per-second',
unitDisplay: 'short',
})
}