{"id":16817507,"url":"https://github.com/yuwu9145/nest-object-deep-copy","last_synced_at":"2025-06-29T06:33:43.402Z","repository":{"id":44099401,"uuid":"199140282","full_name":"yuwu9145/nest-object-deep-copy","owner":"yuwu9145","description":"A pure Javascript function doing real object hardcopy","archived":false,"fork":false,"pushed_at":"2023-03-01T16:40:08.000Z","size":376,"stargazers_count":10,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-23T05:51:25.011Z","etag":null,"topics":["hardcopy","javascript","nested-objects","prototype-chain","spread-operator"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/nest-object-deep-copy","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuwu9145.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-27T08:49:46.000Z","updated_at":"2023-03-07T08:14:08.000Z","dependencies_parsed_at":"2025-02-18T13:35:52.980Z","dependency_job_id":"3d26d71e-ce46-4ca9-b5a2-66219b6ff662","html_url":"https://github.com/yuwu9145/nest-object-deep-copy","commit_stats":null,"previous_names":["yuchaosydney/nest-object-deep-copy"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/yuwu9145/nest-object-deep-copy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuwu9145%2Fnest-object-deep-copy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuwu9145%2Fnest-object-deep-copy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuwu9145%2Fnest-object-deep-copy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuwu9145%2Fnest-object-deep-copy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuwu9145","download_url":"https://codeload.github.com/yuwu9145/nest-object-deep-copy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuwu9145%2Fnest-object-deep-copy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261423729,"owners_count":23156065,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hardcopy","javascript","nested-objects","prototype-chain","spread-operator"],"created_at":"2024-10-13T10:47:26.028Z","updated_at":"2025-06-29T06:33:43.377Z","avatar_url":"https://github.com/yuwu9145.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deep Copy Nested Objects \n\n![Coverage badge gree][coverage-badge-green]\n\nIf this project helps you, please support it with a star :heart: (Thank you!).\n\n[coverage-badge-green]: https://img.shields.io/badge/Coverage-100%25-brightgreen.svg\n[coverage-badge-yellow]: https://img.shields.io/badge/Coverage-100%25-yellow.svg\n[coverage-badge-red]: https://img.shields.io/badge/Coverage-100%25-red.svg\n\n## What\nThis is pure javascript function that aims to create a real hard copy from original javascript object.\n\n\n\n## Why\n* It is just a pure function and final import size is only **662 bytes**.\n* It gives you the **real hard copy**, avoids [limitations](#limitions-of-common-ways) of using **spread operator**, **Object.assign** and **JSON.parse(JSON.stringify(object))**. \n\n| Feature  | JSON.parse(JSON.stringify(object)) | spread operator / Object.assign | nest-object-deep-copy |\n| ------------- | ------------- | ------------- | ------------- |\n| [Hard Copy nested object](#cannot-make-hard-copy-on-nested-objects) | :heavy_check_mark:  | :x:  | :heavy_check_mark:  |\n| [Copy functions](#loosing-functions) | :x:  | :heavy_check_mark:  |  :heavy_check_mark:  |\n| [Keep prototype chain](#loosing-prototype-chain)  | :x:  | :x:  |  :heavy_check_mark:  |\n| Circular Reference  | Throw Error  | Keep Circular Ref  |  [Graceful Handle](#gracefully-handle-circular-reference)  |\n\n### Gracefully handle Circular Reference\n```javascript\nconst originalObject = { \n  a: 'test',\n  f: 1\n};\noriginalObject.a = originalObject;\noriginalObject.b = {};\noriginalObject.b.c = originalObject;\n```\n`deepCopy(originalObject)` will result in:\n\n```javascript\n{ \n  a: '[Circular]',\n  f: 1,\n  b: {\n    c: '[Circular]',\n  },\n};\n```\n## Limitions of common ways\n### Cannot make hard copy on nested objects\n\n```javascript\nlet user = {\n  id: 101,\n  gender: 'male',\n  personalInfo: {\n    name: 'Jack',\n  }\n};\n```\n\nThen the spread operator or Object.assign() **WILL NOT** give you a hard copied object:\n\n```javascript\nlet copiedUser = {\n  ...user\n};\n\n// Change a nested object value\ncopiedUser.personalInfo.name = 'Tom';\n\n// Change a property which holds a primitive value\ncopiedUser.id = 2;\n\n// original user object mutation happens\nconsole.log(user.personalInfo.name); // 'Tom'\nconsole.log(copiedUser.personalInfo.name); // 'Tom'\n\n// BUT mutation does not happen to property which holds a primitive value\nconsole.log(user.id); // 1\nconsole.log(copiedUser.id); // 2\n```\n\n\n### Loosing functions\n\n```javascript\nlet user = {\n  id: 1,\n  name: 'jack',\n  speak: function() {\n    console.log('I am speaking from original object.');\n  }\n};\n\nlet copiedUser = JSON.parse(JSON.stringify(user));\n\nuser.speak(); // `I am speaking from original object.`\ncopiedUser.speak(); //Uncaught TypeError: copiedUser.speak is not a function\n```\n\n### Loosing prototype chain\n\n```javascript\n// Declare a constructor function\nfunction Foo(who) {\n  this.me = who;\n}\n\n// 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\nFoo.prototype.identify = function() {\n  return 'I am ' + this.me;\n}\n\n// Create a user object by constructor Foo\nconst user = new Foo('Jack');\n\n// Create a copy object by using spread operator\nconst copiedUser1 = {...user};\n// Prototype chain is lost !!\ncopiedUser1.identify(); // Uncaught TypeError: copiedUser1.identify is not a function\n\n// Create a copy object by using Object.assign()\nconst copiedUser2 = Object.assign({}, user);\n// Prototype chain is lost !!\ncopiedUser2.identify(); // Uncaught TypeError: copiedUser2.identify is not a function\n\n// Create a copy object by using JSON.parse(JSON.stringify(object))\nconst copiedUser3 = JSON.parse(JSON.stringify(user));\n// Prototype chain is lost !!\ncopiedUser3.identify(); // Uncaught TypeError: copiedUser3.identify is not a function\n\n```\n\n## Install\n```console\n$ npm install nest-object-deep-copy\n```\n\n## How to use it\n```javascript\nconst deepCopy = require('./nest-object-hard-copy');\n\n//Get a hard copy\nlet copiedUser = deepCopy(object);\n\n```\n## License\n\nThis project is licensed under the MIT License \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuwu9145%2Fnest-object-deep-copy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuwu9145%2Fnest-object-deep-copy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuwu9145%2Fnest-object-deep-copy/lists"}