-
Notifications
You must be signed in to change notification settings - Fork 117
/
underscore.function.arity.js
214 lines (181 loc) · 5.67 KB
/
underscore.function.arity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Underscore-contrib (underscore.function.arity.js 0.3.0)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `require` it on the server.
if (typeof exports === 'object') {
_ = module.exports = require('underscore');
}
// Helpers
// -------
function enforcesUnary (fn) {
return function mustBeUnary () {
if (arguments.length === 1) {
return fn.apply(this, arguments);
}
else throw new RangeError('Only a single argument may be accepted.');
};
}
// Curry
// -------
var curry = (function () {
function collectArgs(func, that, argCount, args, newArg, reverse) {
if (reverse === true) {
args.unshift(newArg);
} else {
args.push(newArg);
}
if (args.length == argCount) {
return func.apply(that, args);
} else {
return enforcesUnary(function () {
return collectArgs(func, that, argCount, args.slice(0), arguments[0], reverse);
});
}
}
return function curry (func, reverse) {
var that = this;
return enforcesUnary(function () {
return collectArgs(func, that, func.length, [], arguments[0], reverse);
});
};
}());
// Enforce Arity
// --------------------
var enforce = (function () {
var CACHE = [];
return function enforce (func) {
if (typeof func !== 'function') {
throw new Error('Argument 1 must be a function.');
}
var funcLength = func.length;
if (CACHE[funcLength] === undefined) {
CACHE[funcLength] = function (enforceFunc) {
return function () {
if (arguments.length !== funcLength) {
throw new RangeError(funcLength + ' arguments must be applied.');
}
return enforceFunc.apply(this, arguments);
};
};
}
return CACHE[funcLength](func);
};
}());
// Right curry variants
// ---------------------
var curryRight = function (func) {
return curry.call(this, func, true);
};
var curryRight2 = function (fun) {
return enforcesUnary(function (last) {
return enforcesUnary(function (first) {
return fun.call(this, first, last);
});
});
};
var curryRight3 = function (fun) {
return enforcesUnary(function (last) {
return enforcesUnary(function (second) {
return enforcesUnary(function (first) {
return fun.call(this, first, second, last);
});
});
});
};
// Mixing in the arity functions
// -----------------------------
_.mixin({
// ### Fixed arguments
// Fixes the arguments to a function based on the parameter template defined by
// the presence of values and the `_` placeholder.
fix: function(fun) {
var fixArgs = _.rest(arguments);
var f = function() {
var args = fixArgs.slice();
var arg = 0;
for ( var i = 0; i < (args.length || arg < arguments.length); i++ ) {
if ( args[i] === _ ) {
args[i] = arguments[arg++];
}
}
return fun.apply(null, args);
};
f._original = fun;
return f;
},
unary: function (fun) {
return function unary (a) {
return fun.call(this, a);
};
},
binary: function (fun) {
return function binary (a, b) {
return fun.call(this, a, b);
};
},
ternary: function (fun) {
return function ternary (a, b, c) {
return fun.call(this, a, b, c);
};
},
quaternary: function (fun) {
return function quaternary (a, b, c, d) {
return fun.call(this, a, b, c, d);
};
},
// Flexible curry function with strict arity.
// Argument application left to right.
// source: https://github.com/eborden/js-curry
curry: curry,
// Flexible right to left curry with strict arity.
curryRight: curryRight,
rCurry: curryRight, // alias for backwards compatibility
curry2: function (fun) {
return enforcesUnary(function curried (first) {
return enforcesUnary(function (last) {
return fun.call(this, first, last);
});
});
},
curry3: function (fun) {
return enforcesUnary(function (first) {
return enforcesUnary(function (second) {
return enforcesUnary(function (last) {
return fun.call(this, first, second, last);
});
});
});
},
// reverse currying for functions taking two arguments.
curryRight2: curryRight2,
rcurry2: curryRight2, // alias for backwards compatibility
curryRight3: curryRight3,
rcurry3: curryRight3, // alias for backwards compatibility
// Dynamic decorator to enforce function arity and defeat varargs.
enforce: enforce
});
_.arity = (function () {
// Allow 'new Function', as that is currently the only reliable way
// to manipulate function.length
/* jshint -W054 */
var FUNCTIONS = {};
return function arity (numberOfArgs, fun) {
if (FUNCTIONS[numberOfArgs] == null) {
var parameters = new Array(numberOfArgs);
for (var i = 0; i < numberOfArgs; ++i) {
parameters[i] = "__" + i;
}
var pstr = parameters.join();
var code = "return function ("+pstr+") { return fun.apply(this, arguments); };";
FUNCTIONS[numberOfArgs] = new Function(['fun'], code);
}
if (fun == null) {
return function (fun) { return arity(numberOfArgs, fun); };
}
else return FUNCTIONS[numberOfArgs](fun);
};
})();
}).call(this);