diff --git a/README.md b/README.md index 268db4f..54daf0a 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ # Copy anything 🎭 -An optimised way to copy'ing an object. A small and simple integration. +An optimised way to copy'ing (cloning) an object or array. A small and simple integration. ## Motivation -I created this package because I tried a lot of similar packages that do copy'ing/cloning of objects. But all had its quirks, and *all of them break things they are not supposed to break*... 😞 +I created this package because I tried a lot of similar packages that do copy'ing/cloning. But all had its quirks, and *all of them break things they are not supposed to break*... 😞 I was looking for: -- a simple copy function like `JSON.parse(JSON.stringify(object))` +- a simple copy/clone function +- has to be fast! - props must lose any reference to original object +- works with arrays and objects in arrays! - **does not break special class instances** ‼️ This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to copy them inproperly. So we gotta be careful! @@ -31,33 +33,46 @@ copy-anything will copy objects and nested properties, but only as long as they' ```js import copy from 'copy-anything' -const target = {name: 'Ditto', type: {water: true}} -const copy = copy(target) +const original = {name: 'Ditto', type: {water: true}} +const copy = copy(original) // now if we change a nested prop like the type: copy.type.water = false -copy.type.fire = true +copy.type.fire = true // new prop // then the original object will still be the same: -// target.type.water === true -// target.type.fire === undefined +(original.type.water === true) +(original.type.fire === undefined) +``` + +## Works with arrays + +It will also clone arrays, **as well as objects inside arrays!** 😉 + +```js +import copy from 'copy-anything' + +const original = [{name: 'Squirtle'}] +const copy = copy(original) + +// now if we change a prop in the array like so: +copy[0].name = 'Wartortle' +copy.push({name: 'Charmander'}) // new item + +// then the original array will still be the same: +(original[0].name === 'Squirtle') +(original[1] === undefined) ``` ## Source code The source code is literally just these lines. Most of the magic comes from the isPlainObject function from the [is-what library](https://github.com/mesqueeb/is-what). -```TypeScript +```JavaScript import { isPlainObject } from 'is-what' -/** - * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. - * - * @export - * @param {*} target Target can be anything - * @returns {*} the target with replaced values - */ -export default function copy (target: any): any { +export default function copy (target) { + if (isArray(target)) return target.map(i => copy(i)) if (!isPlainObject(target)) return target return Object.keys(target) .reduce((carry, key) => { diff --git a/dist/index.cjs.js b/dist/index.cjs.js index 3968b01..42e4ee1 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -3,13 +3,15 @@ var isWhat = require('is-what'); /** - * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. + * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked. * * @export * @param {*} target Target can be anything * @returns {*} the target with replaced values */ function copy(target) { + if (isWhat.isArray(target)) + return target.map(function (i) { return copy(i); }); if (!isWhat.isPlainObject(target)) return target; return Object.keys(target) diff --git a/dist/index.esm.js b/dist/index.esm.js index aaa006d..94d0aef 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -1,13 +1,15 @@ -import { isPlainObject } from 'is-what'; +import { isArray, isPlainObject } from 'is-what'; /** - * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. + * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked. * * @export * @param {*} target Target can be anything * @returns {*} the target with replaced values */ function copy(target) { + if (isArray(target)) + return target.map(function (i) { return copy(i); }); if (!isPlainObject(target)) return target; return Object.keys(target) diff --git a/package-lock.json b/package-lock.json index 09cc00e..6305127 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "copy-anything", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 165f232..0853d10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "copy-anything", - "version": "1.1.1", + "version": "1.2.1", "description": "An optimised way to copy'ing an object. A small and simple integration", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/src/index.ts b/src/index.ts index a8bdb08..6dd29ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,14 @@ -import { isPlainObject } from 'is-what' +import { isPlainObject, isArray } from 'is-what' /** - * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. + * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked. * * @export * @param {*} target Target can be anything * @returns {*} the target with replaced values */ export default function copy (target: any): any { + if (isArray(target)) return target.map(i => copy(i)) if (!isPlainObject(target)) return target return Object.keys(target) .reduce((carry, key) => { diff --git a/test/index.js b/test/index.js index 1d2da18..9488403 100644 --- a/test/index.js +++ b/test/index.js @@ -30,6 +30,66 @@ test('copy', t => { t.is(target.c.e, undefined) }) +test('Arrays in objects', t => { + let res, target + target = {a: [1, 2], c: {d: ['a']}} + res = copy(target) + t.deepEqual(res, target) + // change target + target.a.push(3) + t.deepEqual(target.a, [1, 2, 3]) + t.deepEqual(res.a, [1, 2]) + target.c.d.splice(0, 0, 'z') + t.deepEqual(target.c.d, ['z', 'a']) + t.deepEqual(res.c.d, ['a']) + // reset test + target = {a: [1, 2], c: {d: ['a']}} + res = copy(target) + t.deepEqual(res, target) + // change res + res.a.push(3) + t.deepEqual(res.a, [1, 2, 3]) + t.deepEqual(target.a, [1, 2]) + res.c.d.splice(0, 0, 'z') + t.deepEqual(res.c.d, ['z', 'a']) + t.deepEqual(target.c.d, ['a']) +}) + +test('Arrays with objects in objects', t => { + let res, target + target = {a: [{a: 1}], c: {d: [{b: 1}]}} + res = copy(target) + t.deepEqual(res, target) + // change target + target.a[0].a = 2 + t.deepEqual(target.a, [{a: 2}]) + t.deepEqual(res.a, [{a: 1}]) + target.c.d[0].b = 2 + t.deepEqual(target.c.d, [{b: 2}]) + t.deepEqual(res.c.d, [{b: 1}]) + // reset test + target = {a: [{a: 1}], c: {d: [{b: 1}]}} + res = copy(target) + t.deepEqual(res, target) + // change res + res.a[0].a = 2 + t.deepEqual(res.a, [{a: 2}]) + t.deepEqual(target.a, [{a: 1}]) + res.c.d[0].b = 2 + t.deepEqual(res.c.d, [{b: 2}]) + t.deepEqual(target.c.d, [{b: 1}]) +}) + +test('Arrays', t => { + let res, target + target = [1, 2, 3, 4] + res = copy(target) + t.deepEqual(res, target) + res.splice(0, 0, 0) + t.deepEqual(target, [1, 2, 3, 4]) + t.deepEqual(res, [0, 1, 2, 3, 4]) +}) + test('non objects', t => { let res, target target = 'ha' diff --git a/types/index.d.ts b/types/index.d.ts index 281fe8c..906b788 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,5 @@ /** - * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. + * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked. * * @export * @param {*} target Target can be anything