This commit is contained in:
zlh 2017-09-23 15:09:50 +08:00
commit 036a7c8470
6 changed files with 123 additions and 0 deletions

47
dist/deep-copy.js vendored Normal file
View File

@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** from https://github.com/vuejs/vuex/blob/dev/src/util.js
* Get the first item that pass the test
* by second argument function
*
* @param {Array} list
* @param {Function} f
* @return {*}
*/
function find(list, f) {
return list.filter(f)[0];
}
/**
* 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 {*}
*/
function deepCopy(obj, cache) {
if (cache === void 0) { cache = []; }
// just return if obj is immutable value
if (obj === null || typeof obj !== 'object') {
return obj;
}
// if obj is hit, it is in circular structure
var hit = find(cache, function (c) { return c.original === obj; });
if (hit) {
return hit.copy;
}
var copy = Array.isArray(obj) ? [] : {};
// put the copy into cache at first
// because we want to refer it in recursive deepCopy
cache.push({
original: obj,
copy: copy
});
Object.keys(obj).forEach(function (key) {
copy[key] = deepCopy(obj[key], cache);
});
return copy;
}
exports.deepCopy = deepCopy;
//# sourceMappingURL=deep-copy.js.map

1
dist/deep-copy.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"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,CAAA;AAC5B,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,CAAA;IACd,CAAC;IAED,6CAA6C;IAC7C,IAAM,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,QAAQ,KAAK,GAAG,EAAlB,CAAkB,CAAC,CAAA;IAChD,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACN,MAAM,CAAC,GAAG,CAAC,IAAI,CAAA;IACnB,CAAC;IAED,IAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAA;IACzC,mCAAmC;IACnC,oDAAoD;IACpD,KAAK,CAAC,IAAI,CAAC;QACP,QAAQ,EAAE,GAAG;QACb,IAAI,MAAA;KACP,CAAC,CAAA;IAEF,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,CAAA;IACzC,CAAC,CAAC,CAAA;IAEF,MAAM,CAAI,IAAI,CAAC;AACnB,CAAC;AAzBD,4BAyBC"}

11
package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "fast-deep-copy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

3
readme.md Normal file
View File

@ -0,0 +1,3 @@
```js
import { deepCopy } from 'fast-deep-copy'
```

47
src/deep-copy.ts Normal file
View File

@ -0,0 +1,47 @@
/** from https://github.com/vuejs/vuex/blob/dev/src/util.js
* Get the first item that pass the test
* by second argument function
*
* @param {Array} list
* @param {Function} f
* @return {*}
*/
function find(list, f) {
return list.filter(f)[0]
}
/**
* 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 function deepCopy<T>(obj: T, cache = []): T {
// just return if obj is immutable value
if (obj === null || typeof obj !== 'object') {
return obj
}
// if obj is hit, it is in circular structure
const hit = find(cache, c => c.original === obj)
if (hit) {
return hit.copy
}
const copy = Array.isArray(obj) ? [] : {}
// put the copy into cache at first
// because we want to refer it in recursive deepCopy
cache.push({
original: obj,
copy
})
Object.keys(obj).forEach(key => {
copy[key] = deepCopy(obj[key], cache)
})
return <T>copy;
}

14
tsconfig.json Normal file
View File

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