Giter VIP home page Giter VIP logo

deepcopy.js's Introduction

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.

deepcopy.js's People

Contributors

kjirou avatar sasaplus1 avatar

Watchers

 avatar  avatar

Forkers

temybroder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.