ppipe 2.6.3 → 2.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +212 -8
- package/.github/CONTRIBUTING.md +13 -13
- package/.github/ISSUE_TEMPLATE.md +17 -17
- package/.github/PULL_REQUEST_TEMPLATE.md +30 -30
- package/.travis.yml +3 -3
- package/CODE_OF_CONDUCT.md +46 -46
- package/LICENSE +13 -13
- package/README.md +329 -322
- package/package.json +36 -32
- package/prettier.config.js +8 -0
- package/src/getPropertyByPath.js +8 -6
- package/src/index.js +49 -41
- package/src/lib/isFunction.js +1 -1
- package/src/lib/isPromise.js +1 -1
- package/test/examples.js +175 -175
- package/test/test.js +603 -603
- package/.idea/codeStyles/Project.xml +0 -16
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/ppipe.iml +0 -8
- package/.idea/vcs.xml +0 -6
package/package.json
CHANGED
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ppipe",
|
|
3
|
-
"version": "2.6.
|
|
4
|
-
"description": "piping without the operator support",
|
|
5
|
-
"main": "src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "nyc --reporter=text --reporter=html mocha test",
|
|
8
|
-
"coverage": "nyc report --reporter=text-lcov | coveralls"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"pipe
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "ppipe",
|
|
3
|
+
"version": "2.6.6",
|
|
4
|
+
"description": "piping without the operator support",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "nyc --reporter=text --reporter=html mocha test",
|
|
8
|
+
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
|
9
|
+
"lint": "eslint --fix src/"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"piping",
|
|
13
|
+
"promise",
|
|
14
|
+
"chain",
|
|
15
|
+
"async",
|
|
16
|
+
"pipe",
|
|
17
|
+
"pipe-operator",
|
|
18
|
+
"pipes-values"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/egeozcan/ppipe.git"
|
|
23
|
+
},
|
|
24
|
+
"author": "Yavuz Ege Özcan",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"chai": "^4.2.0",
|
|
28
|
+
"coveralls": "^3.1.0",
|
|
29
|
+
"eslint": "^8.12.0",
|
|
30
|
+
"eslint-config-prettier": "^8.5.0",
|
|
31
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
32
|
+
"mocha": "^9.2.2",
|
|
33
|
+
"nyc": "^15.1.0",
|
|
34
|
+
"prettier": "^2.6.1"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/getPropertyByPath.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//Mostly taken from https://stackoverflow.com/a/6491621/300011
|
|
1
|
+
// Mostly taken from https://stackoverflow.com/a/6491621/300011
|
|
2
2
|
const isPromise = require("./lib/isPromise");
|
|
3
3
|
|
|
4
4
|
module.exports = function getPropertyByPath(object, accessString) {
|
|
@@ -6,13 +6,15 @@ module.exports = function getPropertyByPath(object, accessString) {
|
|
|
6
6
|
accessString = accessString.replace(/\[(\w+)\]/g, ".$1");
|
|
7
7
|
// strip a leading dot
|
|
8
8
|
accessString = accessString.replace(/^\./, "");
|
|
9
|
+
|
|
9
10
|
const properties = accessString.split(".");
|
|
11
|
+
|
|
10
12
|
for (let i = 0, n = properties.length; i < n; ++i) {
|
|
11
13
|
const property = properties[i];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
: getProperty(property, object);
|
|
14
|
+
|
|
15
|
+
object = isPromise(object) ? object.then((x) => getProperty(property, x)) : getProperty(property, object);
|
|
15
16
|
}
|
|
17
|
+
|
|
16
18
|
return object;
|
|
17
19
|
};
|
|
18
20
|
|
|
@@ -20,14 +22,14 @@ function getProperty(property, object) {
|
|
|
20
22
|
if (object === undefined) {
|
|
21
23
|
return;
|
|
22
24
|
}
|
|
25
|
+
|
|
23
26
|
let call = false;
|
|
27
|
+
|
|
24
28
|
if (property.endsWith("()")) {
|
|
25
29
|
call = true;
|
|
26
30
|
property = property.substr(0, property.length - 2);
|
|
27
31
|
}
|
|
28
32
|
if (property in object) {
|
|
29
33
|
return call ? object[property]() : object[property];
|
|
30
|
-
} else {
|
|
31
|
-
return;
|
|
32
34
|
}
|
|
33
35
|
}
|
package/src/index.js
CHANGED
|
@@ -1,40 +1,45 @@
|
|
|
1
1
|
const isFn = require("./lib/isFunction");
|
|
2
2
|
const getPropertyByPath = require("./getPropertyByPath");
|
|
3
3
|
const isPromise = require("./lib/isPromise");
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
|
|
5
|
+
const unitFn = (x) => x;
|
|
6
|
+
const isUndef = (val) => typeof val === "undefined";
|
|
7
|
+
const truthy = (val) => !isUndef(val) && val !== null;
|
|
7
8
|
|
|
8
9
|
function createPpipe(extensions = {}) {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
function ppipe(val, thisVal, err) {
|
|
11
|
+
function pipe(fn, ...params) {
|
|
11
12
|
if (isUndef(fn)) {
|
|
12
13
|
if (truthy(err)) {
|
|
13
14
|
throw err;
|
|
14
15
|
}
|
|
16
|
+
|
|
15
17
|
return val;
|
|
16
18
|
}
|
|
19
|
+
|
|
17
20
|
if (!isFn(fn)) {
|
|
18
21
|
if (fn instanceof Placeholder && params.length === 0) {
|
|
19
22
|
params = [fn];
|
|
20
23
|
fn = unitFn;
|
|
21
24
|
} else {
|
|
22
|
-
throw new Error(
|
|
23
|
-
"first parameter to a pipe should be a function or a single placeholder"
|
|
24
|
-
);
|
|
25
|
+
throw new Error("first parameter to a pipe should be a function or a single placeholder");
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
+
|
|
29
|
+
const callResultFn = (value) => {
|
|
28
30
|
let replacedPlaceHolder = false;
|
|
31
|
+
|
|
29
32
|
for (let i = params.length; i >= 0; i--) {
|
|
30
33
|
const pholdr = params[i];
|
|
34
|
+
|
|
31
35
|
if (!(pholdr instanceof Placeholder)) {
|
|
32
36
|
continue;
|
|
33
37
|
}
|
|
38
|
+
|
|
34
39
|
replacedPlaceHolder = true;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
|
|
41
|
+
const replacedParam = !pholdr.prop ? value : getPropertyByPath(value, pholdr.prop);
|
|
42
|
+
|
|
38
43
|
pholdr.expandTarget === true
|
|
39
44
|
? params.splice(i, 1, ...replacedParam)
|
|
40
45
|
: params.splice(i, 1, replacedParam);
|
|
@@ -42,9 +47,11 @@ function createPpipe(extensions = {}) {
|
|
|
42
47
|
if (!replacedPlaceHolder) {
|
|
43
48
|
params.splice(params.length, 0, value);
|
|
44
49
|
}
|
|
50
|
+
|
|
45
51
|
return fn.call(thisVal, ...params);
|
|
46
52
|
};
|
|
47
53
|
let res;
|
|
54
|
+
|
|
48
55
|
if (isPromise(val)) {
|
|
49
56
|
res = val.then(callResultFn);
|
|
50
57
|
} else {
|
|
@@ -54,27 +61,29 @@ function createPpipe(extensions = {}) {
|
|
|
54
61
|
err = e;
|
|
55
62
|
}
|
|
56
63
|
}
|
|
64
|
+
|
|
57
65
|
return ppipe(res, undefined, err);
|
|
58
|
-
}
|
|
66
|
+
}
|
|
67
|
+
|
|
59
68
|
const piped = new Proxy(pipe, {
|
|
60
69
|
get(target, name) {
|
|
61
70
|
switch (name) {
|
|
62
71
|
case "then":
|
|
63
72
|
case "catch": {
|
|
64
|
-
const res = truthy(err)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return (...params) =>
|
|
68
|
-
name === "then" ? res.then(...params) : res.catch(...params);
|
|
73
|
+
const res = truthy(err) ? Promise.reject(err) : Promise.resolve(val);
|
|
74
|
+
|
|
75
|
+
return (...params) => (name === "then" ? res.then(...params) : res.catch(...params));
|
|
69
76
|
}
|
|
70
77
|
case "val":
|
|
71
78
|
if (truthy(err)) {
|
|
72
79
|
throw err;
|
|
73
80
|
}
|
|
81
|
+
|
|
74
82
|
return val;
|
|
75
83
|
case "with":
|
|
76
|
-
return ctx => {
|
|
84
|
+
return (ctx) => {
|
|
77
85
|
thisVal = ctx;
|
|
86
|
+
|
|
78
87
|
return piped;
|
|
79
88
|
};
|
|
80
89
|
case "pipe":
|
|
@@ -82,43 +91,45 @@ function createPpipe(extensions = {}) {
|
|
|
82
91
|
case "bind":
|
|
83
92
|
case "call":
|
|
84
93
|
case "apply":
|
|
85
|
-
return (...params) =>
|
|
86
|
-
return pipe[name](...params);
|
|
87
|
-
};
|
|
94
|
+
return (...params) => pipe[name](...params);
|
|
88
95
|
}
|
|
96
|
+
|
|
89
97
|
if (isPromise(val)) {
|
|
90
98
|
return (...params) =>
|
|
91
|
-
piped(x => {
|
|
99
|
+
piped((x) => {
|
|
92
100
|
if (isUndef(x[name])) {
|
|
93
101
|
throw new TypeError(`${name} is not defined on ${x}`);
|
|
94
102
|
}
|
|
103
|
+
|
|
95
104
|
return isFn(x[name]) ? x[name](...params) : x[name];
|
|
96
105
|
});
|
|
97
106
|
}
|
|
107
|
+
|
|
98
108
|
const fnExistsInCtx = truthy(thisVal) && isFn(thisVal[name]);
|
|
99
109
|
const valHasProp = !fnExistsInCtx && !isUndef(val[name]);
|
|
100
|
-
const extensionWithNameExists =
|
|
101
|
-
|
|
110
|
+
const extensionWithNameExists = !fnExistsInCtx && !valHasProp && isFn(extensions[name]);
|
|
111
|
+
|
|
102
112
|
if (fnExistsInCtx || valHasProp || extensionWithNameExists) {
|
|
103
113
|
const ctx = fnExistsInCtx ? thisVal : valHasProp ? val : extensions;
|
|
114
|
+
|
|
104
115
|
return (...params) =>
|
|
105
116
|
piped((...replacedParams) => {
|
|
106
|
-
const newParams =
|
|
107
|
-
|
|
108
|
-
? replacedParams
|
|
109
|
-
: params;
|
|
117
|
+
const newParams = fnExistsInCtx || extensionWithNameExists ? replacedParams : params;
|
|
118
|
+
|
|
110
119
|
return !isFn(ctx[name]) ? ctx[name] : ctx[name](...newParams);
|
|
111
120
|
}, ...params);
|
|
112
121
|
}
|
|
113
|
-
}
|
|
122
|
+
},
|
|
114
123
|
});
|
|
124
|
+
|
|
115
125
|
return piped;
|
|
116
|
-
}
|
|
126
|
+
}
|
|
127
|
+
|
|
117
128
|
return Object.assign(ppipe, {
|
|
118
129
|
extend(newExtensions) {
|
|
119
130
|
return createPpipe(Object.assign(newExtensions, extensions));
|
|
120
131
|
},
|
|
121
|
-
_
|
|
132
|
+
_,
|
|
122
133
|
});
|
|
123
134
|
}
|
|
124
135
|
class Placeholder {
|
|
@@ -132,19 +143,16 @@ class Placeholder {
|
|
|
132
143
|
}
|
|
133
144
|
}
|
|
134
145
|
|
|
135
|
-
const placeholderProxy = (prop = undefined, expandTarget = false) =>
|
|
136
|
-
|
|
146
|
+
const placeholderProxy = (prop = undefined, expandTarget = false) =>
|
|
147
|
+
new Proxy(new Placeholder(prop, expandTarget), {
|
|
137
148
|
get(target, name) {
|
|
138
|
-
if (
|
|
139
|
-
name === Symbol.iterator ||
|
|
140
|
-
Object.getOwnPropertyNames(target).includes(name)
|
|
141
|
-
) {
|
|
149
|
+
if (name === Symbol.iterator || Object.getOwnPropertyNames(target).includes(name)) {
|
|
142
150
|
return target[name];
|
|
143
151
|
}
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
|
|
153
|
+
return placeholderProxy([prop, name].filter((x) => !!x).join("."));
|
|
154
|
+
},
|
|
146
155
|
});
|
|
147
|
-
};
|
|
148
156
|
|
|
149
157
|
const _ = placeholderProxy();
|
|
150
158
|
|
package/src/lib/isFunction.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = val => typeof val === "function";
|
|
1
|
+
module.exports = (val) => typeof val === "function";
|
package/src/lib/isPromise.js
CHANGED
package/test/examples.js
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
let assert = require("chai").assert;
|
|
2
|
-
let ppipe = require("../src/index.js");
|
|
3
|
-
|
|
4
|
-
const add = (x, y) => x + y;
|
|
5
|
-
const square = x => x * x;
|
|
6
|
-
const divide = (x, y) => x / y;
|
|
7
|
-
const double = x => x * 2;
|
|
8
|
-
|
|
9
|
-
let _ = ppipe._;
|
|
10
|
-
|
|
11
|
-
const delay = fn => (...args) =>
|
|
12
|
-
new Promise(resolve => setTimeout(() => resolve(fn.apply(null, args)), 10));
|
|
13
|
-
const someAPICall = delay(x => x);
|
|
14
|
-
|
|
15
|
-
describe("check readme", function() {
|
|
16
|
-
it("first example", function() {
|
|
17
|
-
const res = ppipe(1)(add, 1)(double)(square)(divide, ppipe._, 8)(add, 1)();
|
|
18
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("second example", function() {
|
|
22
|
-
const res = ppipe(1)
|
|
23
|
-
.pipe(add, 1)
|
|
24
|
-
.pipe(double)
|
|
25
|
-
.pipe(square)
|
|
26
|
-
.pipe(divide, _, 8)
|
|
27
|
-
.pipe(add, 1)();
|
|
28
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("third example", async function() {
|
|
32
|
-
async function asyncDouble(x) {
|
|
33
|
-
const result = x * 2;
|
|
34
|
-
await someAPICall(result);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
const res = await ppipe(1)
|
|
38
|
-
.pipe(add, 1)
|
|
39
|
-
.pipe(asyncDouble)
|
|
40
|
-
.pipe(square)
|
|
41
|
-
.pipe(divide, _, 8)
|
|
42
|
-
.pipe(add, 1);
|
|
43
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("fourth example", async function() {
|
|
47
|
-
async function asyncComplexDouble(x) {
|
|
48
|
-
const result = x * 2;
|
|
49
|
-
const someInfo = await someAPICall(result);
|
|
50
|
-
return {
|
|
51
|
-
result,
|
|
52
|
-
someInfo,
|
|
53
|
-
getResultPlus: y => result + y
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
const res = await ppipe(1)
|
|
57
|
-
.pipe(add, 1)
|
|
58
|
-
.pipe(asyncComplexDouble)
|
|
59
|
-
.pipe(square, _.result)
|
|
60
|
-
.pipe(divide, _, 8)
|
|
61
|
-
.pipe(add, 1);
|
|
62
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
63
|
-
const res2 = await ppipe(1)
|
|
64
|
-
.pipe(add, 1)
|
|
65
|
-
.pipe(asyncComplexDouble)
|
|
66
|
-
.result()
|
|
67
|
-
.pipe(asyncComplexDouble)
|
|
68
|
-
.getResultPlus(2)
|
|
69
|
-
.pipe(square)
|
|
70
|
-
.pipe(divide, _, 8)
|
|
71
|
-
.pipe(add, 1)
|
|
72
|
-
.pipe(add, -2.5);
|
|
73
|
-
assert.equal(11, res2);
|
|
74
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("fourth example async result", async function() {
|
|
78
|
-
async function asyncComplexDouble(x) {
|
|
79
|
-
const result = x * 2;
|
|
80
|
-
const someInfo = await someAPICall(result);
|
|
81
|
-
//go wild with deferring
|
|
82
|
-
return Promise.resolve({
|
|
83
|
-
result,
|
|
84
|
-
someInfo,
|
|
85
|
-
//go wilder with deferring
|
|
86
|
-
getResultPlusAsync: y =>
|
|
87
|
-
new Promise(resolve => setTimeout(() => resolve(result + y), 10))
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const res3 = await ppipe(1)
|
|
91
|
-
.pipe(add, 1)
|
|
92
|
-
.pipe(asyncComplexDouble)
|
|
93
|
-
.result()
|
|
94
|
-
.pipe(asyncComplexDouble)
|
|
95
|
-
.getResultPlusAsync(2)
|
|
96
|
-
.pipe(square)
|
|
97
|
-
.pipe(divide, _, 8)
|
|
98
|
-
.pipe(add, 1)
|
|
99
|
-
.pipe(add, -2.5);
|
|
100
|
-
assert.equal(11, res3);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it("fifth example", async function() {
|
|
104
|
-
async function advancedDouble(x) {
|
|
105
|
-
const result = x * 2;
|
|
106
|
-
const someInfo = await someAPICall(result);
|
|
107
|
-
return {
|
|
108
|
-
getResult() {
|
|
109
|
-
return result;
|
|
110
|
-
},
|
|
111
|
-
someInfo
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const res = await ppipe(1)
|
|
115
|
-
.pipe(add, 1)
|
|
116
|
-
.pipe(advancedDouble)
|
|
117
|
-
.getResult()
|
|
118
|
-
.pipe(square)
|
|
119
|
-
.pipe(divide, _, 8)
|
|
120
|
-
.pipe(add, 1);
|
|
121
|
-
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
122
|
-
const res2 = await ppipe(1)
|
|
123
|
-
.pipe(add, 1)
|
|
124
|
-
.pipe(x => Promise.resolve(x))
|
|
125
|
-
//.pipe((...params) => (console.log(params), params[0]))
|
|
126
|
-
.pipe(advancedDouble)
|
|
127
|
-
.getResult()
|
|
128
|
-
.toFixed(2)
|
|
129
|
-
.pipe(parseInt)
|
|
130
|
-
.pipe(square)
|
|
131
|
-
.pipe(divide, _, 8)
|
|
132
|
-
.pipe(add, 1);
|
|
133
|
-
assert.equal(res2, 3);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("sixth example", async function() {
|
|
137
|
-
class Example {
|
|
138
|
-
constructor(myInt) {
|
|
139
|
-
this.foo = Promise.resolve(myInt);
|
|
140
|
-
}
|
|
141
|
-
addToFoo(x) {
|
|
142
|
-
return this.foo.then(foo => foo + x);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
const res = await ppipe(10)
|
|
146
|
-
.with(new Example(5))
|
|
147
|
-
.addToFoo(_);
|
|
148
|
-
assert.equal(res, 15);
|
|
149
|
-
const res2 = await ppipe(10)
|
|
150
|
-
.with(new Example(5))
|
|
151
|
-
.addToFoo();
|
|
152
|
-
assert.equal(res2, 15);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("seventh example", async function() {
|
|
156
|
-
let logged = false;
|
|
157
|
-
const newPipe = ppipe.extend({
|
|
158
|
-
divide(x, y) {
|
|
159
|
-
return x / y;
|
|
160
|
-
},
|
|
161
|
-
log(...params) {
|
|
162
|
-
logged = true;
|
|
163
|
-
assert.equal(params[params.length - 1], 1);
|
|
164
|
-
return params[params.length - 1];
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
const res = await newPipe(10)
|
|
168
|
-
.pipe(x => x + 1)
|
|
169
|
-
.divide(_, 11)
|
|
170
|
-
.log("here is our x: ")
|
|
171
|
-
.pipe(x => x + 1);
|
|
172
|
-
assert.equal(res, 2);
|
|
173
|
-
assert.equal(logged, true);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
1
|
+
let assert = require("chai").assert;
|
|
2
|
+
let ppipe = require("../src/index.js");
|
|
3
|
+
|
|
4
|
+
const add = (x, y) => x + y;
|
|
5
|
+
const square = x => x * x;
|
|
6
|
+
const divide = (x, y) => x / y;
|
|
7
|
+
const double = x => x * 2;
|
|
8
|
+
|
|
9
|
+
let _ = ppipe._;
|
|
10
|
+
|
|
11
|
+
const delay = fn => (...args) =>
|
|
12
|
+
new Promise(resolve => setTimeout(() => resolve(fn.apply(null, args)), 10));
|
|
13
|
+
const someAPICall = delay(x => x);
|
|
14
|
+
|
|
15
|
+
describe("check readme", function() {
|
|
16
|
+
it("first example", function() {
|
|
17
|
+
const res = ppipe(1)(add, 1)(double)(square)(divide, ppipe._, 8)(add, 1)();
|
|
18
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("second example", function() {
|
|
22
|
+
const res = ppipe(1)
|
|
23
|
+
.pipe(add, 1)
|
|
24
|
+
.pipe(double)
|
|
25
|
+
.pipe(square)
|
|
26
|
+
.pipe(divide, _, 8)
|
|
27
|
+
.pipe(add, 1)();
|
|
28
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("third example", async function() {
|
|
32
|
+
async function asyncDouble(x) {
|
|
33
|
+
const result = x * 2;
|
|
34
|
+
await someAPICall(result);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
const res = await ppipe(1)
|
|
38
|
+
.pipe(add, 1)
|
|
39
|
+
.pipe(asyncDouble)
|
|
40
|
+
.pipe(square)
|
|
41
|
+
.pipe(divide, _, 8)
|
|
42
|
+
.pipe(add, 1);
|
|
43
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("fourth example", async function() {
|
|
47
|
+
async function asyncComplexDouble(x) {
|
|
48
|
+
const result = x * 2;
|
|
49
|
+
const someInfo = await someAPICall(result);
|
|
50
|
+
return {
|
|
51
|
+
result,
|
|
52
|
+
someInfo,
|
|
53
|
+
getResultPlus: y => result + y
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const res = await ppipe(1)
|
|
57
|
+
.pipe(add, 1)
|
|
58
|
+
.pipe(asyncComplexDouble)
|
|
59
|
+
.pipe(square, _.result)
|
|
60
|
+
.pipe(divide, _, 8)
|
|
61
|
+
.pipe(add, 1);
|
|
62
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
63
|
+
const res2 = await ppipe(1)
|
|
64
|
+
.pipe(add, 1)
|
|
65
|
+
.pipe(asyncComplexDouble)
|
|
66
|
+
.result()
|
|
67
|
+
.pipe(asyncComplexDouble)
|
|
68
|
+
.getResultPlus(2)
|
|
69
|
+
.pipe(square)
|
|
70
|
+
.pipe(divide, _, 8)
|
|
71
|
+
.pipe(add, 1)
|
|
72
|
+
.pipe(add, -2.5);
|
|
73
|
+
assert.equal(11, res2);
|
|
74
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("fourth example async result", async function() {
|
|
78
|
+
async function asyncComplexDouble(x) {
|
|
79
|
+
const result = x * 2;
|
|
80
|
+
const someInfo = await someAPICall(result);
|
|
81
|
+
//go wild with deferring
|
|
82
|
+
return Promise.resolve({
|
|
83
|
+
result,
|
|
84
|
+
someInfo,
|
|
85
|
+
//go wilder with deferring
|
|
86
|
+
getResultPlusAsync: y =>
|
|
87
|
+
new Promise(resolve => setTimeout(() => resolve(result + y), 10))
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const res3 = await ppipe(1)
|
|
91
|
+
.pipe(add, 1)
|
|
92
|
+
.pipe(asyncComplexDouble)
|
|
93
|
+
.result()
|
|
94
|
+
.pipe(asyncComplexDouble)
|
|
95
|
+
.getResultPlusAsync(2)
|
|
96
|
+
.pipe(square)
|
|
97
|
+
.pipe(divide, _, 8)
|
|
98
|
+
.pipe(add, 1)
|
|
99
|
+
.pipe(add, -2.5);
|
|
100
|
+
assert.equal(11, res3);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("fifth example", async function() {
|
|
104
|
+
async function advancedDouble(x) {
|
|
105
|
+
const result = x * 2;
|
|
106
|
+
const someInfo = await someAPICall(result);
|
|
107
|
+
return {
|
|
108
|
+
getResult() {
|
|
109
|
+
return result;
|
|
110
|
+
},
|
|
111
|
+
someInfo
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const res = await ppipe(1)
|
|
115
|
+
.pipe(add, 1)
|
|
116
|
+
.pipe(advancedDouble)
|
|
117
|
+
.getResult()
|
|
118
|
+
.pipe(square)
|
|
119
|
+
.pipe(divide, _, 8)
|
|
120
|
+
.pipe(add, 1);
|
|
121
|
+
assert.equal(res, add(divide(square(double(add(1, 1))), 8), 1));
|
|
122
|
+
const res2 = await ppipe(1)
|
|
123
|
+
.pipe(add, 1)
|
|
124
|
+
.pipe(x => Promise.resolve(x))
|
|
125
|
+
//.pipe((...params) => (console.log(params), params[0]))
|
|
126
|
+
.pipe(advancedDouble)
|
|
127
|
+
.getResult()
|
|
128
|
+
.toFixed(2)
|
|
129
|
+
.pipe(parseInt)
|
|
130
|
+
.pipe(square)
|
|
131
|
+
.pipe(divide, _, 8)
|
|
132
|
+
.pipe(add, 1);
|
|
133
|
+
assert.equal(res2, 3);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("sixth example", async function() {
|
|
137
|
+
class Example {
|
|
138
|
+
constructor(myInt) {
|
|
139
|
+
this.foo = Promise.resolve(myInt);
|
|
140
|
+
}
|
|
141
|
+
addToFoo(x) {
|
|
142
|
+
return this.foo.then(foo => foo + x);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const res = await ppipe(10)
|
|
146
|
+
.with(new Example(5))
|
|
147
|
+
.addToFoo(_);
|
|
148
|
+
assert.equal(res, 15);
|
|
149
|
+
const res2 = await ppipe(10)
|
|
150
|
+
.with(new Example(5))
|
|
151
|
+
.addToFoo();
|
|
152
|
+
assert.equal(res2, 15);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("seventh example", async function() {
|
|
156
|
+
let logged = false;
|
|
157
|
+
const newPipe = ppipe.extend({
|
|
158
|
+
divide(x, y) {
|
|
159
|
+
return x / y;
|
|
160
|
+
},
|
|
161
|
+
log(...params) {
|
|
162
|
+
logged = true;
|
|
163
|
+
assert.equal(params[params.length - 1], 1);
|
|
164
|
+
return params[params.length - 1];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
const res = await newPipe(10)
|
|
168
|
+
.pipe(x => x + 1)
|
|
169
|
+
.divide(_, 11)
|
|
170
|
+
.log("here is our x: ")
|
|
171
|
+
.pipe(x => x + 1);
|
|
172
|
+
assert.equal(res, 2);
|
|
173
|
+
assert.equal(logged, true);
|
|
174
|
+
});
|
|
175
|
+
});
|