Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
clone | 1.0.4 | 4.35 kB | MIT | prod |
Clone is a npm package that provides foolproof deep cloning of JavaScript objects, arrays, numbers, strings, and other data types. It is capable of cloning simple objects (even those with custom prototypes), Date objects, and RegExp objects in a recursive manner. This means that even if you have complicated structures like arrays within objects containing dates, they would be cloned without any issues. If you're certain that your dataset does not contain circular references, you can even optimize clone's performance by using the circular
setting.
Using the clone package is straightforward. To begin with, you need to install the package using npm with the following command: npm install clone
. Once installed, you simply require the package in your JavaScript file. A simple usage example is demonstrated below:
var clone = require('clone');
var a, b;
a = { foo: { bar: 'baz' } }; // initial value of a
b = clone(a); // clone a to b
a.foo.bar = 'foo'; // change a
console.log(a); // show a
console.log(b); // show b
In the above code, we're creating an object a
and cloning it into b
using the clone function. Despite changes to object a
after cloning, b
remains unaffected, demonstrating the deep copy functionality.
Also note, clone supports circular references, where an object references itself:
var a, b;
a = { hello: 'world' };
a.myself = a;
b = clone(a);
console.log(b);
In this case, b.myself
points to b
, not a
.
The documentation of the clone package can be found within the README file in the package's GitHub repository. The repository link is https://github.com/pvorb/node-clone. The README provides details on installation, examples on how to use clone, information on the package API, tips for handling circular references, and caveats for using the package. Please keep in mind that certain special objects (like a socket or process.stdout
/stderr
) are known to be uncloneable. It's always recommended to check the documentation for latest updates and issues.