node package manager

deepcopy

deep copy for any data

deepcopy.js

deep copy for any data

REPL powered by Tonic

$ npm install deepcopy
var deepcopy = require("deepcopy");
<script src="deepcopy.min.js"></script>

basic usage:

var base, copy;
 
base = {
  desserts: [
    { name: "cake"      },
    { name: "ice cream" },
    { name: "pudding"   }
  ]
};
 
copy = deepcopy(base);
base.desserts = null;
 
console.log(base);
// { desserts: null } 
console.log(copy);
// { desserts: [ { name: 'cake' }, { name: 'ice cream' }, { name: 'pudding' } ] } 

customize deepcopy:

function MyClass(id) {
  this._id = id;
}
 
var base, copy;
 
base = {
  myClasses: [
    new MyClass(1),
    new MyClass(2),
    new MyClass(3)
  ]
};
 
copy = deepcopy(base, function(target) {
  if (target.constructor === MyClass) {
    return new MyClass(target._id);
  }
});
base.myClasses = null;
 
console.log(base);
// { myClasses: null } 
console.log(copy);
// { myClasses: [ MyClass { _id: 1 }, MyClass { _id: 2 }, MyClass { _id: 3 } ] } 
  • value
    • * - target value
  • customizer
    • Function - customize function
  • return
    • * - copied value

support types are below:

  • Number
  • String
  • Boolean
  • Null
  • Undefined
  • Function
    • shallow copy if it is native function
  • Date
  • RegExp
  • Array
    • support recursive copy
    • also can copy if it has circular reference
  • Object
    • support recursive copy
    • also can copy if it has circular reference
  • Buffer (node.js only)
  • Symbol
$ npm install
$ npm test

The MIT license. Please see LICENSE file.