From 919bcc8451177a1891e0bb0a2a63ed25255a90dd Mon Sep 17 00:00:00 2001 From: cipchk Date: Sun, 14 Aug 2022 16:51:05 +0800 Subject: [PATCH] refactor(util): use rfdc instead of extend in `deepCopy` --- package.json | 4 +--- packages/util/other/deep.spec.ts | 15 ++++++++++++--- packages/util/other/deep.ts | 21 +++++++++++++++------ packages/util/other/index.en-US.md | 2 +- packages/util/other/index.zh-CN.md | 2 +- scripts/site/route-paths.txt | 2 -- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 0bffc8a9e..184a5d072 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "codesandbox": "^2.2.3", "ajv": "^8.11.0", "ajv-formats": "^2.1.1", - "extend": "^3.0.2", + "rfdc": "^1.3.0", "file-saver": "^2.0.5", "ng-github-button": "^14.0.0", "ng-zorro-antd": "^14.0.0-beta.0", @@ -117,8 +117,6 @@ "karma-viewport": "^1.0.9", "@types/aos": "^3.0.4", "@types/file-saver": "^2.0.5", - "@types/deep-extend": "^0.4.32", - "@types/extend": "^3.0.1", "@types/fs-extra": "^9.0.13", "@types/mockjs": "^1.0.6", "@types/parse5": "^6.0.3", diff --git a/packages/util/other/deep.spec.ts b/packages/util/other/deep.spec.ts index 5aafc3a17..d2bf0f82e 100644 --- a/packages/util/other/deep.spec.ts +++ b/packages/util/other/deep.spec.ts @@ -52,9 +52,18 @@ describe('abc: utils', () => { }); }); - it('#deepCopy', () => { - const a = { number: 1 }; - expect(deepCopy(a).number).toBe(a.number); + describe('#deepCopy', () => { + it('should be working', () => { + const a = { number: 1 }; + expect(deepCopy(a).number).toBe(a.number); + }); + it('when is null', () => { + expect(deepCopy(null)).toBe(null); + }); + it('when proto is false', () => { + const res = deepCopy(Object.create({ a: 1 }), { proto: false }); + expect(Object.keys(res).length).toBe(0); + }); }); describe('#deepMerge', () => { diff --git a/packages/util/other/deep.ts b/packages/util/other/deep.ts index 6edd949e5..2a2a73ebf 100644 --- a/packages/util/other/deep.ts +++ b/packages/util/other/deep.ts @@ -1,4 +1,4 @@ -import extend from 'extend'; +import rfdc from 'rfdc'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -21,13 +21,22 @@ export function deepGet(obj: NzSafeAny, path: string | string[] | null | undefin } /** - * Base on [extend](https://github.com/justmoon/node-extend) deep copy. + * Base on [rfdc](https://github.com/davidmarkclements/rfdc) deep copy. * - * 基于 [extend](https://github.com/justmoon/node-extend) 的深度拷贝 + * 基于 [rfdc](https://github.com/davidmarkclements/rfdc) 的深度拷贝 */ -export function deepCopy(obj: T | null | undefined): T { - const result = extend(true, {}, { _: obj }); - return result._ as T; +export function deepCopy(obj: T | null | undefined): T; +/** + * Base on [rfdc](https://github.com/davidmarkclements/rfdc) deep copy. + * + * 基于 [rfdc](https://github.com/davidmarkclements/rfdc) 的深度拷贝 + * + * - [options](https://github.com/davidmarkclements/rfdc#requirerfdcopts---proto-false-circles-false---cloneobj--obj2) 参数文档 + */ +export function deepCopy(obj: T | null | undefined, options?: rfdc.Options): T; +export function deepCopy(obj: T | null | undefined, options?: rfdc.Options): T { + if (obj == null) return obj as T; + return rfdc(options)(obj); } /** diff --git a/packages/util/other/index.en-US.md b/packages/util/other/index.en-US.md index 64b2fd5eb..a5b9a5893 100644 --- a/packages/util/other/index.en-US.md +++ b/packages/util/other/index.en-US.md @@ -23,7 +23,7 @@ deepGet(obj, 'user.age'); // 18 ## deepCopy -Base on [extend](https://github.com/justmoon/node-extend) deep copy. +Base on [rfdc](https://github.com/davidmarkclements/rfdc) deep copy. ```ts const source = { a: 1, user: { name: 'cipchk' } }; diff --git a/packages/util/other/index.zh-CN.md b/packages/util/other/index.zh-CN.md index 02e50bd17..897175576 100644 --- a/packages/util/other/index.zh-CN.md +++ b/packages/util/other/index.zh-CN.md @@ -23,7 +23,7 @@ deepGet(obj, 'user.age'); // 18 ## deepCopy -基于 [extend](https://github.com/justmoon/node-extend) 的深度拷贝。 +基于 [rfdc](https://github.com/davidmarkclements/rfdc) 的深度拷贝。 ```ts const source = { a: 1, user: { name: 'cipchk' } }; diff --git a/scripts/site/route-paths.txt b/scripts/site/route-paths.txt index f4390cc87..358d0cb3e 100644 --- a/scripts/site/route-paths.txt +++ b/scripts/site/route-paths.txt @@ -95,8 +95,6 @@ /components/global-footer/zh /components/highlight/en /components/highlight/zh -/components/image/en -/components/image/zh /components/let/en /components/let/zh /components/loading/en