mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Add iteratee guard to _.includes.
This commit is contained in:
@@ -6342,6 +6342,7 @@
|
|||||||
* @param {Array|Object|string} collection The collection to search.
|
* @param {Array|Object|string} collection The collection to search.
|
||||||
* @param {*} target The value to search for.
|
* @param {*} target The value to search for.
|
||||||
* @param {number} [fromIndex=0] The index to search from.
|
* @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`.
|
* @returns {boolean} Returns `true` if a matching element is found, else `false`.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
@@ -6357,7 +6358,7 @@
|
|||||||
* _.includes('pebbles', 'eb');
|
* _.includes('pebbles', 'eb');
|
||||||
* // => true
|
* // => true
|
||||||
*/
|
*/
|
||||||
function includes(collection, target, fromIndex) {
|
function includes(collection, target, fromIndex, guard) {
|
||||||
var length = collection ? collection.length : 0;
|
var length = collection ? collection.length : 0;
|
||||||
if (!isLength(length)) {
|
if (!isLength(length)) {
|
||||||
collection = values(collection);
|
collection = values(collection);
|
||||||
@@ -6366,10 +6367,10 @@
|
|||||||
if (!length) {
|
if (!length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (typeof fromIndex == 'number') {
|
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
|
||||||
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
|
|
||||||
} else {
|
|
||||||
fromIndex = 0;
|
fromIndex = 0;
|
||||||
|
} else {
|
||||||
|
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
|
||||||
}
|
}
|
||||||
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
|
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
|
||||||
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
|
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
|
||||||
@@ -6616,7 +6617,7 @@
|
|||||||
* `_.reduce`, `_.reduceRight`, and `_.transform`.
|
* `_.reduce`, `_.reduceRight`, and `_.transform`.
|
||||||
*
|
*
|
||||||
* The guarded methods are:
|
* The guarded methods are:
|
||||||
* `assign`, `defaults`, `merge`, `sortByAll`, and `sortByOrder`
|
* `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder`
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
|
|||||||
@@ -6211,6 +6211,13 @@
|
|||||||
strictEqual(_.includes([-0], 0), true);
|
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() {
|
test('should be aliased', 2, function() {
|
||||||
strictEqual(_.contains, _.includes);
|
strictEqual(_.contains, _.includes);
|
||||||
strictEqual(_.include, _.includes);
|
strictEqual(_.include, _.includes);
|
||||||
|
|||||||
Reference in New Issue
Block a user