GithubHelp home page GithubHelp logo

rchl / deepcopy.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sasaplus1/deepcopy.js

0.0 2.0 0.0 1.31 MB

deep copy data

Home Page: https://sasaplus1.github.io/deepcopy.js/benchmark

License: MIT License

JavaScript 20.16% TypeScript 79.84%

deepcopy.js's Introduction

deepcopy.js

test npm version Try deepcopy on RunKit renovate

deep copy data

Installation

npm

$ npm install deepcopy

Usage

node.js

JavaScript

const deepcopy = require('deepcopy');

TypeScript

import * as deepcopy from 'deepcopy';

browser

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

Example

basic usage:

const src = {
  desserts: [
    { name: 'cake'      },
    { name: 'ice cream' },
    { name: 'pudding'   }
  ]
};

const dist = deepcopy(src);

src.desserts = null;

console.log(src);   // { desserts: null }
console.log(dist);  // { desserts: [ { name: 'cake' }, { name: 'ice cream' }, { name: 'pudding' } ] }

customize deepcopy:

function MyClass(id) {
  this._id = id;
}

const src = {
  myClasses: [
    new MyClass(1),
    new MyClass(2),
    new MyClass(3)
  ]
};

const dest = deepcopy(base, {
  customizer(value) {
    if (target.constructor === MyClass) {
      return new MyClass(target._id);
    }
  }
});

src.myClasses = null;

console.log(src);   // { myClasses: null }
console.log(dest);  // { myClasses: [ MyClass { _id: 1 }, MyClass { _id: 2 }, MyClass { _id: 3 } ] }

Functions

deepcopy(value[, options])

  • value
    • *
      • target value
  • options
    • Object|Function
      • Object - pass options
      • Function - use as customize function
  • return
    • * - copied value

Supported types and copy operation

type operation
ArrayBuffer deep copy
Boolean deep copy
Buffer deep copy node.js only
DataView deep copy
Date deep copy
Number deep copy
RegExp deep copy
String deep copy
Float32Array deep copy
Float64Array deep copy
Int16Array deep copy
Int32Array deep copy
Int8Array deep copy
Uint16Array deep copy
Uint32Array deep copy
Uint8Array deep copy
Uint8ClampedArray deep copy
boolean deep copy
null deep copy
number deep copy
string deep copy
symbol deep copy
undefined deep copy
Arguments deep copy recursively, copy as Array
Array deep copy recursively
Map deep copy recursively
Object deep copy recursively
Set deep copy recursively
Array Iterator shallow copy
Map Iterator shallow copy
Promise shallow copy
Set Iterator shallow copy
String Iterator shallow copy
function shallow copy
global shallow copy window, global, self, etc.
WeakMap shallow copy
WeakSet shallow copy

Contributors

License

The MIT license.

deepcopy.js's People

Contributors

sasaplus1 avatar renovate-bot avatar kjirou avatar pr0methean avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

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.