/
helpers.ts
209 lines (176 loc) · 5.29 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
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
206
207
208
209
import type { Arrayable, Nullable } from './types'
interface CloneOptions {
forceWritable?: boolean
}
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
return v != null
}
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 isPrimitive(value: unknown) {
return value === null || (typeof value !== 'function' && typeof value !== 'object')
}
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)
}
const defaultCloneOptions: CloneOptions = { forceWritable: false }
export function deepClone<T>(
val: T,
options: CloneOptions = defaultCloneOptions,
): T {
const seen = new WeakMap()
return clone(val, seen, options)
}
export function clone<T>(
val: T,
seen: WeakMap<any, any>,
options: CloneOptions = defaultCloneOptions,
): 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,
writable: options.forceWritable ? true : descriptor.writable,
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
}
/**
* If code starts with a function call, will return its last index, respecting arguments.
* This will return 25 - last ending character of toMatch ")"
* Also works with callbacks
* ```
* toMatch({ test: '123' });
* toBeAliased('123')
* ```
*/
export function getCallLastIndex(code: string) {
let charIndex = -1
let inString: string | null = null
let startedBracers = 0
let endedBracers = 0
let beforeChar: string | null = null
while (charIndex <= code.length) {
beforeChar = code[charIndex]
charIndex++
const char = code[charIndex]
const isCharString = char === '"' || char === '\'' || char === '`'
if (isCharString && beforeChar !== '\\') {
if (inString === char)
inString = null
else if (!inString)
inString = char
}
if (!inString) {
if (char === '(')
startedBracers++
if (char === ')')
endedBracers++
}
if (startedBracers && endedBracers && startedBracers === endedBracers)
return charIndex
}
return null
}