-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
67 lines (61 loc) · 1.79 KB
/
Copy pathutils.ts
File metadata and controls
67 lines (61 loc) · 1.79 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/**
* Formats a number as currency with proper locale support
*
* @param amount The amount to format
* @param locale The locale to use for formatting (defaults to Norwegian)
* @param options Additional Intl.NumberFormat options
* @returns Formatted currency string
*/
export function formatCurrency(
amount: number,
locale: string = "nb-NO",
options: Intl.NumberFormatOptions = {}
): string {
return new Intl.NumberFormat(locale, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
...options,
}).format(amount)
}
export function formatBytes(
bytes: number,
opts: {
decimals?: number
sizeType?: "accurate" | "normal"
} = {},
) {
const { decimals = 0, sizeType = "normal" } = opts
const sizes = ["Bytes", "KB", "MB", "GB", "TB"]
const accurateSizes = ["Bytes", "KiB", "MiB", "GiB", "TiB"]
if (bytes === 0) return "0 Byte"
const i = Math.floor(Math.log(bytes) / Math.log(1024))
return `${(bytes / Math.pow(1024, i)).toFixed(decimals)} ${
sizeType === "accurate"
? (accurateSizes[i] ?? "Bytes")
: (sizes[i] ?? "Bytes")
}`
}
/**
* Stole this from the @radix-ui/primitive
* @see https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
*/
export function composeEventHandlers<E>(
originalEventHandler?: (event: E) => void,
ourEventHandler?: (event: E) => void,
{ checkForDefaultPrevented = true } = {},
) {
return function handleEvent(event: E) {
originalEventHandler?.(event)
if (
checkForDefaultPrevented === false ||
!(event as unknown as Event).defaultPrevented
) {
return ourEventHandler?.(event)
}
}
}