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

Simple deno module to do simple and fast deep cloning.

Table of contents

Usage

/** Import from GH via `denopkg` */
import { deepClone } from "https://cdn.jsdelivr.net/gh/motss/[email protected]/deep_clone/mod.ts";

(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