-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.js
52 lines (47 loc) · 1.35 KB
/
args.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
var curry = require('./curry');
var reorder = require('./reorder');
var utils = require('./utils');
var private = {
applyTo: function(getArgApplies, fn) {
return curry.curry(function() {
var args = [].slice.apply(arguments);
var argApplies = getArgApplies(args);
for (var i = 0; i < args.length; ++i)
{
if (argApplies[i + 1])
args[i] = argApplies[i + 1](args[i]);
}
return fn.apply(null, args);
}, curry.getArity(fn));
},
argIndexesToArgApplies: function(argIndexes, apply) {
return argIndexes.reduce(function(acc, argIndex) {
acc[argIndex] = apply;
return acc;
}, {});
}
};
var args = {
applyToArg: function(apply, argIndexes, fn) {
return private.applyTo(function(args) {
return private.argIndexesToArgApplies(utils.normalizeToArray(argIndexes), apply);
}, fn);
},
applyToArgsFrom: function(apply, argIndex, fn) {
return private.applyTo(function(args) {
var generator = require('./generator');
return private.argIndexesToArgApplies(generator.range(argIndex, args.length), apply);
}, fn);
},
applyToArgs: function(argApplies, fn) {
return private.applyTo(function(args) {
return argApplies;
}, fn);
}
};
var curriedArgs = curry.curryObject(args);
utils.extend(curriedArgs, {
// add specialized functions
applyToAllArgs: reorder.flip(curriedArgs.applyToArgsFrom)(1)
});
module.exports = curriedArgs;