diff --git a/src/utils/deep-clone.ts b/src/utils/deep-clone.ts index 44aada0e6..bc2b88e2d 100644 --- a/src/utils/deep-clone.ts +++ b/src/utils/deep-clone.ts @@ -1,12 +1,21 @@ -import { deepAssign } from './deep-assign'; +import {isDef} from "./base"; + +export function deepClone | null | undefined>(obj: T): T { + if (!isDef(obj)) { + return obj; + } -export function deepClone>(obj: T): T { if (Array.isArray(obj)) { return (obj.map((item) => deepClone(item)) as unknown) as T; } if (typeof obj === 'object') { - return deepAssign({}, obj) as T; + const to = {} as Record; + Object.keys(obj).forEach((key) => { + to[key] = deepClone(obj[key]); + }); + + return to as T; } return obj; diff --git a/src/utils/test/index.spec.ts b/src/utils/test/index.spec.ts index c64c76034..656a2b489 100644 --- a/src/utils/test/index.spec.ts +++ b/src/utils/test/index.spec.ts @@ -11,9 +11,11 @@ import { addUnit, unitToPx } from '../format/unit'; test('deepClone', () => { const a = { foo: 0 }; const b = { foo: 0, bar: 1 }; + const c = null; const arr = [a, b]; expect(deepClone(a)).toEqual(a); expect(deepClone(b)).toEqual(b); + expect(deepClone(c)).toEqual(c); expect(deepClone(noop)).toEqual(noop); expect(deepClone(arr)).toEqual(arr); });