or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--mergewith

The Lodash method _.mergeWith exported as a module for deep merging objects with customizable merge behavior.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.mergewith@4.6.x

To install, run

npx @tessl/cli install tessl/npm-lodash--mergewith@4.6.0

index.mddocs/

lodash.mergewith

The Lodash method _.mergeWith exported as a Node.js module. This package provides deep object merging capabilities with customizable merge behavior through a customizer function.

Package Information

  • Package Name: lodash.mergewith
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.mergewith

Core Imports

var mergeWith = require('lodash.mergewith');

For ES6 environments:

const mergeWith = require('lodash.mergewith');

ES6 import:

import mergeWith from 'lodash.mergewith';

Basic Usage

var mergeWith = require('lodash.mergewith');

// Simple object merging with custom array concatenation
function customizer(objValue, srcValue) {
  if (Array.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

var object = { 'a': [1], 'b': [2] };
var other = { 'a': [3], 'b': [4] };

mergeWith(object, other, customizer);
// => { 'a': [1, 3], 'b': [2, 4] }

// Multiple source objects
var target = { users: ['alice'] };
var source1 = { users: ['bob'], roles: ['admin'] };
var source2 = { users: ['charlie'], permissions: ['read'] };

mergeWith(target, source1, source2, customizer);
// => { users: ['alice', 'bob', 'charlie'], roles: ['admin'], permissions: ['read'] }

Capabilities

Object Merging with Customizer

Recursively merges objects with customizable merge behavior via a customizer function.

/**
 * This method is like _.merge except that it accepts customizer which
 * is invoked to produce the merged values of the destination and source
 * properties. If customizer returns undefined, merging is handled by the
 * method instead. The customizer is invoked with six arguments:
 * (objValue, srcValue, key, object, source, stack).
 * 
 * Note: This method mutates object.
 * 
 * @param {Object} object The destination object.
 * @param {...Object} sources The source objects (customizer is the last argument).
 * @returns {Object} Returns object.
 */
function mergeWith(object, source1, source2, ..., customizer);

Parameters:

  • object (Object): The destination object to merge into (mutated)
  • sources (Object...): The source objects to merge from, with the customizer function as the last argument
  • customizer (Function): Custom function to control merge behavior (last argument in sources)

Customizer Function:

The customizer function is called with six arguments and determines how values are merged:

/**
 * @param {*} objValue The destination value
 * @param {*} srcValue The source value
 * @param {string} key The key being merged
 * @param {Object} object The destination object
 * @param {Object} source The source object
 * @param {Object} stack Internal stack for handling circular references
 * @returns {*} The merged value, or undefined to use default merge behavior
 */
function customizer(objValue, srcValue, key, object, source, stack);

Returns:

  • Object: Returns the mutated destination object

Behavior:

  • Mutates the destination object
  • Performs deep/recursive merging of nested objects
  • If customizer returns undefined, default merge behavior is used
  • If customizer returns any other value, that value is used for the merge
  • Supports merging of complex objects, arrays, and primitive values
  • Handles circular references through internal stack mechanism
  • Optimized with caching and type checking for performance

Usage Examples:

var mergeWith = require('lodash.mergewith');

// Custom array handling - concatenate instead of replace
function arrayCustomizer(objValue, srcValue) {
  if (Array.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

var obj1 = { a: [1, 2], b: { x: 1 } };
var obj2 = { a: [3, 4], b: { y: 2 } };

mergeWith(obj1, obj2, arrayCustomizer);
// => { a: [1, 2, 3, 4], b: { x: 1, y: 2 } }

// Custom numeric handling - sum numbers instead of replace
function sumCustomizer(objValue, srcValue) {
  if (typeof objValue === 'number' && typeof srcValue === 'number') {
    return objValue + srcValue;
  }
}

var scores = { alice: 10, bob: 5 };
var bonuses = { alice: 2, charlie: 8 };

mergeWith(scores, bonuses, sumCustomizer);
// => { alice: 12, bob: 5, charlie: 8 }

// Conditional merging based on key
function conditionalCustomizer(objValue, srcValue, key) {
  if (key === 'protected') {
    return objValue; // Keep original value for protected keys
  }
}

var config = { debug: false, protected: 'secret' };
var updates = { debug: true, protected: 'hacked', version: '2.0' };

mergeWith(config, updates, conditionalCustomizer);
// => { debug: true, protected: 'secret', version: '2.0' }