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 data
Installation
$ npm install deepcopy
Usage
node.js
via ES Modules
import deepcopy from 'deepcopy';
via CommonJS
const deepcopy = require('deepcopy');
browser
via module
<script type="module">
import deepcopy from "./dist/deepcopy.min.mjs";
</script>
via script
<script src="./dist/deepcopy.min.js"></script>
Example
basic usage:
const src = {
desserts: [
{ name: 'cake' },
{ name: 'ice cream' },
{ name: 'pudding' }
]
};
const dist = deepcopy(src);
src.desserts = null;
console.log(src);
console.log(dist);
customize deepcopy:
function MyClass(id) {
this._id = id;
}
const src = {
myClasses: [
new MyClass(1),
new MyClass(2),
new MyClass(3)
]
};
const dest = deepcopy(base, {
customizer(value) {
if (target.constructor === MyClass) {
return new MyClass(target._id);
}
}
});
src.myClasses = null;
console.log(src);
console.log(dest);
Explanation of build files
- deepcopy.mjs
- library bundled
- ES Modules
- deepcopy.min.mjs
- deepcopy.js
- library bundled
- UMD builds
- deepcopy.min.js
- deepcopy.legacy.js
- deepcopy.legacy.min.js
- minified deepcopy.legacy.js
scripts are put in dist
directory.
Functions
deepcopy(value[, options])
value
options
Object|Function
Object
- pass optionsFunction
- use as customize function
return
Supported types and copy operation
type | operation | |
---|
ArrayBuffer | deep copy | |
Boolean | deep copy | |
Buffer | deep copy | node.js only |
DataView | deep copy | |
Date | deep copy | |
Number | deep copy | |
RegExp | deep copy | |
String | deep copy | |
Float32Array | deep copy | |
Float64Array | deep copy | |
Int16Array | deep copy | |
Int32Array | deep copy | |
Int8Array | deep copy | |
Uint16Array | deep copy | |
Uint32Array | deep copy | |
Uint8Array | deep copy | |
Uint8ClampedArray | deep copy | |
boolean | deep copy | |
null | deep copy | |
number | deep copy | |
string | deep copy | |
symbol | deep copy | |
undefined | deep copy | |
Arguments | deep copy | recursively, copy as Array |
Array | deep copy | recursively |
Map | deep copy | recursively |
Object | deep copy | recursively |
Set | deep copy | recursively |
Array Iterator | shallow copy | |
Map Iterator | shallow copy | |
Promise | shallow copy | |
Set Iterator | shallow copy | |
String Iterator | shallow copy | |
function | shallow copy | |
global | shallow copy | window, global, self, etc. |
WeakMap | shallow copy | |
WeakSet | shallow copy | |
Test
$ npm install
$ npm test
Contributors
License
The MIT license.