als-deep-clone
is a JavaScript utility for deep cloning objects, supporting various built-in types with handling for circular references.
npm install als-deep-clone
const deepClone = require('als-deep-clone');
const original = {
a: 1,
b: [2, 3, { d: 4 }],
c: { e: 5 }
};
const cloned = deepClone(original);
console.log(cloned);
als-deep-clone
supports deep cloning for the following JavaScript types:
- Primitive types (Number, String, Boolean, null, undefined)
- Objects (Plain objects and objects with a prototype chain)
- Arrays
- Date objects
- Regular expressions
Additionally, the utility handles circular references within objects to prevent infinite loops during the cloning process.
While als-deep-clone
covers many common use cases, it does not support cloning for:
- Functions: Due to the complexity and potential side effects of duplicating function scopes and closures.
- Maps, Sets: These collections are not cloned because they can require special considerations for key equality.
- Buffers, Typed Arrays: These binary data structures are not supported by default.
- Promises and other complex built-in types that do not have a straightforward duplication strategy.
Clones an object deeply, returning a new object with the same structure and values, but with no shared references to the original.
-
Parameters
-
object
(Object): The object to clone.
-
-
Returns
- (Object): A deep clone of the provided object.