Internal Lodash function createWrapper for creating function wrappers with behavioral modifications
tessl install tessl/npm-lodash--createwrapper@3.2.0The 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.
npm install lodash._createwrappervar createWrapper = require('lodash._createwrapper');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'The createWrapper function operates as a factory that produces specialized function wrappers based on bitmask flags:
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);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 cappingUsage 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"The createWrapper function includes built-in error validation:
func is not a function and BIND_KEY_FLAG (bitmask 2) is not set// 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 = 2This package depends on: