dcf6f20cfdad9fb093f594ecd30836a8a2fa316e
[platform/framework/web/crosswalk-tizen.git] /
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 `SameValueZero` for equality
14  * comparisons. If `fromIndex` is negative, it is used as the offset from
15  * the end of `collection`.
16  *
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`.
20  *
21  * @static
22  * @memberOf _
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`.
30  * @example
31  *
32  * _.includes([1, 2, 3], 1);
33  * // => true
34  *
35  * _.includes([1, 2, 3], 1, 2);
36  * // => false
37  *
38  * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
39  * // => true
40  *
41  * _.includes('pebbles', 'eb');
42  * // => true
43  */
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;
49   }
50   if (!length) {
51     return false;
52   }
53   if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
54     fromIndex = 0;
55   } else {
56     fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
57   }
58   return (typeof collection == 'string' || !isArray(collection) && isString(collection))
59     ? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
60     : (baseIndexOf(collection, target, fromIndex) > -1);
61 }
62
63 module.exports = includes;