implementation.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var ToIntegerOrInfinity = require('es-abstract/2021/ToIntegerOrInfinity');
  3. var ToLength = require('es-abstract/2021/ToLength');
  4. var ToObject = require('es-abstract/2021/ToObject');
  5. var SameValueZero = require('es-abstract/2021/SameValueZero');
  6. var $isNaN = require('es-abstract/helpers/isNaN');
  7. var $isFinite = require('es-abstract/helpers/isFinite');
  8. var GetIntrinsic = require('get-intrinsic');
  9. var callBound = require('call-bind/callBound');
  10. var isString = require('is-string');
  11. var $charAt = callBound('String.prototype.charAt');
  12. var $indexOf = GetIntrinsic('%Array.prototype.indexOf%'); // TODO: use callBind.apply without breaking IE 8
  13. var $max = GetIntrinsic('%Math.max%');
  14. module.exports = function includes(searchElement) {
  15. var fromIndex = arguments.length > 1 ? ToIntegerOrInfinity(arguments[1]) : 0;
  16. if ($indexOf && !$isNaN(searchElement) && $isFinite(fromIndex) && typeof searchElement !== 'undefined') {
  17. return $indexOf.apply(this, arguments) > -1;
  18. }
  19. var O = ToObject(this);
  20. var length = ToLength(O.length);
  21. if (length === 0) {
  22. return false;
  23. }
  24. var k = fromIndex >= 0 ? fromIndex : $max(0, length + fromIndex);
  25. while (k < length) {
  26. if (SameValueZero(searchElement, isString(O) ? $charAt(O, k) : O[k])) {
  27. return true;
  28. }
  29. k += 1;
  30. }
  31. return false;
  32. };