_array-includes.js 924 B

1234567891011121314151617181920212223
  1. // false -> Array#indexOf
  2. // true -> Array#includes
  3. var toIObject = require('./_to-iobject');
  4. var toLength = require('./_to-length');
  5. var toAbsoluteIndex = require('./_to-absolute-index');
  6. module.exports = function (IS_INCLUDES) {
  7. return function ($this, el, fromIndex) {
  8. var O = toIObject($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
  14. if (IS_INCLUDES && el != el) while (length > index) {
  15. value = O[index++];
  16. // eslint-disable-next-line no-self-compare
  17. if (value != value) return true;
  18. // Array#indexOf ignores holes, Array#includes - not
  19. } else for (;length > index; index++) if (IS_INCLUDES || index in O) {
  20. if (O[index] === el) return IS_INCLUDES || index || 0;
  21. } return !IS_INCLUDES && -1;
  22. };
  23. };