1 var apply = require('./internal/apply'),
2 arrayMap = require('./internal/arrayMap'),
3 baseIteratee = require('./internal/baseIteratee'),
4 rest = require('./rest');
6 /** Used as the `TypeError` message for "Functions" methods. */
7 var FUNC_ERROR_TEXT = 'Expected a function';
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
18 * @param {Array} pairs The predicate-function pairs.
19 * @returns {Function} Returns the new function.
23 * [_.matches({ 'a': 1 }), _.constant('matches A')],
24 * [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
25 * [_.constant(true), _.constant('no match')]
28 * func({ 'a': 1, 'b': 2 });
31 * func({ 'a': 0, 'b': 1 });
34 * func({ 'a': '1', 'b': '2' });
37 function cond(pairs) {
38 var length = pairs ? pairs.length : 0;
40 pairs = !length ? [] : arrayMap(pairs, function(pair) {
41 if (typeof pair[1] != 'function') {
42 throw new TypeError(FUNC_ERROR_TEXT);
44 return [baseIteratee(pair[0]), pair[1]];
47 return rest(function(args) {
49 while (++index < length) {
50 var pair = pairs[index];
51 if (apply(pair[0], this, args)) {
52 return apply(pair[1], this, args);
58 module.exports = cond;