forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.ts
More file actions
36 lines (34 loc) · 1.1 KB
/
lang.ts
File metadata and controls
36 lines (34 loc) · 1.1 KB
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
/**
* @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
}
export const mergeObject = <T extends Record<string, any>>(defaults: T, newVal: Partial<T>): T => {
Object.entries(newVal).forEach(([k, v]) => {
if (typeof v === 'object' && !!v && !Array.isArray(v)) {
(defaults as any)[k] = mergeObject(defaults[k], v as Record<string, any>)
}
(defaults as any)[k] = v
})
return defaults
}