-
Notifications
You must be signed in to change notification settings - Fork 600
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (52 loc) · 1.77 KB
/
index.ts
File metadata and controls
66 lines (52 loc) · 1.77 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
/** Parameterized hex string type for specific byte lengths */
export type Hex<TByteLength extends number> = `0x${string}` & { readonly _length: TByteLength };
export function hasHexPrefix(str: string): str is `0x${string}` {
return str.startsWith('0x');
}
export function withoutHexPrefix(str: string): string {
return hasHexPrefix(str) ? str.slice(2) : str;
}
export function withHexPrefix(str: string): `0x${string}` {
return hasHexPrefix(str) ? str : `0x${str}`;
}
export function isHex(str: string): boolean {
return /^(0x)?[0-9a-fA-F]*$/.test(str);
}
export function hexToBuffer(str: string): Buffer {
return Buffer.from(withoutHexPrefix(str), 'hex');
}
export function bufferToHex(buffer: Buffer): `0x${string}` {
return `0x${buffer.toString('hex')}`;
}
export function pluralize(str: string, count: number | bigint, plural?: string): string {
return count === 1 || count === 1n ? str : (plural ?? `${str}s`);
}
export function count(count: number | bigint, str: string, plural?: string): string {
return `${count} ${pluralize(str, count, plural)}`;
}
export function truncate(str: string, length: number = 64): string {
return str.length > length ? str.slice(0, length) + '...' : str;
}
export function isoDate(date?: Date) {
return (date ?? new Date()).toISOString().replace(/[-:T]/g, '').replace(/\..+$/, '');
}
export function urlJoin(...args: string[]): string {
const processed = [];
for (const arg of args) {
if (arg.length === 0) {
continue;
}
let start = 0;
let end = arg.length - 1;
while (start <= end && arg[start] === '/') {
start++;
}
while (end >= start && arg[end] === '/') {
end--;
}
if (start <= end) {
processed.push(arg.slice(start, end + 1));
}
}
return processed.join('/');
}