add types

This commit is contained in:
zhaolihang-com 2024-10-19 17:36:14 +08:00
parent 1666647ae7
commit 7b8a85e788
10 changed files with 48 additions and 21 deletions

10
dist/deep-copy.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
/**
* Deep copy the given object considering circular structure.
* This function caches all nested objects and its copies.
* If it detects circular structure, use cached copy to avoid infinite loop.
*
* @param {*} obj
* @param {Array<Object>} cache
* @return {*}
*/
export declare function deepCopy<T>(obj: T, cache?: any[]): T;

2
dist/deep-copy.js vendored
View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.deepCopy = void 0;
/** from https://github.com/vuejs/vuex/blob/dev/src/util.js /** from https://github.com/vuejs/vuex/blob/dev/src/util.js
* Get the first item that pass the test * Get the first item that pass the test
* by second argument function * by second argument function
@ -41,4 +42,3 @@ function deepCopy(obj, cache) {
return copy; return copy;
} }
exports.deepCopy = deepCopy; exports.deepCopy = deepCopy;
//# sourceMappingURL=deep-copy.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"deep-copy.js","sourceRoot":"","sources":["../src/deep-copy.ts"],"names":[],"mappings":";;AAAA;;;;;;;GAOG;AACH,cAAc,IAAI,EAAE,CAAC;IACjB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7B,CAAC;AAED;;;;;;;;GAQG;AACH,kBAA4B,GAAM,EAAE,KAAU;IAAV,sBAAA,EAAA,UAAU;IAC1C,wCAAwC;IACxC,EAAE,CAAC,CAAC,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC;QAC1C,MAAM,CAAC,GAAG,CAAC;IACf,CAAC;IAED,6CAA6C;IAC7C,IAAM,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,QAAQ,KAAK,GAAG,EAAlB,CAAkB,CAAC,CAAC;IACjD,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACN,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;IACpB,CAAC;IAED,IAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;IAC1C,mCAAmC;IACnC,oDAAoD;IACpD,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,MAAA,EAAE,CAAC,CAAC;IAEpC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,UAAA,GAAG;QACxB,IAAI,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,MAAM,CAAI,IAAI,CAAC;AACnB,CAAC;AAtBD,4BAsBC"}

1
dist/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export {};

3
dist/test.js vendored
View File

@ -5,8 +5,7 @@ var a = { dataA: 10, b: null };
var b = { dataB: 20, a: null }; var b = { dataB: 20, a: null };
a.b = b; a.b = b;
b.a = a; b.a = a;
var newA = deep_copy_1.deepCopy(a); var newA = (0, deep_copy_1.deepCopy)(a);
console.log(newA.dataA === 10); //true console.log(newA.dataA === 10); //true
console.log(newA.b.dataB === 20); //true console.log(newA.b.dataB === 20); //true
console.log(newA.b.a === newA); //true console.log(newA.b.a === newA); //true
//# sourceMappingURL=test.js.map

1
dist/test.js.map vendored
View File

@ -1 +0,0 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,yCAAuC;AAEvC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC;AAC/B,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC;AAC/B,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AACR,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAER,IAAI,IAAI,GAAG,oBAAQ,CAAC,CAAC,CAAC,CAAC;AACvB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAK,MAAM;AAC1C,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAG,MAAM;AAC1C,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAK,MAAM"}

View File

@ -1,15 +1,18 @@
{ {
"name": "fast-deep-copy", "name": "fast-deep-copy",
"version": "1.0.0", "version": "1.0.1",
"description": "20 lines code! Deep copy the given object considering circular structure", "description": "20 lines code! Deep copy the given object considering circular structure",
"main": "dist/deep-copy.js", "main": "dist/deep-copy.js",
"types": "dist/deep-copy.d.ts",
"keywords": [ "keywords": [
"fast-deep-copy", "fast-deep-copy",
"deep-copy", "deep-copy",
"deepcopy" "deepcopy"
], ],
"scripts": { "scripts": {
"test": "node dist/test.js" "test": "node dist/test.js",
"build": "tsc",
"desc": "tsc --project ./tsconfig.desc.json"
}, },
"author": "625335512@qq.com", "author": "625335512@qq.com",
"homepage": "https://github.com/zhaolihang/fast-deep-copy", "homepage": "https://github.com/zhaolihang/fast-deep-copy",

View File

@ -1,5 +1,6 @@
# fast-deep-copy # fast-deep-copy
Only 20 lines code! no deps! Only 20 lines code! no deps!
It can correctly handle circular references!
## Useage: ## Useage:
``` ```

15
tsconfig.desc.json Normal file
View File

@ -0,0 +1,15 @@
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": false,
"declaration": true,
"allowJs": true,
"moduleResolution": "node",
"outDir": "dist"
},
"include": [
"src/**/*"
]
}

View File

@ -1,14 +1,14 @@
{ {
"compileOnSave": true, "compileOnSave": true,
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es5",
"sourceMap": true, "sourceMap": false,
"allowJs":true, "allowJs": true,
"moduleResolution": "node", "moduleResolution": "node",
"outDir": "dist" "outDir": "dist"
}, },
"include": [ "include": [
"src/**/*" "src/**/*"
] ]
} }