Giter VIP home page Giter VIP logo

deep-clone's Introduction

deep-clone

Overview

The deep-clone module provides a function that can be used to clone both json objects and json arrays.

Rather that a single parameter, the function takes two parameters: the object to clone and an optional function that will be called in case a function or a non-native object is found in the object to clone.

In this way, the developer can decide what to do with the function or the non-native object.

In case this function is not provided and a function or a non-native object is found, the function will throw an error.

Usage

Simple clone

const {deepClone} = require('deep-clone');

const obj = {
  a: 1,
  b: {
    c: undefined,
    d: {
      e: null,
      f: [1, 2, 3, 4, 5, 6, 7, 8, 9, {g: 10}]
    }
  }
};

const clonedObj = deepClone(obj);

console.log(clonedObj);

// {
//   a: 1,
//   b: {
//     c: undefined,
//     d: {
//       e: null,
//       f: [1, 2, 3, 4, 5, 6, 7, 8, 9, { g: 10 }]
//     }
//   }
// }

Clone with custom function

As said before, the function takes two parameters: the object to clone and an optional function that will be called in case a function or a non-native object is found in the object to clone.

In this way, the developer can decide what to do with the function or the *non-native object.

const {deepClone} = require('deep-clone');

const obj = {
  a: 1,
  b: {
    c: new Date("2023-03-25T21:27:20.469Z")
    d: [1, 2, 3, 4, 5, 6, 7, 8, 9, {g: 10}]
  },
  c: function () {
    console.log('Hello world!');
  },
  d: new Map([
    [1, 'one'],
    [2, 'two'],
    [3, 'three'],
  ])
};

const clonedObj = deepClone(obj, (value) => {
  if (value instanceof Date) {
    return new Date(value);
  }

  if (value instanceof Map) {
    return new Map(value);
  }

  return value;
});

Possible errors

The function will throw an error if a function or a non-native object is found in the object to clone and no custom function is provided, like in the following example:

const {deepClone} = require('deep-clone');

const obj = {
  a: 1,
  b: {
    c: new Date("2023-03-25T21:27:20.469Z")
    d: [1, 2, 3, 4, 5, 6, 7, 8, 9, {g: 10}]
  },
  c: new Map([
    [1, 'one'],
    [2, 'two'],
    [3, 'three'],
  ])
};

const clonedObj = deepClone(obj);

deep-clone's People

Watchers

James Cloos avatar Fabio Cingottini 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.