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 (42 lines) | stat: -rw-r--r-- 1,070 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use strict";

exports.__esModule = true;
/**
 * @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']
 * 
 */

exports["default"] = function (pred, ifDo) {
  var elseDo = arguments[2] === undefined ? function () {} : arguments[2];
  return function () {
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return pred.apply(undefined, args) ? ifDo.apply(undefined, args) : elseDo.apply(undefined, args);
  };
};

module.exports = exports["default"];