-
Notifications
You must be signed in to change notification settings - Fork 170
/
util.ts
205 lines (200 loc) · 6.34 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const symbolFor = typeof Symbol === 'function' && Symbol.for
const REACT_FORWARD_REF_TYPE = symbolFor ? Symbol.for('react.forward_ref') : 0xead0
const REACT_MEMO_TYPE = symbolFor ? Symbol.for('react.memo') : 0xead3
export const hashShort = 9
export const reHttp = /^https?:\/\//i
export const reModuleExt = /\.(js|jsx|mjs|ts|tsx)$/i
export const reStyleModuleExt = /\.(css|less|sass|scss)$/i
export const reMDExt = /\.(md|markdown)$/i
export const reLocaleID = /^[a-z]{2}(-[a-zA-Z0-9]+)?$/
export const reHashJs = new RegExp(`\\.[0-9a-fx]{${hashShort}}\\.js$`, 'i')
export const KB = 1024
export const MB = KB ** 2
export const GB = KB ** 3
export const TB = KB ** 4
export const PB = KB ** 5
export default {
isNumber(a: any): a is number {
return typeof a === 'number' && !Number.isNaN(a)
},
isUNumber(a: any): a is number {
return this.isNumber(a) && a >= 0
},
isInt(a: any): a is number {
return this.isNumber(a) && Number.isInteger(a)
},
isUInt(a: any): a is number {
return this.isInt(a) && a >= 0
},
isString(a: any): a is string {
return typeof a === 'string'
},
isNEString(a: any): a is string {
return typeof a === 'string' && a.length > 0
},
isArray(a: any): a is Array<any> {
return Array.isArray(a)
},
isNEArray(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 && !this.isArray(a) && Object.getPrototypeOf(a) == Object.prototype
},
isFunction(a: any): a is Function {
return typeof a === 'function'
},
isLikelyReactComponent(type: any): Boolean {
switch (typeof type) {
case 'function':
if (type.prototype != null) {
if (type.prototype.isReactComponent) {
return true
}
const ownNames = Object.getOwnPropertyNames(type.prototype);
if (ownNames.length > 1 || ownNames[0] !== 'constructor') {
return false
}
}
const name = type.name || type.displayName
return typeof name === 'string' && /^[A-Z]/.test(name)
case 'object':
if (type != null) {
switch (type.$$typeof) {
case REACT_FORWARD_REF_TYPE:
case REACT_MEMO_TYPE:
return true
default:
return false
}
}
return false
default:
return false
}
},
isHttpUrl(url: string) {
try {
const { protocol } = new URL(url)
return protocol === 'https:' || protocol === 'http:'
} catch (error) {
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
},
ensureExt(s: string, ext: string): string {
if (s.endsWith(ext)) {
return s
}
return s + ext
},
splitBy(s: string, searchString: string): [string, string] {
const i = s.indexOf(searchString)
if (i >= 0) {
return [s.slice(0, i), s.slice(i + 1)]
}
return [s, '']
},
bytesString(bytes: number) {
if (bytes < KB) {
return bytes.toString() + 'B'
} else if (bytes < MB) {
return Math.ceil(bytes / KB) + 'KB'
} else if (bytes < GB) {
return (bytes / MB).toFixed(1).replace(/\.0$/, '') + 'MB'
} else if (bytes < TB) {
return (bytes / GB).toFixed(1).replace(/\.0$/, '') + 'GB'
} else if (bytes < PB) {
return (bytes / TB).toFixed(1).replace(/\.0$/, '') + 'TB'
} else {
return (bytes / PB).toFixed(1).replace(/\.0$/, '') + 'PB'
}
},
splitPath(path: string): string[] {
return path
.split('/')
.map(p => p.trim())
.filter(p => p !== '' && p !== '.')
.reduce((path, p) => {
if (p === '..') {
path.pop()
} else {
path.push(p)
}
return path
}, [] 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
},
debounceX(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))
}
}
// @require Deno
export function existsDirSync(path: string) {
if (typeof Deno === 'undefined' && Deno.version.deno) {
throw new Error('require Deno')
}
try {
const fi = Deno.lstatSync(path)
if (fi.isDirectory) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
throw err
}
}
// @require Deno
export function existsFileSync(path: string) {
if (typeof Deno === 'undefined' && Deno.version.deno) {
throw new Error('require Deno')
}
try {
const fi = Deno.lstatSync(path)
if (fi.isFile) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
throw err
}
}