iterators-core.js 1.8 KB

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