-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
31 lines (27 loc) · 813 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
function isPromise(val) {
return val && typeof val.then === "function";
}
const placeHolder = Symbol();
function ppipe(val) {
const promised = Promise.resolve(val);
const pipe = function(fn, ...params) {
if (!fn) {
return val;
}
const plHoldrIdx = params.indexOf(placeHolder);
const plHoldrExists = plHoldrIdx >= 0;
const argumentInsPos = plHoldrExists ? plHoldrIdx : params.length;
const callResultFn = value => {
params.splice(argumentInsPos, plHoldrExists ? 1 : 0, value);
return fn(...params);
};
const res = isPromise(val) ? val.then(callResultFn) : callResultFn(val);
return ppipe(res);
};
pipe.val = val;
pipe.then = (...args) => promised.then(...args);
pipe.catch = (...args) => promised.catch(...args);
return pipe;
}
ppipe._ = placeHolder;
module.exports = ppipe;