iterators-core.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var fails = require('../internals/fails');
  3. var isCallable = require('../internals/is-callable');
  4. var create = require('../internals/object-create');
  5. var getPrototypeOf = require('../internals/object-get-prototype-of');
  6. var redefine = require('../internals/redefine');
  7. var wellKnownSymbol = require('../internals/well-known-symbol');
  8. var IS_PURE = require('../internals/is-pure');
  9. var ITERATOR = wellKnownSymbol('iterator');
  10. var BUGGY_SAFARI_ITERATORS = false;
  11. // `%IteratorPrototype%` object
  12. // https://tc39.es/ecma262/#sec-%iteratorprototype%-object
  13. var IteratorPrototype, PrototypeOfArrayIteratorPrototype, arrayIterator;
  14. /* eslint-disable es-x/no-array-prototype-keys -- safe */
  15. if ([].keys) {
  16. arrayIterator = [].keys();
  17. // Safari 8 has buggy iterators w/o `next`
  18. if (!('next' in arrayIterator)) BUGGY_SAFARI_ITERATORS = true;
  19. else {
  20. PrototypeOfArrayIteratorPrototype = getPrototypeOf(getPrototypeOf(arrayIterator));
  21. if (PrototypeOfArrayIteratorPrototype !== Object.prototype) IteratorPrototype = PrototypeOfArrayIteratorPrototype;
  22. }
  23. }
  24. var NEW_ITERATOR_PROTOTYPE = IteratorPrototype == undefined || fails(function () {
  25. var test = {};
  26. // FF44- legacy iterators case
  27. return IteratorPrototype[ITERATOR].call(test) !== test;
  28. });
  29. if (NEW_ITERATOR_PROTOTYPE) IteratorPrototype = {};
  30. else if (IS_PURE) IteratorPrototype = create(IteratorPrototype);
  31. // `%IteratorPrototype%[@@iterator]()` method
  32. // https://tc39.es/ecma262/#sec-%iteratorprototype%-@@iterator
  33. if (!isCallable(IteratorPrototype[ITERATOR])) {
  34. redefine(IteratorPrototype, ITERATOR, function () {
  35. return this;
  36. });
  37. }
  38. module.exports = {
  39. IteratorPrototype: IteratorPrototype,
  40. BUGGY_SAFARI_ITERATORS: BUGGY_SAFARI_ITERATORS
  41. };