Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseIndexOf = require('../internal/baseIndexOf'),
2
getLength = require('../internal/getLength'),
3
isArray = require('../lang/isArray'),
4
isIterateeCall = require('../internal/isIterateeCall'),
5
isLength = require('../internal/isLength'),
6
isString = require('../lang/isString'),
7
values = require('../object/values');
8
9
/* Native method references for those with the same name as other `lodash` methods. */
10
var nativeMax = Math.max;
11
12
/**
13
* Checks if `value` is in `collection` using
14
* [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
15
* for equality comparisons. If `fromIndex` is negative, it is used as the offset
16
* from the end of `collection`.
17
*
18
* @static
19
* @memberOf _
20
* @alias contains, include
21
* @category Collection
22
* @param {Array|Object|string} collection The collection to search.
23
* @param {*} target The value to search for.
24
* @param {number} [fromIndex=0] The index to search from.
25
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
26
* @returns {boolean} Returns `true` if a matching element is found, else `false`.
27
* @example
28
*
29
* _.includes([1, 2, 3], 1);
30
* // => true
31
*
32
* _.includes([1, 2, 3], 1, 2);
33
* // => false
34
*
35
* _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
36
* // => true
37
*
38
* _.includes('pebbles', 'eb');
39
* // => true
40
*/
41
function includes(collection, target, fromIndex, guard) {
42
var length = collection ? getLength(collection) : 0;
43
if (!isLength(length)) {
44
collection = values(collection);
45
length = collection.length;
46
}
47
if (!length) {
48
return false;
49
}
50
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
51
fromIndex = 0;
52
} else {
53
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
54
}
55
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
56
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
57
: (baseIndexOf(collection, target, fromIndex) > -1);
58
}
59
60
module.exports = includes;
61
62