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 `target` is in `collection` using
14 * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
15 * for equality comparisons. If `fromIndex` is negative, it's used as the offset
16 * from the end of `collection`.
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`.
29 * _.includes([1, 2, 3], 1);
32 * _.includes([1, 2, 3], 1, 2);
35 * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
38 * _.includes('pebbles', 'eb');
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;
47 if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
50 fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
52 return (typeof collection == 'string' || !isArray(collection) && isString(collection))
53 ? (fromIndex <= length && collection.indexOf(target, fromIndex) > -1)
54 : (!!length && baseIndexOf(collection, target, fromIndex) > -1);
57 module.exports = includes;