CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-createwrapper

The internal lodash function createWrapper exported as a module for creating wrapped versions of functions with binding, currying, partial application, and argument reordering support.

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 sophisticated utility creates wrapped versions of functions with support for various functional programming patterns including binding, currying, partial application, argument reordering, and arity control.

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');

// Create a simple bound function
function greet(greeting, name) {
  return greeting + ' ' + name + '!';
}

// Bind with BIND_FLAG (1)
var boundGreet = createWrapper(greet, 1, null, ['Hello'], null, null, null, null);
console.log(boundGreet('World')); // "Hello World!"

// Create a curried function with CURRY_FLAG (8)
function add(a, b, c) {
  return a + b + c;
}

var curriedAdd = createWrapper(add, 8, null, null, null, null, null, 3);
console.log(curriedAdd(1)(2)(3)); // 6
console.log(curriedAdd(1, 2)(3)); // 6

Architecture

The createWrapper function implements a sophisticated wrapper creation system using:

  • Bitmask Flags: Control different wrapper behaviors through bit flags
  • Wrapper Types: Different internal wrapper functions for specific use cases
  • Placeholder Support: Advanced placeholder handling for currying
  • Type Safety: Comprehensive type checking and conversion utilities
  • Performance Optimization: Optimized execution paths for different wrapper types

Capabilities

Function Wrapper Creation

Creates sophisticated function wrappers with configurable behavior through bitmask flags.

/**
 * Creates a function that either curries or invokes `func` with optional
 * `this` binding and partially applied arguments.
 * 
 * @param {Function|string} func The function or method name to wrap
 * @param {number} bitmask The bitmask of wrapper flags
 * @param {*} [thisArg] The `this` binding of `func`
 * @param {Array} [partials] The arguments to be partially applied
 * @param {Array} [holders] The `partials` placeholder indexes
 * @param {Array} [argPos] The argument positions of the new function
 * @param {number} [ary] The arity cap of `func`
 * @param {number} [arity] The arity of `func`
 * @returns {Function} Returns the new wrapped function
 * @throws {TypeError} Throws if `func` is not a function and not using BIND_KEY_FLAG
 */
function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity);

Bitmask Flags:

// Wrapper behavior flags (used in bitmask parameter)
const BIND_FLAG = 1;           // _.bind
const BIND_KEY_FLAG = 2;       // _.bindKey  
const CURRY_BOUND_FLAG = 4;    // _.curry or _.curryRight of a bound function
const CURRY_FLAG = 8;          // _.curry
const CURRY_RIGHT_FLAG = 16;   // _.curryRight
const PARTIAL_FLAG = 32;       // _.partial
const PARTIAL_RIGHT_FLAG = 64; // _.partialRight
const ARY_FLAG = 128;          // _.ary (arity control)
const FLIP_FLAG = 512;         // _.flip (argument order reversal)

Usage Examples:

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

// Example 1: Simple binding
function greet(greeting, name) {
  return this.prefix + greeting + ' ' + name + '!';
}

var context = { prefix: 'Say: ' };
var boundGreet = createWrapper(greet, 1, context, ['Hello'], null, null, null, null);
console.log(boundGreet('World')); // "Say: Hello World!"

// Example 2: Currying
function multiply(a, b, c) {
  return a * b * c;
}

var curriedMultiply = createWrapper(multiply, 8, null, null, null, null, null, 3);
var multiplyBy2 = curriedMultiply(2);
var multiplyBy2And3 = multiplyBy2(3);
console.log(multiplyBy2And3(4)); // 24

// Example 3: Partial application
function calculate(operation, a, b, c) {
  if (operation === 'sum') return a + b + c;
  if (operation === 'product') return a * b * c;
}

var sum = createWrapper(calculate, 32, null, ['sum'], null, null, null, null);
console.log(sum(1, 2, 3)); // 6

// Example 4: Argument reordering with flip
function divide(a, b) {
  return a / b;
}

var flippedDivide = createWrapper(divide, 512, null, null, null, null, null, null);
console.log(flippedDivide(2, 10)); // 5 (equivalent to divide(10, 2))

// Example 5: Arity control
function variadic() {
  return Array.prototype.slice.call(arguments);
}

var binary = createWrapper(variadic, 128, null, null, null, null, 2, null);
console.log(binary(1, 2, 3, 4, 5)); // [1, 2] (only first 2 arguments)

Advanced Usage Patterns

Complex Wrapper Combinations

You can combine multiple flags to create sophisticated wrapper behaviors:

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

// Combine BIND_FLAG + PARTIAL_FLAG for bound partial application
function logger(level, category, message) {
  console.log('[' + level + '] ' + category + ': ' + message);
}

var context = { prefix: 'App ' };
var infoLogger = createWrapper(
  logger, 
  1 | 32, // BIND_FLAG | PARTIAL_FLAG
  context, 
  ['INFO'], 
  null, 
  null, 
  null, 
  null
);

infoLogger('Database', 'Connection established'); // [INFO] Database: Connection established

// Combine CURRY_FLAG + PARTIAL_FLAG for curried partial application  
function process(config, data, options, callback) {
  // Processing logic
  callback(null, 'processed: ' + data);
}

var curriedProcessor = createWrapper(
  process,
  8 | 32, // CURRY_FLAG | PARTIAL_FLAG
  null,
  [{ mode: 'fast' }], // partial config
  null,
  null,
  null,
  4 // arity
);

// Can be called with currying
curriedProcessor('mydata')({ compress: true })(function(err, result) {
  console.log(result); // 'processed: mydata'
});

Placeholder Support

For curried functions, the wrapper supports placeholder values for flexible argument positioning:

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

function format(template, value1, value2, value3) {
  return template
    .replace('%1', value1)
    .replace('%2', value2) 
    .replace('%3', value3);
}

var curriedFormat = createWrapper(format, 8, null, null, null, null, null, 4);

// Use placeholder pattern (when accessing .placeholder property in real lodash)
// Note: In this standalone module, placeholder handling is internal
var template = curriedFormat('Hello %1, you have %2 %3');
console.log(template('John', 5, 'messages')); // "Hello John, you have 5 messages"

Error Handling

The createWrapper function includes comprehensive error handling:

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

try {
  // This will throw TypeError since 'not-a-function' is not a function
  // and BIND_KEY_FLAG is not used
  var wrapper = createWrapper('not-a-function', 1, null, null, null, null, null, null);
} catch (error) {
  console.log(error.message); // "Expected a function"
}

// Valid usage with BIND_KEY_FLAG for method names
var obj = {
  getValue: function() { return this.value; },
  value: 42
};

var boundMethod = createWrapper('getValue', 2, obj, null, null, null, null, null);
console.log(boundMethod()); // 42

Performance Considerations

  • The wrapper uses optimized execution paths based on the bitmask flags
  • Simple wrappers (bind-only) use lightweight implementations
  • Complex wrappers (hybrid with multiple flags) use more sophisticated but heavier implementations
  • Arity and argument handling are optimized for common use cases (0-7 arguments)
  • Type checking is minimal and focuses on essential validations only

Common Use Cases

  1. Function Binding: Create bound methods with preset this context
  2. Currying: Transform multi-argument functions into sequences of single-argument functions
  3. Partial Application: Create specialized functions with some arguments pre-filled
  4. Argument Reordering: Change the order of function arguments
  5. Arity Control: Limit the number of arguments passed to a function
  6. Method Binding: Bind object methods by name rather than reference
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash._createwrapper@4.0.x
Publish Source
CLI
Badge
tessl/npm-lodash-createwrapper badge