-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.js
More file actions
30 lines (25 loc) · 777 Bytes
/
pipe.js
File metadata and controls
30 lines (25 loc) · 777 Bytes
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
'use strict';
// From "Mastering JavaScript Functional Programming", Federico Kereki
const pipeKereki = (...fns) =>
fns.reduce((result, f) =>
(...args) =>
f(result(...args)));
// Functionally the same as pipeKereki but more clear IMO
const pipe = (f1, ...restFns) =>
(...args) =>
restFns.reduce((result, f) =>
f(result), f1(...args));
const asyncPipe = (...fns) =>
async (args) => {
let _args = args;
for(let f of fns) {
_args = f.constructor.name == 'AsyncFunction' ? await f(_args) : f(_args);
}
return _args;
}
// From "Mastering JavaScript Functional Programming", Federico Kereki
const tap = fn => x => (fn(x), x)
// Usage:
// const tee = tap(fn)
// pipe(..., tee, ...)(...args)
module.exports = {pipe, asyncPipe, tap}