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');
9 /* Native method references for those with the same name as other `lodash` methods. */
10 var nativeMax = Math.max;
13 * Checks if `value` is in `collection` using `SameValueZero` for equality
14 * comparisons. If `fromIndex` is negative, it is used as the offset from
15 * the end of `collection`.
17 * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
18 * comparisons are like strict equality comparisons, e.g. `===`, except that
19 * `NaN` matches `NaN`.
23 * @alias contains, include
24 * @category Collection
25 * @param {Array|Object|string} collection The collection to search.
26 * @param {*} target The value to search for.
27 * @param {number} [fromIndex=0] The index to search from.
28 * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
29 * @returns {boolean} Returns `true` if a matching element is found, else `false`.
32 * _.includes([1, 2, 3], 1);
35 * _.includes([1, 2, 3], 1, 2);
38 * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
41 * _.includes('pebbles', 'eb');
44 function includes(collection, target, fromIndex, guard) {
45 var length = collection ? getLength(collection) : 0;
46 if (!isLength(length)) {
47 collection = values(collection);
48 length = collection.length;
53 if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
56 fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
58 return (typeof collection == 'string' || !isArray(collection) && isString(collection))
59 ? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
60 : (baseIndexOf(collection, target, fromIndex) > -1);
63 module.exports = includes;