-
Notifications
You must be signed in to change notification settings - Fork 170
/
util.ts
128 lines (128 loc) · 3.51 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
export default {
isString(a: any): a is string {
return typeof a === 'string'
},
isFilledString(a: any): a is string {
return typeof a === 'string' && a.length > 0
},
isArray(a: any): a is Array<any> {
return Array.isArray(a)
},
isFilledArray(a: any): a is Array<any> {
return Array.isArray(a) && a.length > 0
},
isPlainObject(a: any): a is Record<string, any> {
return typeof a === 'object' && a !== null && Object.getPrototypeOf(a) === Object.prototype
},
isFunction(a: any): a is Function {
return typeof a === 'function'
},
isLikelyHttpURL(s: string): boolean {
const p = s.slice(0, 8).toLowerCase()
return p === 'https://' || p.slice(0, 7) === 'http://'
},
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
},
splitBy(s: string, searchString: string, fromLast = false): [string, string] {
const i = fromLast ? s.lastIndexOf(searchString) : s.indexOf(searchString)
if (i >= 0) {
return [s.slice(0, i), s.slice(i + 1)]
}
return [s, '']
},
btoaUrl(s: string) {
return btoa(s).replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_')
},
atobUrl(b64: string) {
const b = b64.length % 4
if (b === 3) {
b64 += '='
} else if (b === 2) {
b64 += '=='
} else if (b === 1) {
throw new TypeError('Illegal base64 Url String')
}
b64 = b64.replace(/\-/g, '+').replace(/_/g, '/')
return atob(b64)
},
formatBytes(bytes: number) {
const fix = (n: number) => {
if (n >= 10) {
return Math.ceil(n)
} else {
return (Math.round(10 * n) / 10).toFixed(1).replace(/\.0$/, '')
}
}
if (bytes < 1024) {
return bytes.toString() + 'B'
}
if (bytes < 1024 ** 2) {
return fix(bytes / 1024) + 'KB'
}
if (bytes < 1024 ** 3) {
return fix(bytes / 1024 ** 2) + 'MB'
}
if (bytes < 1024 ** 4) {
return fix(bytes / 1024 ** 3) + 'GB'
}
if (bytes < 1024 ** 5) {
return fix(bytes / 1024 ** 4) + 'TB'
}
return fix(bytes / 1024 ** 5) + 'PB'
},
splitPath(path: string): string[] {
return path
.split(/[\/\\]+/g)
.map(p => p.trim())
.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<T extends Function>(callback: T, delay: number): T {
let timer: number | null = null
return ((...args: any[]) => {
if (timer !== null) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null
callback(...args)
}, delay)
}) as any
},
debounceById(id: string, callback: () => void, delay: number) {
const self = this as any
const timers: Map<string, number> = self.__debounce_timers || (self.__debounce_timers = new Map())
if (timers.has(id)) {
clearTimeout(timers.get(id)!)
}
timers.set(id, setTimeout(() => {
timers.delete(id)
callback()
}, delay))
},
async isUrlOk(url: string): Promise<boolean> {
const res = await fetch(url).catch((e) => e)
await res.body?.cancel();
return res.status === 200
}
}