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

nest-object-deep-copy

v1.0.13

Published

![Coverage badge gree][coverage-badge-green]

Downloads

68

Readme

Deep Copy Nested Objects

Coverage badge gree

If this project helps you, please support it with a star :heart: (Thank you!).

What

This is pure javascript function that aims to create a real hard copy from original javascript object.

Why

  • It is just a pure function and final import size is only 662 bytes.
  • It gives you the real hard copy, avoids limitations of using spread operator, Object.assign and JSON.parse(JSON.stringify(object)).

| Feature | JSON.parse(JSON.stringify(object)) | spread operator / Object.assign | nest-object-deep-copy | | ------------- | ------------- | ------------- | ------------- | | Hard Copy nested object | :heavy_check_mark: | :x: | :heavy_check_mark: | | Copy functions | :x: | :heavy_check_mark: | :heavy_check_mark: | | Keep prototype chain | :x: | :x: | :heavy_check_mark: | | Circular Reference | Throw Error | Keep Circular Ref | Graceful Handle |

Gracefully handle Circular Reference

const originalObject = { 
  a: 'test',
  f: 1
};
originalObject.a = originalObject;
originalObject.b = {};
originalObject.b.c = originalObject;

deepCopy(originalObject) will result in:

{ 
  a: '[Circular]',
  f: 1,
  b: {
    c: '[Circular]',
  },
};

Limitions of common ways

Cannot make hard copy on nested objects

let user = {
  id: 101,
  gender: 'male',
  personalInfo: {
    name: 'Jack',
  }
};

Then the spread operator or Object.assign() WILL NOT give you a hard copied object:

let copiedUser = {
  ...user
};

// Change a nested object value
copiedUser.personalInfo.name = 'Tom';

// Change a property which holds a primitive value
copiedUser.id = 2;

// original user object mutation happens
console.log(user.personalInfo.name); // 'Tom'
console.log(copiedUser.personalInfo.name); // 'Tom'

// BUT mutation does not happen to property which holds a primitive value
console.log(user.id); // 1
console.log(copiedUser.id); // 2

Loosing functions

let user = {
  id: 1,
  name: 'jack',
  speak: function() {
    console.log('I am speaking from original object.');
  }
};

let copiedUser = JSON.parse(JSON.stringify(user));

user.speak(); // `I am speaking from original object.`
copiedUser.speak(); //Uncaught TypeError: copiedUser.speak is not a function

Loosing prototype chain

// Declare a constructor function
function Foo(who) {
  this.me = who;
}

// Now there is a new property called 'identify' which equals to a function in prototype chain of any object being created by calling new Foo
Foo.prototype.identify = function() {
  return 'I am ' + this.me;
}

// Create a user object by constructor Foo
const user = new Foo('Jack');

// Create a copy object by using spread operator
const copiedUser1 = {...user};
// Prototype chain is lost !!
copiedUser1.identify(); // Uncaught TypeError: copiedUser1.identify is not a function

// Create a copy object by using Object.assign()
const copiedUser2 = Object.assign({}, user);
// Prototype chain is lost !!
copiedUser2.identify(); // Uncaught TypeError: copiedUser2.identify is not a function

// Create a copy object by using JSON.parse(JSON.stringify(object))
const copiedUser3 = JSON.parse(JSON.stringify(user));
// Prototype chain is lost !!
copiedUser3.identify(); // Uncaught TypeError: copiedUser3.identify is not a function

Install

$ npm install nest-object-deep-copy

How to use it

const deepCopy = require('./nest-object-hard-copy');

//Get a hard copy
let copiedUser = deepCopy(object);

License

This project is licensed under the MIT License