A wrapper around
JSON.parse
+JSON.stringify
for fast deep clone for object with primitive values. Also support deep clone for complex objects via optional flag.
import { deepClone } from 'nodemod/dist/deep-clone/index.js';
(async () => {
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 }
};
await deepClone(simpleObject);
await deepClone(complexObject, { absolute: true });
})();
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