šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Book a DemoInstallSign in
Socket

deep.clone

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep.clone

Simple package to deeply clone any objects with nested objects

2.1.2
latest
Source
npm
Version published
Weekly downloads
4.1K
-59.36%
Maintainers
2
Weekly downloads
Ā 
Created
Source

deep.clone

Simple and fast deep cloning

Follow me

Version Node version MIT License

Downloads Total downloads Packagephobia Bundlephobia

CircleCI Dependency Status codecov Coverage Status

codebeat badge Codacy Badge Code of Conduct

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

Table of contents

Pre-requisite

Install

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

Usage

TypeScript or ES Modules

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

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

Browser

ES Modules

<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>

IIFE

<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>

deno

šŸ‘‰ Check out the deno module at deno_mod/deep_clone.

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

Keywords

clone

FAQs

Package last updated on 28 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts