0.1.0 • Published 6 years ago
@gact/clone v0.1.0
clone
clone let's you perfectly deep clone JavaScript values.
Supported Values
The following types of JavaScript values are cloneable:
stringnumberbigintbooleannullundefinedNumberStringBooleanBigIntDateRegExpBlobFileArrayBufferDataViewInt8ArrayInt16ArrayInt32ArrayUint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64ArrayArray(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]; // undefinedGetter/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
- 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); // 1000.1.0
6 years ago