Lodash Internal createAssigner Module

3.1.1 · abandoned · verified Sun Apr 19

lodash._createassigner is a low-level, internal utility module originating from the Lodash v3 ecosystem. Its primary function is to provide the core logic for object assignment operations, which are then used by public Lodash functions like `_.assign` and `_.assignIn`. This module is considered an internal implementation detail and is not intended for direct public consumption; its API is subject to change without notice and it may not be compatible with newer Lodash versions. The current stable version for this specific package is 3.1.1, corresponding to the Lodash v3 major release cycle. Modern Lodash (v4.x and newer) typically advocates for importing specific functions directly from the main `lodash` or `lodash-es` packages for improved tree-shaking and module resolution, rendering these older, granular internal modules largely obsolete. This package does not maintain an independent release cadence but is tied to the Lodash monorepo's overarching development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the internal mechanism of `lodash._createassigner` by using it to construct a basic object assignment function, similar to how Lodash itself creates `_.assign`. It highlights the module's role as a higher-order function that processes a base assignment logic into a fully-fledged assignment utility.

const createAssigner = require('lodash._createassigner');

// lodash._createassigner exports a higher-order function that generates
// specialized assignment functions. This example demonstrates how it
// might be used internally by Lodash to create a simple assignment utility.
// In modern Lodash (v4+), you would typically use `_.assign` or `_.assignIn` directly.

// This is a simplified 'base' function that handles the core property copying logic.
// The `createAssigner` function takes this as an argument.
const baseAssign = function(object, source) {
  if (object == null || source == null) return object;
  for (const key in source) {
    if (Object.prototype.hasOwnProperty.call(source, key)) {
      object[key] = source[key];
    }
  }
  return object;
};

// Use createAssigner to get an actual assignment function.
// The parameters to createAssigner (e.g., handling defaults, customizers)
// are complex and reflect internal Lodash logic.
// For a basic 'assign' equivalent, we pass our base function.
const assignFunction = createAssigner(baseAssign);

const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };

// The generated function behaves like Lodash's _.assign, taking multiple source objects.
const result = assignFunction(target, source1, source2);

console.log('Initial target:', { a: 1 });
console.log('Source 1:', source1);
console.log('Source 2:', source2);
console.log('Result of internal assignment:', result); // Expected: { a: 1, b: 2, c: 3 }

// For comparison, modern public API usage would be:
// import { assign } from 'lodash';
// const modernResult = assign({ a: 1 }, { b: 2 }, { c: 3 });
// console.log('Modern Lodash assign result:', modernResult);

view raw JSON →