Skip to content

Latest commit

 

History

History

deep-clone

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

deep-clone

Simple and fast deep cloning


MIT License

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.

Table of contents

Usage

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 });
})();

API Reference

deepClone<T>(target[, options])

  • target <T> Target to be cloned.
  • options <?Object> Optionally set absolute: true for deep cloning complex objects that are not possible with JSON.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.

deepCloneSync(target[, options])

This methods works the same as deepClone(target[, options]) except that this is the synchronous version.

License

MIT License © Rong Sen Ng