UNPKG

2.53 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var global = require('../internals/global');
4var anInstance = require('../internals/an-instance');
5var anObject = require('../internals/an-object');
6var isCallable = require('../internals/is-callable');
7var getPrototypeOf = require('../internals/object-get-prototype-of');
8var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
9var createProperty = require('../internals/create-property');
10var fails = require('../internals/fails');
11var hasOwn = require('../internals/has-own-property');
12var wellKnownSymbol = require('../internals/well-known-symbol');
13var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
14var DESCRIPTORS = require('../internals/descriptors');
15var IS_PURE = require('../internals/is-pure');
16
17var CONSTRUCTOR = 'constructor';
18var ITERATOR = 'Iterator';
19var TO_STRING_TAG = wellKnownSymbol('toStringTag');
20
21var $TypeError = TypeError;
22var NativeIterator = global[ITERATOR];
23
24// FF56- have non-standard global helper `Iterator`
25var FORCED = IS_PURE
26 || !isCallable(NativeIterator)
27 || NativeIterator.prototype !== IteratorPrototype
28 // FF44- non-standard `Iterator` passes previous tests
29 || !fails(function () { NativeIterator({}); });
30
31var IteratorConstructor = function Iterator() {
32 anInstance(this, IteratorPrototype);
33 if (getPrototypeOf(this) === IteratorPrototype) throw new $TypeError('Abstract class Iterator not directly constructable');
34};
35
36var defineIteratorPrototypeAccessor = function (key, value) {
37 if (DESCRIPTORS) {
38 defineBuiltInAccessor(IteratorPrototype, key, {
39 configurable: true,
40 get: function () {
41 return value;
42 },
43 set: function (replacement) {
44 anObject(this);
45 if (this === IteratorPrototype) throw new $TypeError("You can't redefine this property");
46 if (hasOwn(this, key)) this[key] = replacement;
47 else createProperty(this, key, replacement);
48 }
49 });
50 } else IteratorPrototype[key] = value;
51};
52
53if (!hasOwn(IteratorPrototype, TO_STRING_TAG)) defineIteratorPrototypeAccessor(TO_STRING_TAG, ITERATOR);
54
55if (FORCED || !hasOwn(IteratorPrototype, CONSTRUCTOR) || IteratorPrototype[CONSTRUCTOR] === Object) {
56 defineIteratorPrototypeAccessor(CONSTRUCTOR, IteratorConstructor);
57}
58
59IteratorConstructor.prototype = IteratorPrototype;
60
61// `Iterator` constructor
62// https://github.com/tc39/proposal-iterator-helpers
63$({ global: true, constructor: true, forced: FORCED }, {
64 Iterator: IteratorConstructor
65});