-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
41 lines (33 loc) · 1010 Bytes
/
utils.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
import { writeFileSync } from 'fs'
export function clamp(x: number, lo: number, hi: number): number {
return Math.max(lo, Math.min(x, hi))
}
export function className(classNames: Array<string | false>): string {
return classNames.filter(Boolean).join(' ')
}
export function escapeRegExp(string: string): string {
return string.trim().replace(/[.*+?^${}()|[\]\\]/g, '')
}
export function isConstructor(x: any): boolean {
try {
Reflect.construct(Object, [], x)
return true
} catch {
return false
}
}
export function not(x: any): boolean {
return !x
}
export function writeJSON(path: string, data: unknown, minify = false): void {
writeFileSync(path, minify ? JSON.stringify(data) : JSON.stringify(data, null, 3))
}
function flat(xs: Array<any>, depth: number = 1): Array<any> {
return depth < 1 ? xs.slice() : flat([].concat(...xs), depth - 1)
}
Array.prototype.flat =
Array.prototype.flat ||
function(depth: number) {
// @ts-ignore
return flat(this, depth)
}