CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash--createwrapper

Internal Lodash function createWrapper for creating function wrappers with behavioral modifications

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Lodash CreateWrapper

The internal Lodash function createWrapper exported as a standalone module. This low-level utility creates function wrappers with various behavioral modifications through bitmask flags, serving as the foundational wrapper factory for many of Lodash's higher-level functional programming utilities like bind, partial, curry, and rearg.

Package Information

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

Core Imports

var createWrapper = require('lodash._createwrapper');

Basic Usage

var createWrapper = require('lodash._createwrapper');

// Example: Create a simple bind wrapper
function greet(greeting, name) {
  return greeting + ', ' + name;
}
var obj = { greeting: 'Hello' };

// Create wrapper with bind flag (bitmask value 1 for BIND_FLAG)
var boundGreet = createWrapper(greet, 1, obj);
var result = boundGreet('Hello', 'World'); // 'Hello, World'

Architecture

The createWrapper function operates as a factory that produces specialized function wrappers based on bitmask flags:

  • Bitmask System: Uses bitwise flags to determine wrapper behavior (bind, partial, curry, etc.)
  • Specialized Creators: Delegates to optimized wrapper creators based on flag combinations
  • Metadata Management: Supports function metadata preservation and merging
  • Performance Optimization: Uses different wrapper strategies for optimal execution speed

Capabilities

Core Wrapper Creation

Creates function wrappers with behavioral modifications based on bitmask flags.

/**
 * Creates a function wrapper with behavioral modifications
 * @param {Function|string} func - The function or method name to wrap
 * @param {number} bitmask - Bitmask of flags controlling wrapper behavior
 * @param {*} [thisArg] - Optional 'this' binding for the wrapper
 * @param {Array} [partials] - Optional arguments to prepend
 * @param {Array} [holders] - Optional placeholder indexes for partials
 * @param {Array} [argPos] - Optional argument positions for reordering
 * @param {number} [ary] - Optional arity cap
 * @param {number} [arity] - Optional arity of the function
 * @returns {Function} The new wrapped function
 * @throws {TypeError} When func is not a function and BIND_KEY_FLAG is not set
 */
function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity);

Bitmask Flags

The createWrapper function uses numeric bitmask flags to control wrapper behavior. These flags can be combined using bitwise OR operations:

// Flag values (internal constants, use numeric values directly):
// BIND_FLAG = 1        - Bind 'this' context
// BIND_KEY_FLAG = 2    - Bind method by key name
// CURRY_BOUND_FLAG = 4 - Curry bound functions
// CURRY_FLAG = 8       - Enable currying
// CURRY_RIGHT_FLAG = 16    - Enable right-currying
// PARTIAL_FLAG = 32        - Partial application (left)
// PARTIAL_RIGHT_FLAG = 64  - Partial application (right)
// REARG_FLAG = 128     - Argument reordering
// ARY_FLAG = 256       - Arity capping

Usage Examples:

var createWrapper = require('lodash._createwrapper');

// Bind example (bitmask: 1)
var obj = { name: 'John' };
function sayHello() { return 'Hello, ' + this.name; }
var boundSayHello = createWrapper(sayHello, 1, obj);
// boundSayHello() returns "Hello, John"

// Partial application example (bitmask: 32)
function add(a, b, c) { return a + b + c; }
var addFive = createWrapper(add, 32, null, [5]);
// addFive(2, 3) returns 10

// Currying example (bitmask: 8)
function multiply(a, b, c) { return a * b * c; }
var curriedMultiply = createWrapper(multiply, 8);
// curriedMultiply(2)(3)(4) returns 24

// Arity capping example (bitmask: 256)
function variadicSum() {
  var sum = 0;
  for (var i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
}
var binarySum = createWrapper(variadicSum, 256, null, null, null, null, 2);
// binarySum(1, 2, 3, 4) returns 3 (only uses first 2 arguments)

// Combined flags example (bitmask: 1 | 32 = 33)
function greetWithPrefix(prefix, name) { 
  return prefix + ', ' + name + ' from ' + this.location; 
}
var obj = { location: 'Paris' };
var parisGreeter = createWrapper(greetWithPrefix, 33, obj, ['Bonjour']);
// parisGreeter('Marie') returns "Bonjour, Marie from Paris"

Error Handling

The createWrapper function includes built-in error validation:

  • TypeError: Thrown when func is not a function and BIND_KEY_FLAG (bitmask 2) is not set
  • Parameter Validation: Validates arity and length parameters
  • Null Handling: Safely handles null/undefined partials and holders arrays
// This will throw TypeError - not a function without BIND_KEY_FLAG
try {
  createWrapper("not a function", 1); // BIND_FLAG (1) without BIND_KEY_FLAG (2)
} catch (e) {
  console.log(e instanceof TypeError); // true
  console.log(e.message); // "Expected a function"
}

// This is valid (BIND_KEY_FLAG allows string method names)
var wrapper = createWrapper("toString", 2, {}); // BIND_KEY_FLAG = 2

Dependencies

This package depends on:

  • lodash._root: Required for root object access and utility functions
  • WeakMap support: For function metadata storage (polyfilled if not available)

Performance Considerations

  • Uses specialized wrapper creators based on bitmask combinations for optimal performance
  • Supports metadata caching through getData/setData mechanisms
  • Optimizes common cases like simple binding and partial application
  • Lazy evaluation patterns minimize overhead until wrapper execution
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash._createwrapper@3.2.x
Publish Source
CLI
Badge
tessl/npm-lodash--createwrapper badge