What is deepcopy?
The 'deepcopy' npm package is used to create deep copies of JavaScript objects. This means that it recursively copies all nested objects and arrays, ensuring that the new object is completely independent of the original.
What are deepcopy's main functionalities?
Deep Copy of Objects
This feature allows you to create a deep copy of an object, ensuring that nested objects are also copied.
const deepcopy = require('deepcopy');
const original = { a: 1, b: { c: 2 } };
const copy = deepcopy(original);
console.log(copy); // { a: 1, b: { c: 2 } }
Deep Copy of Arrays
This feature allows you to create a deep copy of an array, ensuring that nested arrays and objects are also copied.
const deepcopy = require('deepcopy');
const original = [1, [2, 3], { a: 4 }];
const copy = deepcopy(original);
console.log(copy); // [1, [2, 3], { a: 4 }]
Handling Circular References
This feature allows you to handle circular references in objects, ensuring that the deep copy process does not result in an infinite loop.
const deepcopy = require('deepcopy');
const original = { a: 1 };
original.b = original;
const copy = deepcopy(original);
console.log(copy); // { a: 1, b: [Circular] }
Other packages similar to deepcopy
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a `cloneDeep` function that performs deep copying similar to `deepcopy`. However, Lodash offers many additional utilities beyond deep copying.
rfdc
The 'rfdc' (Really Fast Deep Clone) package is designed for performance and provides a very fast way to deep clone objects. It is similar to `deepcopy` in functionality but is optimized for speed.
clone-deep
The 'clone-deep' package is another utility for deep cloning objects and arrays. It is similar to `deepcopy` but offers additional options for customizing the cloning process.
deepcopy.js
deep copy for any data
Installation
npm
$ npm install deepcopy
bower
$ bower install deepcopy
Usage
node.js
var deepcopy = require('deepcopy');
browser
<script src="deepcopy.min.js"></script>
var source = {
data: {
number: 123,
string: 'a',
boolean: true,
null: null,
undefined: undefined,
date: new Date,
regexp: /regexp/ig,
array: [
[ 123, 'abc', false ],
[ new Date, /node/i ],
{ array: [] }
],
object: {
number: 123, string: 'abc', boolean: false
},
to: undefined
}
};
source.data.to = source;
var shallow = source,
deep = deepcopy(source);
delete source.data;
console.dir(source);
console.dir(shallow);
console.log(
require('util').inspect(deep, {depth: null}));
Functions
deepcopy(target)
target
any types - target of copyreturn
any types - copied object from target
get deep copy of target.
return deep copy if target is Date, RegExp or primitive types.
return shallow copy if target is function.
do recursive copy if target is Array or Object.
also can copy if target has circular reference.
Test
test for node.js
$ npm install
$ npm test
test for browser
$ npm install
$ npm run-script bower
$ npm run-script testem
Contributors
License
The MIT License. Please see LICENSE file.