1 | var toIndexedObject = require('../internals/to-indexed-object');
|
---|
2 | var toLength = require('../internals/to-length');
|
---|
3 | var toAbsoluteIndex = require('../internals/to-absolute-index');
|
---|
4 |
|
---|
5 | // `Array.prototype.{ indexOf, includes }` methods implementation
|
---|
6 | var createMethod = function (IS_INCLUDES) {
|
---|
7 | return function ($this, el, fromIndex) {
|
---|
8 | var O = toIndexedObject($this);
|
---|
9 | var length = toLength(O.length);
|
---|
10 | var index = toAbsoluteIndex(fromIndex, length);
|
---|
11 | var value;
|
---|
12 | // Array#includes uses SameValueZero equality algorithm
|
---|
13 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
14 | if (IS_INCLUDES && el != el) while (length > index) {
|
---|
15 | value = O[index++];
|
---|
16 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
17 | if (value != value) return true;
|
---|
18 | // Array#indexOf ignores holes, Array#includes - not
|
---|
19 | } else for (;length > index; index++) {
|
---|
20 | if ((IS_INCLUDES || index in O) && O[index] === el) return IS_INCLUDES || index || 0;
|
---|
21 | } return !IS_INCLUDES && -1;
|
---|
22 | };
|
---|
23 | };
|
---|
24 |
|
---|
25 | module.exports = {
|
---|
26 | // `Array.prototype.includes` method
|
---|
27 | // https://tc39.es/ecma262/#sec-array.prototype.includes
|
---|
28 | includes: createMethod(true),
|
---|
29 | // `Array.prototype.indexOf` method
|
---|
30 | // https://tc39.es/ecma262/#sec-array.prototype.indexof
|
---|
31 | indexOf: createMethod(false)
|
---|
32 | };
|
---|