-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (35 loc) · 987 Bytes
/
index.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
Function.prototype.asfirst = function (...dargs) {
return function (...args) {
fillUndefs(dargs, args, false);
return this(...dargs,
...args.splice(dargs.length));
}.bind(this);
};
Function.prototype.aslast = function (...dargs) {
return function (...args) {
fillUndefs(dargs, args, true);
var dlen = dargs.length;
return this(...(args.splice(
Math.max(0, args.length - dlen), dlen)
&& args), ...dargs);
}.bind(this);
};
Function.prototype.as = function (fargs, largs) {
fargs = fargs || [];
largs = largs || [];
return this.asfirst(...fargs).aslast(...largs);
};
function fillUndefs(dargs, args, last) {
let undef = dargs.indexOf(undefined);
let aindex;
while (undef >= 0) {
if (undef < args.length) {
aindex = last ?
undef + Math.max(args.length - dargs.length, 0) :
undef;
dargs[undef] = args[aindex];
undef = dargs.indexOf(undefined);
} else break;
}
}
module.exports = true;