🚨 No longer maintained. Moved to @reallyland/node_mod. 🚨
A simple NPM package to do simple and fast deep cloning with
JSON.parse
+JSON.stringify
.
# Install via NPM
$ npm install --save deep.clone
/** Import project dependencies */
import deepClone from 'deep.clone';
/** Setting up */
const simpleObject = {
a: {
b: { c: [1, 2,3] },
e: [ { f: null } ],
},
d: 'deep',
};
const complexObject = {
a: () => {},
b: /test/gi,
c: [1, 2],
d: new Date(),
e: { f: 111 },
};
(async () => {
const clonedSimpleObject = await deepClone(simpleObject);
const clonedComplexObject = await deepClone(compleObject, {
absolute: true,
});
})();
/** Import project dependencies */
const { deepClone } = require('deep.clone');
/** Setting up */
const simpleObject = {
a: {
b: { c: [1, 2,3] },
e: [ { f: null } ],
},
d: 'deep',
};
const complexObject = {
a: () => {},
b: /test/gi,
c: [1, 2],
d: new Date(),
e: { f: 111 },
};
(async () => {
const clonedSimpleObject = await deepClone(simpleObject);
const clonedComplexObject = await deepClone(compleObject, {
absolute: true,
});
})();
<script type="module">
import { deepClone } from 'https://unpkg.com/deep.clone@latest/dist/deep.clone.js';
deepClone({ ... }) /** Object truncated for brevity */
.then(console.log);
.then(console.error);
</script>
<script src="https://unpkg.com/deep.clone@latest/dist/deep.clone.iife.js"></script>
<script>
const { deepClone } = window.DeepClone;
deepClone({ ... }) /** Object truncated for brevity */
.then(console.log);
.then(console.error);
</script>
👉 Check out the deno module at deno_mod/deep_clone.
target
<T
> Target to be cloned.options
<?Object> Optionally setabsolute: true
for deep cloning complex objects that are not possible withJSON.parse
+JSON.stringify
.absolute
<boolean> If true, deep clone complex objects.
- returns: <Promise<
T
>> Promise which resolves with the deeply cloned target.
This method deeply clones a given target with JSON.parse
+ JSON.stringify
asynchronously by default. Set absolute: true
for deep cloning complex objects that contain Date, RegExp, Function, etc.
This methods works the same as deepClone(target[, options])
except that this is the synchronous version.
MIT License © Rong Sen Ng