-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclone.ts
58 lines (44 loc) · 1.42 KB
/
clone.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
import isArray from './isArray'
import isObject from './isObject'
export function clone<T>(value: T): T {
if (value === null || typeof value !== 'object') {
return value
}
if (isArray(value)) {
return value.slice() as T
}
if (value instanceof Date) {
return new Date(value.getTime()) as T
}
if (value instanceof RegExp) {
return new RegExp(value.source, value.flags) as T
}
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
const TypedArrayConstructor = value.constructor as new (buffer: ArrayBuffer) => ArrayBufferView
return new TypedArrayConstructor(value.buffer.slice(0)) as T
}
if (value instanceof ArrayBuffer) {
return value.slice(0) as T
}
if (value instanceof Map) {
return new Map(value) as T
}
if (value instanceof Set) {
return new Set(value) as T
}
if (isObject(value)) {
const result = Object.create(Object.getPrototypeOf(value))
Object.assign(result, value)
const symbols = Object.getOwnPropertySymbols(value)
if (symbols.length) {
symbols.forEach((sym) => {
if (Object.prototype.propertyIsEnumerable.call(value, sym)) {
result[sym] = value[sym as keyof typeof value]
}
})
}
return result as T
}
return {} as T
}
export default clone