-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorder.js
43 lines (40 loc) · 1.16 KB
/
reorder.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
var curry = require('./curry');
var id = require('./id');
var private = {
createDefaultOrder: function(fn) {
return Array.apply(null, Array(curry.getArity(fn))).map(function(item, index) {
return index + 1;
});
}
};
var reorder = {
reorder: function(fn, order) {
order = order || [];
var defaultOrder = private.createDefaultOrder(fn);
order = defaultOrder.map(function(item, index) {
return order[index] || item;
});
return curry.curryLike(fn, function() {
var reorderedArgs = [].slice.apply(arguments).map(function(item, index, array) {
return array[order[index] - 1];
});
return fn.apply(null, reorderedArgs);
});
},
nextArg: function(argIndex, fn) {
var defaultOrder = private.createDefaultOrder(fn);
var index = defaultOrder.indexOf(argIndex);
if (index != -1)
return reorder.reorder(fn, [argIndex].concat(defaultOrder.slice(0, argIndex - 1).concat(defaultOrder.slice(argIndex))));
else
return fn;
},
flipAll: function(fn) {
var defaultOrder = private.createDefaultOrder(fn);
return reorder.reorder(fn, defaultOrder.reverse());
},
flip: function(fn) {
return reorder.reorder(fn, [2, 1]);
}
};
module.exports = reorder;