clone-deep
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
is-plain-object | 2.0.4 | 2.96 kB | MIT | prod | |
kind-of | 6.0.3 | 7.47 kB | MIT | prod | |
shallow-clone | 3.0.1 | 3.58 kB | MIT | prod |
Clone-deep is a highly useful library in JavaScript that can recursively deep clone JavaScript native types. This includes Objects, Arrays, RegExps, Dates, as well as primitive types. The ability to deeply copy these types, rather than just merely referencing them, becomes extremely beneficial in ensuring data sanctity in your code and preventing any unseen side effects.
To use clone-deep, you first need to install the module from npm using the command: npm install --save clone-deep
. Once installed, you can import clone-deep into your JavaScript file using const cloneDeep = require('clone-deep');
.
Here is a practical example of usage:
const cloneDeep = require('clone-deep');
let obj = { a: 'b' };
let arr = [obj];
let copy = cloneDeep(arr);
obj.c = 'd';
console.log(copy); // Outputs: [{ a: 'b' }]
console.log(arr); // Outputs: [{ a: 'b', c: 'd' }]
In this example, despite modifying the original object (obj) after cloning the array containing it, the cloned array (copy
) remains unaffected because it holds a deep clone of the original object.
The complete documentation for clone-deep can be found in its README file on its GitHub repository located at: https://github.com/jonschlinkert/clone-deep. The README includes a comprehensive guide on how to install, use, and test the library, as well as other relevant details such as its license and contributing guidelines.