Skip to content
This repository was archived by the owner on Jun 23, 2019. It is now read-only.

motss/deep.clone

Repository files navigation

deep.clone

Simple and fast deep cloning


NPM

Build Status Version Downloads MIT License NSP Status Dependency Status

Code of Conduct

Inline docs

Stories in Ready

A simple NPM package to do simple and fast deep cloning with JSON.parse + JSON.stringify.

Table of contents

Pre-requisite

Setup

Install

# Install via NPM
$ npm install --save deep.clone

Usage

Node.js

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

Native ES Modules or Typescript

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

API Reference

deepClone(target[, options])

  • target <any> Target to be cloned.
  • options <Object> Set absolute: true for deep cloning complex objects that are not possible with JSON.parse + JSON.stringify.
    • absolute <boolean> True if deep cloning complex objects.
  • returns: <Promise<any>> 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])

  • target <any> Target to be cloned.
  • options <Object> Set absolute: true for deep cloning complex objects that are not possible with JSON.parse + JSON.stringify.
    • absolute <boolean> True if deep cloning complex objects.
  • returns: <Promise<any>> Promise which resolves with the deeply cloned target.

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

License

MIT License © Rong Sen Ng

About

Deeply clone any objects with nested objects.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •