-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathlang.ts
More file actions
26 lines (25 loc) · 754 Bytes
/
lang.ts
File metadata and controls
26 lines (25 loc) · 754 Bytes
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
/**
* @since 2.1.7
*/
export const deepCopy = <T = any | null | undefined>(obj: T): T => {
if (!obj) return obj
if (typeof obj !== 'object') return obj
let deep: Record<string, any> = {}
Object.entries(obj).forEach(([k, v]) => {
if (typeof v !== "object" || v === null) {
deep[k] = v
} else if (Array.isArray(v)) {
deep[k] = v.map(e => deepCopy(e))
} else if (v instanceof Set) {
deep[k] = new Set(v)
} else if (v instanceof Map) {
deep[k] = new Map(v)
} else if (v instanceof Date) {
deep[k] = new Date(v.getTime())
} else {
// Ignored type
deep[k] = deepCopy(v)
}
})
return deep as T
}