diff --git a/lodash.js b/lodash.js index e54014049..f981ba8cb 100644 --- a/lodash.js +++ b/lodash.js @@ -12820,6 +12820,39 @@ return object; }); + /** + * Creates a function that iterates over `pairs` invoking the corresponding + * function of the first predicate to return truthy. The predicate-function + * pairs are invoked with the `this` binding and arguments of the created + * function. + * + * @static + * @memberOf _ + * @category Utility + * @param {Array} pairs The predicate-function pairs. + * @returns {Function} Returns the new function. + * @example + */ + function cond(pairs) { + var length = pairs ? pairs.length : 0, + index = length; + + while (index--) { + if (typeof pairs[index][0] != 'function' || typeof pairs[index][1] != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + return rest(function(args) { + var index = -1; + while (++index < length) { + var pair = pairs[index]; + if (apply(pair[0], this, args)) { + return apply(pair[1], this, args); + } + } + }); + } + /** * Creates a function that invokes the predicate properties of `source` with * the corresponding property values of a given object, returning `true` if @@ -13838,6 +13871,7 @@ lodash.chunk = chunk; lodash.compact = compact; lodash.concat = concat; + lodash.cond = cond; lodash.conforms = conforms; lodash.constant = constant; lodash.countBy = countBy; diff --git a/test/test.js b/test/test.js index d0e754d63..d18e590d3 100644 --- a/test/test.js +++ b/test/test.js @@ -22950,7 +22950,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(285); + assert.expect(286); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); @@ -23021,7 +23021,7 @@ func = _[methodName]; var actual = lodashStable.map(falsey, function(value, index) { - var pass = !index && /^(?:backflow|compose|flow(Right)?|over(?:Every|Some)?)$/.test(methodName); + var pass = !index && /^(?:backflow|compose|cond|flow(Right)?|over(?:Every|Some)?)$/.test(methodName); try { index ? func(value) : func();