/
util.ts
114 lines (114 loc) · 3.5 KB
/
util.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
export default {
utf8TextEncoder: new TextEncoder(),
utf8TextDecoder: new TextDecoder(),
isInt(a: unknown): a is number {
return typeof a === "number" && !Number.isNaN(a) && Number.isInteger(a);
},
isUint(a: unknown): a is number {
return this.isInt(a) && a >= 0;
},
isFilledString(a: unknown): a is string {
return typeof a === "string" && a.length > 0;
},
isFilledArray(a: unknown): a is Array<unknown> {
return Array.isArray(a) && a.length > 0;
},
isPlainObject<T = Record<string, unknown>>(a: unknown): a is T {
return a !== null && typeof a === "object" && Object.getPrototypeOf(a) === Object.prototype;
},
isLikelyHttpURL(s: string): boolean {
const p = s.slice(0, 8).toLowerCase();
return p === "https://" || p.slice(0, 7) === "http://";
},
startsWithAny(str: string, ...prefixs: string[]) {
for (const prefix of prefixs) {
if (str.startsWith(prefix)) {
return true;
}
}
return false;
},
endsWithAny(str: string, ...suffixes: string[]) {
for (const suffix of suffixes) {
if (str.endsWith(suffix)) {
return true;
}
}
return false;
},
trimPrefix(s: string, prefix: string): string {
if (prefix !== "" && s.startsWith(prefix)) {
return s.slice(prefix.length);
}
return s;
},
trimSuffix(s: string, suffix: string): string {
if (suffix !== "" && s.endsWith(suffix)) {
return s.slice(0, -suffix.length);
}
return s;
},
pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: K[]): Record<string, unknown> {
const result: Record<string, unknown> = {};
for (const key of keys) {
if (key in obj) {
result[key as string] = obj[key];
}
}
return result;
},
splitBy(s: string, searchString: string, fromLast = false): [prefix: string, suffix: string] {
const i = fromLast ? s.lastIndexOf(searchString) : s.indexOf(searchString);
if (i >= 0) {
return [s.slice(0, i), s.slice(i + 1)];
}
return [s, ""];
},
toHex(buffer: ArrayBuffer) {
const bytes = new Uint8Array(buffer);
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
},
computeHash(algorithm: AlgorithmIdentifier, data: string | Uint8Array): Promise<string> {
return crypto.subtle.digest(
algorithm,
typeof data === "string" ? this.utf8TextEncoder.encode(data) : data,
).then((sum) => this.toHex(sum));
},
prettyBytes(bytes: number) {
const units = ["", "K", "M", "G", "T", "P", "E"];
const exp = Math.floor(Math.log(bytes) / Math.log(1024));
return `${Math.round(bytes * 100 / Math.pow(1024, exp)) / 100}${units[exp]}B`;
},
splitPath(path: string): string[] {
return path
.split(/[\/\\]+/g)
.filter((p) => p !== "" && p !== ".")
.reduce((slice, p) => {
if (p === "..") {
slice.pop();
} else {
slice.push(p);
}
return slice;
}, [] as Array<string>);
},
cleanPath(path: string): string {
return "/" + this.splitPath(path).join("/");
},
debounce<Args extends unknown[], F extends (...args: Args) => void>(
fn: F,
delay: number,
): (this: ThisParameterType<F>, ...args: Parameters<F>) => void {
let timer: number | null;
function debounced(this: ThisParameterType<F>, ...args: Parameters<F>): void {
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(() => {
timer = null;
fn(...args);
}, delay);
}
return debounced;
},
};