Skip to content

Commit

Permalink
Works now with Arrays! 🌈
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Feb 25, 2019
1 parent feae96b commit f40f0d1
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 25 deletions.
49 changes: 32 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!
Expand All @@ -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) => {
Expand Down
4 changes: 3 additions & 1 deletion dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
60 changes: 60 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f40f0d1

Please sign in to comment.