| 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 |
| 12 |
* checked for a substring of `value`, otherwise |
| 13 |
* [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) |
| 14 |
* is used for equality comparisons. If `fromIndex` is negative, it's used as |
| 15 |
* the offset from the end of `collection`. |
| 16 |
* |
| 17 |
* @static |
| 18 |
* @memberOf _ |
| 19 |
* @since 0.1.0 |
| 20 |
* @category Collection |
| 21 |
* @param {Array|Object|string} collection The collection to inspect. |
| 22 |
* @param {*} value The value to search for. |
| 23 |
* @param {number} [fromIndex=0] The index to search from. |
| 24 |
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`. |
| 25 |
* @returns {boolean} Returns `true` if `value` is found, else `false`. |
| 26 |
* @example |
| 27 |
* |
| 28 |
* _.includes([1, 2, 3], 1); |
| 29 |
* // => true |
| 30 |
* |
| 31 |
* _.includes([1, 2, 3], 1, 2); |
| 32 |
* // => false |
| 33 |
* |
| 34 |
* _.includes({ 'a': 1, 'b': 2 }, 1); |
| 35 |
* // => true |
| 36 |
* |
| 37 |
* _.includes('abcd', 'bc'); |
| 38 |
* // => true |
| 39 |
*/ |
| 40 |
function includes(collection, value, fromIndex, guard) { |
| 41 |
collection = isArrayLike(collection) ? collection : values(collection); |
| 42 |
fromIndex = (fromIndex && !guard) ? toInteger(fromIndex) : 0; |
| 43 |
|
| 44 |
var length = collection.length; |
| 45 |
if (fromIndex < 0) { |
| 46 |
fromIndex = nativeMax(length + fromIndex, 0); |
| 47 |
} |
| 48 |
return isString(collection) |
| 49 |
? (fromIndex <= length && collection.indexOf(value, fromIndex) > -1) |
| 50 |
: (!!length && baseIndexOf(collection, value, fromIndex) > -1); |
| 51 |
} |
| 52 |
|
| 53 |
module.exports = includes; |