@emahuni/clone-deep
v5.1.3
Published
Like the original clone-deep, it will recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives, setters, getters and circular objects cloning. Clones object property descriptors info as well.
Downloads
14
Maintainers
Readme
@emahuni/clone-deep
Like the original clone-deep, it will recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives, setters, getters and circular objects cloning. Clones object property descriptors info as well.
Please consider following this project's author, Emmanuel Mahuni, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save @emahuni/clone-deep
Usage
const cloneDeep = require('@emahuni/clone-deep');
let obj = { a: 'b' };
let arr = [obj];
let copy = cloneDeep(arr);
obj.c = 'd';
console.log(copy);
//=> [{ a: 'b' }]
console.log(arr);
//=> [{ a: 'b', c: 'd' }]
obj.e = obj;
copy = cloneDeep(arr);
console.log(copy);
//=> [{ a: 'b', c: 'd', e: {...} }] // handles circular arrays and objects cloning
Heads up!
The last argument specifies whether to clone instances (objects that are from a custom class or are not created by the Object
constructor. This value may be true
or the function use for cloning instances.
When an instanceClone
function is provided, it will be invoked to clone objects that are not "plain" objects (as defined by isPlainObjectisPlainObject
). If instanceClone
is not specified, this library will not attempt to clone non-plain objects, and will simply copy the object reference.
Attribution
Based on jonschlinkert's that was... initially based on mout's implementation of deepClone.
About
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Contributors
| Commits | Contributor |
| -- | --- |
| 24 | emahuni |
| 46 | jonschlinkert |
| 2 | yujunlong2000 |
Author
Emmanuel Mahuni
License
Copyright © 2022, Emmanuel Mahuni. Released under the MIT License.