deepcopy

deep copy for any data

deepcopy.js

deep copy for any data

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

define deepcopy by define() if using AMD loader.

otherwise deepcopy export to global.

var data, shallow, deep;
 
data = {
  objects: {
    array: [
      null, , new Date, /deepcopy/ig
    ],
    object: {
      number: NaN,
      string: 'A',
      boolean: true
    },
    to: null
  }
};
 
// circular reference 
data.objects.to = data;
 
// shallow copy and deep copy 
shallow = data;
deep = deepcopy(data);
 
// remove entry 
delete data.objects;
 
// results 
console.log(data);
// {} 
console.log(shallow);
// {} 
console.log(require('util').inspect(deep, { depth: null }));
// { objects: 
//    { array: 
//       [ null, 
//         undefined, 
//         Sat Jan 10 2015 03:18:32 GMT+0900 (JST), 
//         /deepcopy/gi ], 
//      object: { number: NaN, string: 'A', boolean: true }, 
//      to: [Circular] } } 
  • value
    • * - copy target value
  • return
    • * - deep copied value

return deep copied value.

supported types are below:

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

The MIT license. Please see LICENSE file.