npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

clone-deep-circular-references

v2.0.0

Published

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

Downloads

206

Readme

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

Related projects

You might also be interested in these projects:

License

MIT