0.1.0 • Published 6 years ago

@gact/clone v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

clone

CircleCI Coveralls github GitHub npm npm bundle size

clone let's you perfectly deep clone JavaScript values.

Supported Values

The following types of JavaScript values are cloneable:

  • string
  • number
  • bigint
  • boolean
  • null
  • undefined
  • Number
  • String
  • Boolean
  • BigInt
  • Date
  • RegExp
  • Blob
  • File
  • ArrayBuffer
  • DataView
  • Int8Array
  • Int16Array
  • Int32Array
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • Float32Array
  • Float64Array
  • BigInt64Array
  • BigUint64Array
  • Array (when every value is cloneabe)
  • Object (when every property is cloneable)
  • Map (when every key and value is cloneable)
  • Set (when every element is cloneable)

Important Caveats

Symbols

You cannot clone symbol properties because symbols are not cloneable.

let dope = Symbol("dope");

let bob = {
  [dope]: 1000
};

let bobClone = clone(bob);

bob[dope]; // 1000
bobClone[dope]; // undefined

Getter/Setter Properties

You cannot clone any value with getter/setter properties because functions are not cloneable.

let bob = {
  get dope() {
    return 1000;
  }
};

// this will throw an error
let bobClone = clone(bob);

API

clone

Creates a perfect clone of the provided value.

Arguments

  1. value (Cloneable): The value to clone

Returns

(Cloneable): A prefect clone of the provided value

Example

import clone from "@gact/clone";

let bob = {
  name: "bob",
  hobbies: [
    {
      name: "programming",
      mastery: 88
    },
    {
      name: "cooking",
      mastery: 75
    }
  ]
};

let bobClone = clone(bob);

bobClone.hobbies[0].mastery = 100;

console.log(bob.hobbies[0].mastery); // 88
console.log(bobClone.hobbies[0].mastery); // 100
0.1.0

6 years ago