Creates a deep clone of a value with customizable cloning behavior through a user-provided customizer function
npx @tessl/cli install tessl/npm-lodash--clonedeepwith@4.5.0Creates a deep clone of a value with customizable cloning behavior through a user-provided customizer function. This method is like _.cloneWith except that it recursively clones the value, providing complete control over how different types of values are cloned.
npm install lodash.clonedeepwithconst cloneDeepWith = require('lodash.clonedeepwith');Or when using the full lodash library:
const _ = require('lodash');
// Use as _.cloneDeepWith(value, customizer)const cloneDeepWith = require('lodash.clonedeepwith');
// Basic deep cloning with custom DOM element handling
function customizer(value) {
if (value && value.nodeType) {
return value.cloneNode(true);
}
}
const originalElement = document.body;
const clonedElement = cloneDeepWith(originalElement, customizer);
console.log(clonedElement === originalElement); // => false
console.log(clonedElement.nodeName); // => 'BODY'
console.log(clonedElement.childNodes.length); // => 20Creates a deep clone of a value with customizable cloning behavior through a user-provided customizer function.
/**
* This method is like `_.cloneWith` except that it recursively clones `value`.
*
* @param {*} value The value to recursively clone.
* @param {Function} [customizer] The function to customize cloning.
* @returns {*} Returns the deep cloned value.
*/
function cloneDeepWith(value, customizer);Parameters:
value ()* - The value to recursively clone. Can be any JavaScript value including objects, arrays, primitives, functions, etc.customizer (Function) [optional] - The function to customize cloning behaviorReturns:
Customizer Function:
The customizer function is invoked with different arguments depending on the context:
customizer(value)customizer(value, key, object, stack)Where:
value - The current value being clonedkey - The key of the property being cloned (when cloning object properties)object - The parent object containing the property (when cloning object properties)stack - Internal stack object used for circular reference handlingCustomizer Return Behavior:
undefined to use default cloning behaviorUsage Examples:
const cloneDeepWith = require('lodash.clonedeepwith');
// Example 1: Custom DOM element cloning
function domCustomizer(value) {
if (value && value.nodeType) {
return value.cloneNode(true);
}
}
const element = document.createElement('div');
element.innerHTML = '<span>Hello</span>';
const clonedElement = cloneDeepWith(element, domCustomizer);
// Example 2: Custom class instance handling
class MyClass {
constructor(data) {
this.data = data;
}
}
function classCustomizer(value) {
if (value instanceof MyClass) {
return new MyClass(value.data);
}
}
const original = {
items: [new MyClass('test'), new MyClass('data')]
};
const cloned = cloneDeepWith(original, classCustomizer);
// Example 3: Selective property handling
function selectiveCustomizer(value, key) {
if (key === 'password') {
return '[REDACTED]';
}
if (key === 'timestamp') {
return new Date();
}
}
const userData = {
name: 'John',
password: 'secret123',
timestamp: new Date('2020-01-01'),
profile: {
email: 'john@example.com',
password: 'another-secret'
}
};
const sanitized = cloneDeepWith(userData, selectiveCustomizer);
// Example 4: Type-based customization
function typeCustomizer(value) {
if (value instanceof Date) {
return new Date(value.getTime() + 86400000); // Add one day
}
if (typeof value === 'function') {
return function() { return 'wrapped'; };
}
}
const complex = {
date: new Date(),
fn: function() { return 'original'; },
nested: {
date: new Date('2020-01-01'),
fn: () => 'arrow'
}
};
const customized = cloneDeepWith(complex, typeCustomizer);The function can clone the following types:
The function handles circular references automatically using an internal stack. If the customizer function throws an error, the error will propagate up to the caller.