Add _.cond.

This commit is contained in:
John-David Dalton
2016-01-01 21:51:14 -06:00
parent 3e155bdbb9
commit 1427c99169
2 changed files with 36 additions and 2 deletions

View File

@@ -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;

View File

@@ -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();