Socket
Socket
Sign inDemoInstall

clone-deep-circular-references

Package Overview
Dependencies
2
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    clone-deep-circular-references

Shallow and recursively (deep) clone JavaScript objects that handles circular references


Version published
Weekly downloads
60
decreased by-27.71%
Maintainers
1
Install size
46.0 kB
Created
Weekly downloads
 

Readme

Source

clone-deep-circular-references

npm travis Coverage Status minified size minified & gziped size

Shallow object colone and recursively (deep) clone JavaScript objects that handles circular references.

This is a reimplementation of the package clone-deep to allow have circular references; as well, a reimplementation of shallow-clone to make it friendly with webpack.

Install

Install with npm:

$ npm install --save clone-deep-circular-references

Basic usage

In this way you need to call the cloneDeep or the cloneShallow function passing as first argument the value to clone.

import { cloneDeep, cloneShallow } from 'clone-deep-circular-references';

let obj = { a: 'b' };
let arr = [obj];
let deepCopy = cloneDeep(arr);
let shallowCopy = cloneShallow(arr);
obj.c = 'd';
shallowCopy.push('hello');

console.log(deepCopy);
//=> [{ a: 'b' }]

console.log(arr);
//=> [{ a: 'b', c: 'd' }]

console.log(shallowCopy);
//=> [{ a: 'b', c: 'd' }, 'hello']

Cloning custom classes

By default, if a no plain object is found, no copy will be created; if you want to change it, you need to specify true as second argument. Note: in this way the class's constructor will not be called (a new object with the same prototype will be created), and then each property will be set.

import { cloneDeep, cloneShallow } from 'clone-deep-circular-references';

class MyClass {
    constructor(value) {
        this.prop = value;
    }
}
let obj = { a: new MyClass('b') };

let deepCopy = cloneDeep(arr);
console.log(obj === deepCopy);
//=> true
console.log(obj.a === deepCopy.a);
//=> true

deepCopy = cloneDeep(arr, true);
console.log(obj === deepCopy);
//=> false
console.log(obj.a === deepCopy.a);
//=> false

let shallowCopy = cloneShallow(arr);
console.log(obj === shallowCopy);
//=> true
console.log(obj.a === shallowCopy.a);
//=> true

shallowCopy = cloneShallow(arr, true);
console.log(obj === shallowCopy);
//=> false
console.log(obj.a === shallowCopy.a);
//=> true

Controlling how custom classes are cloned

If you want to control how how no plain objects are copied you can specify a function as second argument that copy the value; this function receives as first argument the value to clone and as second argument the a Map with the object already cloned.

Note: if inside the cloneDeep function you want to call the cloneDeep function you need to pass a third argument the map with the object already cloned.

import { cloneDeep, cloneShallow } from 'clone-deep-circular-references';

class MyClass {
    constructor(value) {
        this.prop = value;
    }
}
let obj = { a: new MyClass('b') };

let deepCopy = cloneDeep(arr, function myCustomClone(value, instancesMap) {
    if (value instanceof MyClass) {
        return new MyClass(value.prop)
    }
    return cloneDeep(value, myCustomClone, instancesMap);
});

console.log(obj === deepCopy);
//=> false

console.log(obj.a === deepCopy.a);
//=> false

let shallowCopy = cloneShallow(arr, function myCustomClone(value) {
    if (value instanceof MyClass) {
        return new MyClass(value.prop)
    }
    return cloneShallow(value, myCustomClone);
});

console.log(obj === shallowCopy);
//=> false

console.log(obj.a === shallowCopy.a);
//=> true

Handled types

The following types are handled recursively by the cloneDeep function:

  • Plain objects
  • No plain objects (see the previous documentation)
  • Arrays
  • Map
  • Set

The other object types are copied using the cloneShallowfunction.

The following types are handled by the cloneShallow function:

  • Plain objects
  • No plain objects (see the previous documentation)
  • Arrays
  • Map
  • Set
  • Date
  • RegExp
  • Error

The following types are returned without create a copy:

  • Symbol
  • Promise
  • WeakMap
  • WeakSet
  • Buffer (nodejs)
  • ArrayBuffer
  • Int8Array
  • Uint8Array
  • Uint8ClampedArray
  • Int16Array
  • Uint16Array
  • Int32Array
  • Uint32Array
  • Float32Array
  • Float64Array
  • undefined
  • null
  • boolean
  • string
  • number
  • function
  • iterator
  • any other special JavaScript object

You might also be interested in these projects:

License

MIT

Keywords

FAQs

Last updated on 23 Aug 2020

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc