implementation.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var ToInteger = require('es-abstract/2020/ToInteger');
  3. var ToLength = require('es-abstract/2020/ToLength');
  4. var ToObject = require('es-abstract/2020/ToObject');
  5. var SameValueZero = require('es-abstract/2020/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. module.exports = function includes(searchElement) {
  14. var fromIndex = arguments.length > 1 ? ToInteger(arguments[1]) : 0;
  15. if ($indexOf && !$isNaN(searchElement) && $isFinite(fromIndex) && typeof searchElement !== 'undefined') {
  16. return $indexOf.apply(this, arguments) > -1;
  17. }
  18. var O = ToObject(this);
  19. var length = ToLength(O.length);
  20. if (length === 0) {
  21. return false;
  22. }
  23. var k = fromIndex >= 0 ? fromIndex : Math.max(0, length + fromIndex);
  24. while (k < length) {
  25. if (SameValueZero(searchElement, isString(O) ? $charAt(O, k) : O[k])) {
  26. return true;
  27. }
  28. k += 1;
  29. }
  30. return false;
  31. };