Skip to content

Commit 7a76bb6

Browse files
committed
feat: deepClone
1 parent ad07dbb commit 7a76bb6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/utils/clone.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 深拷贝
2+
export function deepClone<T>(source: T): T {
3+
return Array.isArray(source)
4+
? source.map((item) => deepClone(item))
5+
: source instanceof Date
6+
? new Date(source.getTime())
7+
: source && typeof source === 'object'
8+
? Object.getOwnPropertyNames(source).reduce((o, prop) => {
9+
Object.defineProperty(o, prop, Object.getOwnPropertyDescriptor(source, prop)!)
10+
o[prop] = deepClone((source as { [key: string]: any })[prop])
11+
return o
12+
}, Object.create(Object.getPrototypeOf(source)))
13+
: (source as T)
14+
}

src/utils/shared.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ export function lastItem<T>(arr: T[]) {
22
return arr[arr.length - 1]
33
}
44

5-
export function clone<T>(json: T): T {
6-
return JSON.parse(JSON.stringify(json))
7-
}
8-
95
export function isValidKey(
106
object: object,
117
key: string | number | symbol

0 commit comments

Comments
 (0)