2 var $ = require('../internals/export');
3 var call = require('../internals/function-call');
4 var IS_PURE = require('../internals/is-pure');
5 var FunctionName = require('../internals/function-name');
6 var isCallable = require('../internals/is-callable');
7 var createIteratorConstructor = require('../internals/create-iterator-constructor');
8 var getPrototypeOf = require('../internals/object-get-prototype-of');
9 var setPrototypeOf = require('../internals/object-set-prototype-of');
10 var setToStringTag = require('../internals/set-to-string-tag');
11 var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
12 var redefine = require('../internals/redefine');
13 var wellKnownSymbol = require('../internals/well-known-symbol');
14 var Iterators = require('../internals/iterators');
15 var IteratorsCore = require('../internals/iterators-core');
17 var PROPER_FUNCTION_NAME = FunctionName.PROPER;
18 var CONFIGURABLE_FUNCTION_NAME = FunctionName.CONFIGURABLE;
19 var IteratorPrototype = IteratorsCore.IteratorPrototype;
20 var BUGGY_SAFARI_ITERATORS = IteratorsCore.BUGGY_SAFARI_ITERATORS;
21 var ITERATOR = wellKnownSymbol('iterator');
23 var VALUES = 'values';
24 var ENTRIES = 'entries';
26 var returnThis = function () { return this; };
28 module.exports = function (Iterable, NAME, IteratorConstructor, next, DEFAULT, IS_SET, FORCED) {
29 createIteratorConstructor(IteratorConstructor, NAME, next);
31 var getIterationMethod = function (KIND) {
32 if (KIND === DEFAULT && defaultIterator) return defaultIterator;
33 if (!BUGGY_SAFARI_ITERATORS && KIND in IterablePrototype) return IterablePrototype[KIND];
35 case KEYS: return function keys() { return new IteratorConstructor(this, KIND); };
36 case VALUES: return function values() { return new IteratorConstructor(this, KIND); };
37 case ENTRIES: return function entries() { return new IteratorConstructor(this, KIND); };
38 } return function () { return new IteratorConstructor(this); };
41 var TO_STRING_TAG = NAME + ' Iterator';
42 var INCORRECT_VALUES_NAME = false;
43 var IterablePrototype = Iterable.prototype;
44 var nativeIterator = IterablePrototype[ITERATOR]
45 || IterablePrototype['@@iterator']
46 || DEFAULT && IterablePrototype[DEFAULT];
47 var defaultIterator = !BUGGY_SAFARI_ITERATORS && nativeIterator || getIterationMethod(DEFAULT);
48 var anyNativeIterator = NAME == 'Array' ? IterablePrototype.entries || nativeIterator : nativeIterator;
49 var CurrentIteratorPrototype, methods, KEY;
52 if (anyNativeIterator) {
53 CurrentIteratorPrototype = getPrototypeOf(anyNativeIterator.call(new Iterable()));
54 if (CurrentIteratorPrototype !== Object.prototype && CurrentIteratorPrototype.next) {
55 if (!IS_PURE && getPrototypeOf(CurrentIteratorPrototype) !== IteratorPrototype) {
57 setPrototypeOf(CurrentIteratorPrototype, IteratorPrototype);
58 } else if (!isCallable(CurrentIteratorPrototype[ITERATOR])) {
59 redefine(CurrentIteratorPrototype, ITERATOR, returnThis);
62 // Set @@toStringTag to native iterators
63 setToStringTag(CurrentIteratorPrototype, TO_STRING_TAG, true, true);
64 if (IS_PURE) Iterators[TO_STRING_TAG] = returnThis;
68 // fix Array.prototype.{ values, @@iterator }.name in V8 / FF
69 if (PROPER_FUNCTION_NAME && DEFAULT == VALUES && nativeIterator && nativeIterator.name !== VALUES) {
70 if (!IS_PURE && CONFIGURABLE_FUNCTION_NAME) {
71 createNonEnumerableProperty(IterablePrototype, 'name', VALUES);
73 INCORRECT_VALUES_NAME = true;
74 defaultIterator = function values() { return call(nativeIterator, this); };
78 // export additional methods
81 values: getIterationMethod(VALUES),
82 keys: IS_SET ? defaultIterator : getIterationMethod(KEYS),
83 entries: getIterationMethod(ENTRIES)
85 if (FORCED) for (KEY in methods) {
86 if (BUGGY_SAFARI_ITERATORS || INCORRECT_VALUES_NAME || !(KEY in IterablePrototype)) {
87 redefine(IterablePrototype, KEY, methods[KEY]);
89 } else $({ target: NAME, proto: true, forced: BUGGY_SAFARI_ITERATORS || INCORRECT_VALUES_NAME }, methods);
93 if ((!IS_PURE || FORCED) && IterablePrototype[ITERATOR] !== defaultIterator) {
94 redefine(IterablePrototype, ITERATOR, defaultIterator, { name: DEFAULT });
96 Iterators[NAME] = defaultIterator;