clone
lang
Creates a shallow clone of value.
Installation
Import
import { clone } from '@tulx/utils';Source Code
Implementation
/**
* Creates a shallow clone of value.
*
* @param value - The value to clone.
* @returns Returns the cloned value.
*
* @example
* ```ts
* const objects = [{ 'a': 1 }, { 'b': 2 }];
* const shallow = clone(objects);
* console.log(shallow[0] === objects[0]); // true
* ```
*/
export function clone<T>(value: T): T {
if (value === null || typeof value !== 'object') {
return value;
}
if (Array.isArray(value)) {
return [...value] as T;
}
return { ...value } as T;
}
Example
import { clone } from '@tulx/utils';
const objects = [{ 'a': 1 }, { 'b': 2 }];
const shallow = clone(objects);
console.log(shallow[0] === objects[0]); // trueRelated Functions
castArray
Casts value as an array if it's not one.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.
eq
Performs a SameValueZero comparison between two values to determine if they are equivalent.