Kevin 29b9a0c50c clean && clean html base 4 年之前
..
bower_components 29b9a0c50c clean && clean html base 4 年之前
.npmignore 29b9a0c50c clean && clean html base 4 年之前
HISTORY.md 29b9a0c50c clean && clean html base 4 年之前
LICENSE 29b9a0c50c clean && clean html base 4 年之前
README.md 29b9a0c50c clean && clean html base 4 年之前
bower.json 29b9a0c50c clean && clean html base 4 年之前
deepcopy.js 29b9a0c50c clean && clean html base 4 年之前
package.json 29b9a0c50c clean && clean html base 4 年之前
testem.json 29b9a0c50c clean && clean html base 4 年之前

README.md

deepcopy.js

Build Status Dependency Status NPM version Bower version

deep copy for any data

Installation

npm

$ npm install deepcopy

bower

$ bower install deepcopy

Usage

node.js

var deepcopy = require('deepcopy');

browser

<script src="deepcopy.min.js"></script>

define deepcopy by define() if using AMD loader.

otherwise deepcopy export to global.

Example

var data, shallow, deep;

data = {
  objects: {
    array: [
      null, undefined, 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] } }
var data, deep;

data = { object: {} };
data.object[Symbol.for('sym')] = 123;

deep = deepcopy(data);

delete data.object;

console.log(data.object);
// undefined
console.log(deep.object[Symbol.for('sym')]);
// 123

Functions

deepcopy(value)

  • 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)
  • Symbol

Test

node.js

$ npm install
$ npm test

browser

$ npm install
$ npm run bower
$ npm run testem

Contributors

License

The MIT license. Please see LICENSE file.