-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.pipe.js
More file actions
24 lines (24 loc) · 897 Bytes
/
Copy pathfunction.pipe.js
File metadata and controls
24 lines (24 loc) · 897 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
/**
* Composable function piping
* Works as a simple function compositor or using Hack pipe syntax
* @param {*} value The intial value to start with
* @param {function|array} funcs.. The functions to pipe the value via. If an array the format isassumed to be a Hack style pipe of the form `[function, args...]`
* @return {*} Eventual output value
* @url https://github.com/MomsFriendlyDevCo/Nodash
*
* @example Simple pipe using unary functions
* _.pipe(1, increment, increment) //=2
* _.pipe(1, v => v + 1, v => v + 1) //=2
*
* @example Hack style pipes using arrays
* _.pipe(1, [add, '%', 2]) //=3
*/
export default function pipe(value, ...funcs) {
return funcs.reduce((val, func) => {
if (Array.isArray(func)) { // Hack style syntax
return func[0].apply(this, func.slice(1).map(f => f === '%' ? val : f));
} else { // Assume regular function call
return func(val);
}
}, value);
}