copy-anything
Advanced tools
Comparing version 1.1.1 to 1.2.1
@@ -6,3 +6,3 @@ 'use strict'; | ||
/** | ||
* 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. | ||
* | ||
@@ -14,2 +14,4 @@ * @export | ||
function copy(target) { | ||
if (isWhat.isArray(target)) | ||
return target.map(function (i) { return copy(i); }); | ||
if (!isWhat.isPlainObject(target)) | ||
@@ -16,0 +18,0 @@ return target; |
@@ -1,5 +0,5 @@ | ||
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. | ||
* | ||
@@ -11,2 +11,4 @@ * @export | ||
function copy(target) { | ||
if (isArray(target)) | ||
return target.map(function (i) { return copy(i); }); | ||
if (!isPlainObject(target)) | ||
@@ -13,0 +15,0 @@ return target; |
{ | ||
"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", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
# 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** ‼️ | ||
@@ -34,14 +36,33 @@ | ||
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 | ||
@@ -51,13 +72,7 @@ | ||
```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 | ||
@@ -64,0 +79,0 @@ return Object.keys(target) |
@@ -1,5 +0,5 @@ | ||
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. | ||
* | ||
@@ -11,2 +11,3 @@ * @export | ||
export default function copy (target: any): any { | ||
if (isArray(target)) return target.map(i => copy(i)) | ||
if (!isPlainObject(target)) return target | ||
@@ -13,0 +14,0 @@ return Object.keys(target) |
@@ -33,2 +33,62 @@ import test from 'ava' | ||
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 => { | ||
@@ -35,0 +95,0 @@ let res, target |
/** | ||
* 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. | ||
* | ||
@@ -4,0 +4,0 @@ * @export |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12306
239
85