Security update for permissions_by_term
[yaffs-website] / node_modules / uncss / node_modules / lodash / cond.js
1 var apply = require('./internal/apply'),
2     arrayMap = require('./internal/arrayMap'),
3     baseIteratee = require('./internal/baseIteratee'),
4     rest = require('./rest');
5
6 /** Used as the `TypeError` message for "Functions" methods. */
7 var FUNC_ERROR_TEXT = 'Expected a function';
8
9 /**
10  * Creates a function that iterates over `pairs` invoking the corresponding
11  * function of the first predicate to return truthy. The predicate-function
12  * pairs are invoked with the `this` binding and arguments of the created
13  * function.
14  *
15  * @static
16  * @memberOf _
17  * @category Util
18  * @param {Array} pairs The predicate-function pairs.
19  * @returns {Function} Returns the new function.
20  * @example
21  *
22  * var func = _.cond([
23  *   [_.matches({ 'a': 1 }),           _.constant('matches A')],
24  *   [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
25  *   [_.constant(true),                _.constant('no match')]
26  * ]);
27  *
28  * func({ 'a': 1, 'b': 2 });
29  * // => 'matches A'
30  *
31  * func({ 'a': 0, 'b': 1 });
32  * // => 'matches B'
33  *
34  * func({ 'a': '1', 'b': '2' });
35  * // => 'no match'
36  */
37 function cond(pairs) {
38   var length = pairs ? pairs.length : 0;
39
40   pairs = !length ? [] : arrayMap(pairs, function(pair) {
41     if (typeof pair[1] != 'function') {
42       throw new TypeError(FUNC_ERROR_TEXT);
43     }
44     return [baseIteratee(pair[0]), pair[1]];
45   });
46
47   return rest(function(args) {
48     var index = -1;
49     while (++index < length) {
50       var pair = pairs[index];
51       if (apply(pair[0], this, args)) {
52         return apply(pair[1], this, args);
53       }
54     }
55   });
56 }
57
58 module.exports = cond;