Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / collection / includes.js
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 `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`.
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 (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
48     fromIndex = 0;
49   } else {
50     fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
51   }
52   return (typeof collection == 'string' || !isArray(collection) && isString(collection))
53     ? (fromIndex <= length && collection.indexOf(target, fromIndex) > -1)
54     : (!!length && baseIndexOf(collection, target, fromIndex) > -1);
55 }
56
57 module.exports = includes;