0
# lodash._createwrapper
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash._createwrapper
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash._createwrapper`
10
11
## Core Imports
12
13
```javascript
14
var createWrapper = require('lodash._createwrapper');
15
```
16
17
## Basic Usage
18
19
```javascript
20
var createWrapper = require('lodash._createwrapper');
21
22
// Create a simple bound function
23
function greet(greeting, name) {
24
return greeting + ' ' + name + '!';
25
}
26
27
// Bind with BIND_FLAG (1)
28
var boundGreet = createWrapper(greet, 1, null, ['Hello'], null, null, null, null);
29
console.log(boundGreet('World')); // "Hello World!"
30
31
// Create a curried function with CURRY_FLAG (8)
32
function add(a, b, c) {
33
return a + b + c;
34
}
35
36
var curriedAdd = createWrapper(add, 8, null, null, null, null, null, 3);
37
console.log(curriedAdd(1)(2)(3)); // 6
38
console.log(curriedAdd(1, 2)(3)); // 6
39
```
40
41
## Architecture
42
43
The createWrapper function implements a sophisticated wrapper creation system using:
44
45
- **Bitmask Flags**: Control different wrapper behaviors through bit flags
46
- **Wrapper Types**: Different internal wrapper functions for specific use cases
47
- **Placeholder Support**: Advanced placeholder handling for currying
48
- **Type Safety**: Comprehensive type checking and conversion utilities
49
- **Performance Optimization**: Optimized execution paths for different wrapper types
50
51
## Capabilities
52
53
### Function Wrapper Creation
54
55
Creates sophisticated function wrappers with configurable behavior through bitmask flags.
56
57
```javascript { .api }
58
/**
59
* Creates a function that either curries or invokes `func` with optional
60
* `this` binding and partially applied arguments.
61
*
62
* @param {Function|string} func The function or method name to wrap
63
* @param {number} bitmask The bitmask of wrapper flags
64
* @param {*} [thisArg] The `this` binding of `func`
65
* @param {Array} [partials] The arguments to be partially applied
66
* @param {Array} [holders] The `partials` placeholder indexes
67
* @param {Array} [argPos] The argument positions of the new function
68
* @param {number} [ary] The arity cap of `func`
69
* @param {number} [arity] The arity of `func`
70
* @returns {Function} Returns the new wrapped function
71
* @throws {TypeError} Throws if `func` is not a function and not using BIND_KEY_FLAG
72
*/
73
function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity);
74
```
75
76
**Bitmask Flags:**
77
78
```javascript { .api }
79
// Wrapper behavior flags (used in bitmask parameter)
80
const BIND_FLAG = 1; // _.bind
81
const BIND_KEY_FLAG = 2; // _.bindKey
82
const CURRY_BOUND_FLAG = 4; // _.curry or _.curryRight of a bound function
83
const CURRY_FLAG = 8; // _.curry
84
const CURRY_RIGHT_FLAG = 16; // _.curryRight
85
const PARTIAL_FLAG = 32; // _.partial
86
const PARTIAL_RIGHT_FLAG = 64; // _.partialRight
87
const ARY_FLAG = 128; // _.ary (arity control)
88
const FLIP_FLAG = 512; // _.flip (argument order reversal)
89
```
90
91
**Usage Examples:**
92
93
```javascript
94
var createWrapper = require('lodash._createwrapper');
95
96
// Example 1: Simple binding
97
function greet(greeting, name) {
98
return this.prefix + greeting + ' ' + name + '!';
99
}
100
101
var context = { prefix: 'Say: ' };
102
var boundGreet = createWrapper(greet, 1, context, ['Hello'], null, null, null, null);
103
console.log(boundGreet('World')); // "Say: Hello World!"
104
105
// Example 2: Currying
106
function multiply(a, b, c) {
107
return a * b * c;
108
}
109
110
var curriedMultiply = createWrapper(multiply, 8, null, null, null, null, null, 3);
111
var multiplyBy2 = curriedMultiply(2);
112
var multiplyBy2And3 = multiplyBy2(3);
113
console.log(multiplyBy2And3(4)); // 24
114
115
// Example 3: Partial application
116
function calculate(operation, a, b, c) {
117
if (operation === 'sum') return a + b + c;
118
if (operation === 'product') return a * b * c;
119
}
120
121
var sum = createWrapper(calculate, 32, null, ['sum'], null, null, null, null);
122
console.log(sum(1, 2, 3)); // 6
123
124
// Example 4: Argument reordering with flip
125
function divide(a, b) {
126
return a / b;
127
}
128
129
var flippedDivide = createWrapper(divide, 512, null, null, null, null, null, null);
130
console.log(flippedDivide(2, 10)); // 5 (equivalent to divide(10, 2))
131
132
// Example 5: Arity control
133
function variadic() {
134
return Array.prototype.slice.call(arguments);
135
}
136
137
var binary = createWrapper(variadic, 128, null, null, null, null, 2, null);
138
console.log(binary(1, 2, 3, 4, 5)); // [1, 2] (only first 2 arguments)
139
```
140
141
142
## Advanced Usage Patterns
143
144
### Complex Wrapper Combinations
145
146
You can combine multiple flags to create sophisticated wrapper behaviors:
147
148
```javascript
149
var createWrapper = require('lodash._createwrapper');
150
151
// Combine BIND_FLAG + PARTIAL_FLAG for bound partial application
152
function logger(level, category, message) {
153
console.log('[' + level + '] ' + category + ': ' + message);
154
}
155
156
var context = { prefix: 'App ' };
157
var infoLogger = createWrapper(
158
logger,
159
1 | 32, // BIND_FLAG | PARTIAL_FLAG
160
context,
161
['INFO'],
162
null,
163
null,
164
null,
165
null
166
);
167
168
infoLogger('Database', 'Connection established'); // [INFO] Database: Connection established
169
170
// Combine CURRY_FLAG + PARTIAL_FLAG for curried partial application
171
function process(config, data, options, callback) {
172
// Processing logic
173
callback(null, 'processed: ' + data);
174
}
175
176
var curriedProcessor = createWrapper(
177
process,
178
8 | 32, // CURRY_FLAG | PARTIAL_FLAG
179
null,
180
[{ mode: 'fast' }], // partial config
181
null,
182
null,
183
null,
184
4 // arity
185
);
186
187
// Can be called with currying
188
curriedProcessor('mydata')({ compress: true })(function(err, result) {
189
console.log(result); // 'processed: mydata'
190
});
191
```
192
193
### Placeholder Support
194
195
For curried functions, the wrapper supports placeholder values for flexible argument positioning:
196
197
```javascript
198
var createWrapper = require('lodash._createwrapper');
199
200
function format(template, value1, value2, value3) {
201
return template
202
.replace('%1', value1)
203
.replace('%2', value2)
204
.replace('%3', value3);
205
}
206
207
var curriedFormat = createWrapper(format, 8, null, null, null, null, null, 4);
208
209
// Use placeholder pattern (when accessing .placeholder property in real lodash)
210
// Note: In this standalone module, placeholder handling is internal
211
var template = curriedFormat('Hello %1, you have %2 %3');
212
console.log(template('John', 5, 'messages')); // "Hello John, you have 5 messages"
213
```
214
215
### Error Handling
216
217
The createWrapper function includes comprehensive error handling:
218
219
```javascript
220
var createWrapper = require('lodash._createwrapper');
221
222
try {
223
// This will throw TypeError since 'not-a-function' is not a function
224
// and BIND_KEY_FLAG is not used
225
var wrapper = createWrapper('not-a-function', 1, null, null, null, null, null, null);
226
} catch (error) {
227
console.log(error.message); // "Expected a function"
228
}
229
230
// Valid usage with BIND_KEY_FLAG for method names
231
var obj = {
232
getValue: function() { return this.value; },
233
value: 42
234
};
235
236
var boundMethod = createWrapper('getValue', 2, obj, null, null, null, null, null);
237
console.log(boundMethod()); // 42
238
```
239
240
## Performance Considerations
241
242
- The wrapper uses optimized execution paths based on the bitmask flags
243
- Simple wrappers (bind-only) use lightweight implementations
244
- Complex wrappers (hybrid with multiple flags) use more sophisticated but heavier implementations
245
- Arity and argument handling are optimized for common use cases (0-7 arguments)
246
- Type checking is minimal and focuses on essential validations only
247
248
## Common Use Cases
249
250
1. **Function Binding**: Create bound methods with preset `this` context
251
2. **Currying**: Transform multi-argument functions into sequences of single-argument functions
252
3. **Partial Application**: Create specialized functions with some arguments pre-filled
253
4. **Argument Reordering**: Change the order of function arguments
254
5. **Arity Control**: Limit the number of arguments passed to a function
255
6. **Method Binding**: Bind object methods by name rather than reference