01d684454162947311a7ea6422c3fde6b3afee6c
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / includes.js
1 var baseIndexOf = require('./_baseIndexOf'),
2     isArrayLike = require('./isArrayLike'),
3     isString = require('./isString'),
4     toInteger = require('./toInteger'),
5     values = require('./values');
6
7 /* Built-in method references for those with the same name as other `lodash` methods. */
8 var nativeMax = Math.max;
9
10 /**
11  * Checks if `value` is in `collection`. If `collection` is a string it's checked
12  * for a substring of `value`, otherwise [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
13  * is used for equality comparisons. If `fromIndex` is negative, it's used as
14  * the offset from the end of `collection`.
15  *
16  * @static
17  * @memberOf _
18  * @category Collection
19  * @param {Array|Object|string} collection The collection to search.
20  * @param {*} value The value to search for.
21  * @param {number} [fromIndex=0] The index to search from.
22  * @param- {Object} [guard] Enables use as an iteratee for functions like `_.reduce`.
23  * @returns {boolean} Returns `true` if `value` is found, else `false`.
24  * @example
25  *
26  * _.includes([1, 2, 3], 1);
27  * // => true
28  *
29  * _.includes([1, 2, 3], 1, 2);
30  * // => false
31  *
32  * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
33  * // => true
34  *
35  * _.includes('pebbles', 'eb');
36  * // => true
37  */
38 function includes(collection, value, fromIndex, guard) {
39   collection = isArrayLike(collection) ? collection : values(collection);
40   fromIndex = (fromIndex && !guard) ? toInteger(fromIndex) : 0;
41
42   var length = collection.length;
43   if (fromIndex < 0) {
44     fromIndex = nativeMax(length + fromIndex, 0);
45   }
46   return isString(collection)
47     ? (fromIndex <= length && collection.indexOf(value, fromIndex) > -1)
48     : (!!length && baseIndexOf(collection, value, fromIndex) > -1);
49 }
50
51 module.exports = includes;