Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@toolz/clone
Advanced tools
Readme
clone
is a utility for creating deep clones of JavaScript objects and arrays. The functions work recursively, so a massively-nested objecdt/array will still yield a clean "clone" - with no ties to its original. This package was created to deal with this issue:
const originalObject = {
one: 'uno',
two: [
42,
{
three: 'tres',
four: 'quatro',
},
],
}
const copiedObject = originalObject;
copiedObject.two[1].four = 'vier';
console.log(originalObject.two[1].four); // 'vier'
/*
Even though the updated value was set on copiedObject,
the changes are reflected in originalObject,
because copiedObject holds a reference to originalObject.
*/
After installation, import the package:
import { clone } from '@toolz/clone';
const API = {
arguments: {
sourceArray: {
required,
format: Array,
},
},
returns: Array,
}
Examples:
const bigHairyNestedArray = [
'foo',
1,
null,
{},
[],
true,
{
one: 'one',
two: 'two',
three: {
a: null,
b: {},
c: 42,
d: 3.14,
e: [
'1',
'2',
'3',
4,
null,
],
},
},
]
const clonedArray = clone.array(bigHairyNestedArray);
clonedArray[6].three.e[3] = 42;
console.log(bigHairyNestedArray[6].three.e[3]); // 4
/*
clonedArray has all of the elements/levels present in
bigHairyNestedArray, but changes to clonedArray are
not reflected in the source, because it's an independent
clone
*/
const API = {
arguments: {
sourceObject: {
required,
format: Object,
},
},
returns: Object,
}
const bigHairyNestedObject = {
a: 'foo',
b: 1,
c: null,
d: {},
e: [],
f: true,
g: {
one: 'one',
two: 'two',
three: {
a: null,
b: {},
c: 42,
d: 3.14,
e: [
'1',
'2',
'3',
4,
null,
],
},
},
}
const clonedObject = clone.object(bigHairyNestedObject);
clonedObject.g.three.e[3] = 42;
console.log(bigHairyNestedObject.g.three.e[3]); // 4
/*
clonedObject has all of the elements/levels present in
bigHairyNestedObject, but changes to clonedObject are
not reflected in the source, because it's an independent
clone
*/
FAQs
A library to do deep clones of JavaScript objects and arrays
The npm package @toolz/clone receives a total of 6 weekly downloads. As such, @toolz/clone popularity was classified as not popular.
We found that @toolz/clone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.