File: ifElse.js

package info (click to toggle)
node-array-from 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 644 kB
  • sloc: makefile: 2
file content (27 lines) | stat: -rw-r--r-- 711 bytes parent folder | download | duplicates (2)
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
/**
 * @module 1-liners/ifElse
 * 
 * @description
 *
 * Creates a function which calls the first function if the predicate is true
 * and the second function if the predicate is false.
 * 
 * @example
 * 
 * 	let ifElse = require('1-liners/ifElse');
 * 
 * 	let eq = (a, b) => a === b;
 * 	let add = (a, b) => a + b;
 * 	let sub = (a, b) => a - b;
 * 
 * 	let addIfEq = ifElse(eq, add, sub);
 * 
 * 	addIfEq(1, 1); // => 2
 * 	addIfEq(2, 1); // => 1
 * 
 * 	let words = ifElse((str) => typeof str === 'string', (str) => str.split(' '));
 * 
 * 	words('Hello ES2015'); // => ['Hello', 'ES2015']
 * 
 */
export default (pred, ifDo, elseDo=()=>{}) => (...args) => pred(...args) ? ifDo(...args) : elseDo(...args);