UNPKG

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