From 036a7c847053da5bb30bb420dcb2f375bf0f6657 Mon Sep 17 00:00:00 2001 From: zlh <625335512@qq.com> Date: Sat, 23 Sep 2017 15:09:50 +0800 Subject: [PATCH] init --- dist/deep-copy.js | 47 +++++++++++++++++++++++++++++++++++++++++++ dist/deep-copy.js.map | 1 + package.json | 11 ++++++++++ readme.md | 3 +++ src/deep-copy.ts | 47 +++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 14 +++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 dist/deep-copy.js create mode 100644 dist/deep-copy.js.map create mode 100644 package.json create mode 100644 readme.md create mode 100644 src/deep-copy.ts create mode 100644 tsconfig.json diff --git a/dist/deep-copy.js b/dist/deep-copy.js new file mode 100644 index 0000000..48464aa --- /dev/null +++ b/dist/deep-copy.js @@ -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} 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 \ No newline at end of file diff --git a/dist/deep-copy.js.map b/dist/deep-copy.js.map new file mode 100644 index 0000000..419fb82 --- /dev/null +++ b/dist/deep-copy.js.map @@ -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"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..802b374 --- /dev/null +++ b/package.json @@ -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" +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..25f7ef7 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +```js + import { deepCopy } from 'fast-deep-copy' +``` \ No newline at end of file diff --git a/src/deep-copy.ts b/src/deep-copy.ts new file mode 100644 index 0000000..05efc9f --- /dev/null +++ b/src/deep-copy.ts @@ -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} cache + * @return {*} + */ +export function deepCopy(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 copy; +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7a83bd5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ + { + "compileOnSave": true, + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "sourceMap": true, + "allowJs":true, + "moduleResolution": "node", + "outDir": "dist" + }, + "include": [ + "src/**/*" + ] + } \ No newline at end of file