Add _.conj, _.disj, & _.juxt.

This commit is contained in:
John-David Dalton
2015-09-23 22:45:55 -07:00
parent 12af298e33
commit ee2078f3ea
2 changed files with 90 additions and 2 deletions

View File

@@ -3596,6 +3596,25 @@
return wrapper;
}
/**
* Creates a function like `_.conj`.
*
* @private
* @param {Function} arrayFunc The function to iterate over `iteratees`.
* @returns {Function} Returns the new invoker function.
*/
function createInvoker(arrayFunc) {
return restParam(function(iteratees) {
iteratees = arrayMap(baseFlatten(iteratees), getIteratee());
return restParam(function(args) {
var thisArg = this;
return arrayFunc(iteratees, function(iteratee) {
return iteratee.apply(thisArg, args);
});
});
});
}
/**
* Creates a function like `_.modArgs`.
*
@@ -7250,6 +7269,30 @@
return createWrapper(key, bitmask, object, partials, holders);
});
/**
* Creates a function that checks if **all** of the `predicates` return
* truthy when invoked with the arguments provided to the created function.
*
* @static
* @memberOf _
* @category Function
* @param {Function[]} predicates The predicates to check.
* @returns {Function} Returns the new function.
* @example
*
* var conjed = _.conj(Boolean, isFinite);
*
* conjed('1');
* // => true
*
* conjed(null);
* // => false
*
* conjed(NaN);
* // => false
*/
var conj = createInvoker(arrayEvery);
/**
* Creates a function that accepts one or more arguments of `func` that when
* called either invokes `func` returning its result, if all `func` arguments
@@ -7553,6 +7596,30 @@
return baseDelay(func, wait, args);
});
/**
* Creates a function that checks if **any** of the `predicates` return
* truthy when invoked with the arguments provided to the created function.
*
* @static
* @memberOf _
* @category Function
* @param {Function[]} predicates The predicates to check.
* @returns {Function} Returns the new function.
* @example
*
* var disjed = _.disj(Boolean, isFinite);
*
* disjed('1');
* // => true
*
* disjed(null);
* // => true
*
* disjed(NaN);
* // => false
*/
var disj = createInvoker(arraySome);
/**
* Creates a function that invokes `func` with arguments reversed.
*
@@ -7617,6 +7684,24 @@
*/
var flowRight = createFlow(true);
/**
* Creates a function that invokes `iteratees` with the arguments provided
* to the created function and returns their results.
*
* @static
* @memberOf _
* @category Function
* @param {Function[]} iteratees The iteratees to invoke.
* @returns {Function} Returns the new function.
* @example
*
* var juxted = _.juxt(Math.max, Math.min);
*
* juxted(1, 2, 3, 4);
* // => [4, 1]
*/
var juxt = createInvoker(arrayMap);
/**
* Creates a function that memoizes the result of `func`. If `resolver` is
* provided it determines the cache key for storing the result based on the
@@ -12024,6 +12109,7 @@
lodash.chain = chain;
lodash.chunk = chunk;
lodash.compact = compact;
lodash.conj = conj;
lodash.constant = constant;
lodash.countBy = countBy;
lodash.create = create;
@@ -12035,6 +12121,7 @@
lodash.defer = defer;
lodash.delay = delay;
lodash.difference = difference;
lodash.disj = disj;
lodash.drop = drop;
lodash.dropRight = dropRight;
lodash.dropRightWhile = dropRightWhile;
@@ -12055,6 +12142,7 @@
lodash.invert = invert;
lodash.invoke = invoke;
lodash.iteratee = iteratee;
lodash.juxt = juxt;
lodash.keyBy = keyBy;
lodash.keys = keys;
lodash.keysIn = keysIn;

View File

@@ -20952,7 +20952,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(235);
assert.expect(238);
var emptyArrays = _.map(falsey, _.constant([]));
@@ -21023,7 +21023,7 @@
func = _[methodName];
var actual = _.map(falsey, function(value, index) {
var pass = !index && /^(?:backflow|compose|flow(Right)?)$/.test(methodName);
var pass = !index && /^(?:backflow|compose|conj|disj|flow(Right)?)$/.test(methodName);
try {
index ? func(value) : func();