Add iteratee guard to _.includes.

This commit is contained in:
jdalton
2015-03-13 12:33:49 -07:00
parent c976b637d8
commit eee714f52b
2 changed files with 13 additions and 5 deletions

View File

@@ -6342,6 +6342,7 @@
* @param {Array|Object|string} collection The collection to search.
* @param {*} target The value to search for.
* @param {number} [fromIndex=0] The index to search from.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {boolean} Returns `true` if a matching element is found, else `false`.
* @example
*
@@ -6357,7 +6358,7 @@
* _.includes('pebbles', 'eb');
* // => true
*/
function includes(collection, target, fromIndex) {
function includes(collection, target, fromIndex, guard) {
var length = collection ? collection.length : 0;
if (!isLength(length)) {
collection = values(collection);
@@ -6366,10 +6367,10 @@
if (!length) {
return false;
}
if (typeof fromIndex == 'number') {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} else {
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
fromIndex = 0;
} else {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
}
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
@@ -6616,7 +6617,7 @@
* `_.reduce`, `_.reduceRight`, and `_.transform`.
*
* The guarded methods are:
* `assign`, `defaults`, `merge`, `sortByAll`, and `sortByOrder`
* `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder`
*
* @static
* @memberOf _

View File

@@ -6211,6 +6211,13 @@
strictEqual(_.includes([-0], 0), true);
});
test('should work as an iteratee for methods like `_.reduce`', 1, function() {
var array1 = [1, 2, 3],
array2 = [2, 3, 1];
ok(_.every(array1, _.partial(_.includes, array2)));
});
test('should be aliased', 2, function() {
strictEqual(_.contains, _.includes);
strictEqual(_.include, _.includes);