/
helpers.ts
146 lines (122 loc) · 3.75 KB
/
helpers.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
import type { Arrayable, Nullable } from './types'
export function assertTypes(value: unknown, name: string, types: string[]): void {
const receivedType = typeof value
const pass = types.includes(receivedType)
if (!pass)
throw new TypeError(`${name} value must be ${types.join(' or ')}, received "${receivedType}"`)
}
export function slash(path: string) {
return path.replace(/\\/g, '/')
}
// convert RegExp.toString to RegExp
export function parseRegexp(input: string): RegExp {
// Parse input
const m = input.match(/(\/?)(.+)\1([a-z]*)/i)
// match nothing
if (!m)
return /$^/
// Invalid flags
if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3]))
return RegExp(input)
// Create the regular expression
return new RegExp(m[2], m[3])
}
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
if (array === null || array === undefined)
array = []
if (Array.isArray(array))
return array
return [array]
}
export function isObject(item: unknown): boolean {
return item != null && typeof item === 'object' && !Array.isArray(item)
}
function isFinalObj(obj: any) {
return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype
}
export function getType(value: unknown): string {
return Object.prototype.toString.apply(value).slice(8, -1)
}
function collectOwnProperties(obj: any, collector: Set<string | symbol> | ((key: string | symbol) => void)) {
const collect = typeof collector === 'function' ? collector : (key: string | symbol) => collector.add(key)
Object.getOwnPropertyNames(obj).forEach(collect)
Object.getOwnPropertySymbols(obj).forEach(collect)
}
export function getOwnProperties(obj: any) {
const ownProps = new Set<string | symbol>()
if (isFinalObj(obj))
return []
collectOwnProperties(obj, ownProps)
return Array.from(ownProps)
}
export function deepClone<T>(val: T): T {
const seen = new WeakMap()
return clone(val, seen)
}
export function clone<T>(val: T, seen: WeakMap<any, any>): T {
let k: any, out: any
if (seen.has(val))
return seen.get(val)
if (Array.isArray(val)) {
out = Array(k = val.length)
seen.set(val, out)
while (k--)
out[k] = clone(val[k], seen)
return out as any
}
if (Object.prototype.toString.call(val) === '[object Object]') {
out = Object.create(Object.getPrototypeOf(val))
seen.set(val, out)
// we don't need properties from prototype
const props = getOwnProperties(val)
for (const k of props) {
const descriptor = Object.getOwnPropertyDescriptor(val, k)
if (!descriptor)
continue
const cloned = clone((val as any)[k], seen)
if ('get' in descriptor) {
Object.defineProperty(out, k, {
...descriptor,
get() {
return cloned
},
})
}
else {
Object.defineProperty(out, k, {
...descriptor,
value: cloned,
})
}
}
return out
}
return val
}
export function noop() {}
export function objectAttr(source: any, path: string, defaultValue = undefined) {
// a[3].b -> a.3.b
const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.')
let result = source
for (const p of paths) {
result = Object(result)[p]
if (result === undefined)
return defaultValue
}
return result
}
type DeferPromise<T> = Promise<T> & {
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}
export function createDefer<T>(): DeferPromise<T> {
let resolve: ((value: T | PromiseLike<T>) => void) | null = null
let reject: ((reason?: any) => void) | null = null
const p = new Promise<T>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
}) as DeferPromise<T>
p.resolve = resolve!
p.reject = reject!
return p
}