Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(util): use rfdc instead of extend in deepCopy #1510

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 12 additions & 3 deletions packages/util/other/deep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
21 changes: 15 additions & 6 deletions packages/util/other/deep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extend from 'extend';
import rfdc from 'rfdc';

import type { NzSafeAny } from 'ng-zorro-antd/core/types';

Expand All @@ -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<T extends { [key: string]: NzSafeAny } = NzSafeAny>(obj: T | null | undefined): T {
const result = extend(true, {}, { _: obj });
return result._ as T;
export function deepCopy<T>(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<T>(obj: T | null | undefined, options?: rfdc.Options): T;
export function deepCopy<T>(obj: T | null | undefined, options?: rfdc.Options): T {
if (obj == null) return obj as T;
return rfdc(options)<T>(obj);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/util/other/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' } };
Expand Down
2 changes: 1 addition & 1 deletion packages/util/other/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' } };
Expand Down
2 changes: 0 additions & 2 deletions scripts/site/route-paths.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down