
Enter a search term above to see results...
Enter a search term above to see results...
The Cloning utilities provide functions for creating copies of objects and arrays in JavaScript. These functions offer ways to create shallow or deep copies of data structures, which is crucial for avoiding unintended side effects when working with complex objects.
function clone(src, seen = new Map())
Creates a deep clone of the provided value.
Handling Complex Structures This function can clone arrays, objects, dates, regular expressions, maps, and sets. It also handles circular references to prevent infinite recursion.
Name | Type | Description |
---|---|---|
src | any | The value to clone |
seen | Map | Internal use. A Map to keep track of circular references |
A deep clone of the input value.
import { clone } from '@semantic-ui/utils';
const original = { a: 1, b: { c: 2 }, d: [1, 2, 3], e: new Date(), f: /test/gi};const cloned = clone(original);
console.log(cloned); // { a: 1, b: { c: 2 }, d: [1, 2, 3], e: Date, f: /test/gi }console.log(cloned !== original); // trueconsole.log(cloned.b !== original.b); // trueconsole.log(cloned.d !== original.d); // true
function extend(obj, ...sources)
Extends an object with properties from other objects, properly handling getter/setters.
Descriptor Handling This function uses
Object.getOwnPropertyDescriptor
andObject.defineProperty
to ensure that getters and setters are correctly copied, preserving their behavior in the extended object.
Name | Type | Description |
---|---|---|
obj | object | The target object to extend |
sources | …object | One or more source objects |
The extended object.
import { extend } from '@semantic-ui/utils';
const target = { a: 1 };const source1 = { b: 2, get c() { return this.b * 2; }};const source2 = { d: 4 };
const result = extend(target, source1, source2);console.log(result); // { a: 1, b: 2, d: 4 }console.log(result.c); // 4