-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowPipe.js
64 lines (55 loc) · 2.33 KB
/
FlowPipe.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
class FlowingValue {
constructor (val) {
this.get = function () {
return val;
};
return new Proxy(this, {
get (target, property, receiver) {
if (property == 'pipe') {
flow.$ = val;
return function (input, ...args) {
var res;
var lastArg = args[args.length-1];
var returnOrig = false, returnBoth = false;
if (lastArg === flow.$orig) {
returnOrig = true;
} else if (lastArg === flow.$both) {
returnBoth = true;
}
function isFunctionCallSpec(obj) {
var propNum = Object.keys(obj).length;
var doesMatchSpec = (
(propNum == 2 && (!!obj.this || obj.this === null) && !!obj.args) ||
(propNum == 1 && ((!!obj.this || obj.this === null) || !!obj.args))
);
return doesMatchSpec;
}
if (typeof input == 'function') {
let ndArg;
if ((ndArg = args[0]) && typeof ndArg == 'object' && isFunctionCallSpec(ndArg)) {
let callThisRef = ndArg.this || null;
let callArgs = ndArg.args || [];
res = input.apply(callThisRef, callArgs);
} else {
res = input(val, ...args);
}
} else {
res = input;
}
var newFlowing = res;
if (returnOrig) newFlowing = val;
if (returnBoth) newFlowing = [res, val];
return new FlowingValue(newFlowing);
};
}
return Reflect.get(target, property, receiver);
}
});
}
}
function flow(val) {
return new FlowingValue(val);
}
flow.$ = undefined;
flow.$orig = Symbol('FlowReturnOrigValue');
flow.$both = Symbol('FlowReturnBothValues');