91 lines
3.9 KiB
JavaScript
91 lines
3.9 KiB
JavaScript
|
'use strict';
|
||
|
var $ = require('../internals/export');
|
||
|
var createIteratorConstructor = require('../internals/create-iterator-constructor');
|
||
|
var getPrototypeOf = require('../internals/object-get-prototype-of');
|
||
|
var setPrototypeOf = require('../internals/object-set-prototype-of');
|
||
|
var setToStringTag = require('../internals/set-to-string-tag');
|
||
|
var hide = require('../internals/hide');
|
||
|
var redefine = require('../internals/redefine');
|
||
|
var wellKnownSymbol = require('../internals/well-known-symbol');
|
||
|
var IS_PURE = require('../internals/is-pure');
|
||
|
var Iterators = require('../internals/iterators');
|
||
|
var IteratorsCore = require('../internals/iterators-core');
|
||
|
|
||
|
var IteratorPrototype = IteratorsCore.IteratorPrototype;
|
||
|
var BUGGY_SAFARI_ITERATORS = IteratorsCore.BUGGY_SAFARI_ITERATORS;
|
||
|
var ITERATOR = wellKnownSymbol('iterator');
|
||
|
var KEYS = 'keys';
|
||
|
var VALUES = 'values';
|
||
|
var ENTRIES = 'entries';
|
||
|
|
||
|
var returnThis = function () { return this; };
|
||
|
|
||
|
module.exports = function (Iterable, NAME, IteratorConstructor, next, DEFAULT, IS_SET, FORCED) {
|
||
|
createIteratorConstructor(IteratorConstructor, NAME, next);
|
||
|
|
||
|
var getIterationMethod = function (KIND) {
|
||
|
if (KIND === DEFAULT && defaultIterator) return defaultIterator;
|
||
|
if (!BUGGY_SAFARI_ITERATORS && KIND in IterablePrototype) return IterablePrototype[KIND];
|
||
|
switch (KIND) {
|
||
|
case KEYS: return function keys() { return new IteratorConstructor(this, KIND); };
|
||
|
case VALUES: return function values() { return new IteratorConstructor(this, KIND); };
|
||
|
case ENTRIES: return function entries() { return new IteratorConstructor(this, KIND); };
|
||
|
} return function () { return new IteratorConstructor(this); };
|
||
|
};
|
||
|
|
||
|
var TO_STRING_TAG = NAME + ' Iterator';
|
||
|
var INCORRECT_VALUES_NAME = false;
|
||
|
var IterablePrototype = Iterable.prototype;
|
||
|
var nativeIterator = IterablePrototype[ITERATOR]
|
||
|
|| IterablePrototype['@@iterator']
|
||
|
|| DEFAULT && IterablePrototype[DEFAULT];
|
||
|
var defaultIterator = !BUGGY_SAFARI_ITERATORS && nativeIterator || getIterationMethod(DEFAULT);
|
||
|
var anyNativeIterator = NAME == 'Array' ? IterablePrototype.entries || nativeIterator : nativeIterator;
|
||
|
var CurrentIteratorPrototype, methods, KEY;
|
||
|
|
||
|
// fix native
|
||
|
if (anyNativeIterator) {
|
||
|
CurrentIteratorPrototype = getPrototypeOf(anyNativeIterator.call(new Iterable()));
|
||
|
if (IteratorPrototype !== Object.prototype && CurrentIteratorPrototype.next) {
|
||
|
if (!IS_PURE && getPrototypeOf(CurrentIteratorPrototype) !== IteratorPrototype) {
|
||
|
if (setPrototypeOf) {
|
||
|
setPrototypeOf(CurrentIteratorPrototype, IteratorPrototype);
|
||
|
} else if (typeof CurrentIteratorPrototype[ITERATOR] != 'function') {
|
||
|
hide(CurrentIteratorPrototype, ITERATOR, returnThis);
|
||
|
}
|
||
|
}
|
||
|
// Set @@toStringTag to native iterators
|
||
|
setToStringTag(CurrentIteratorPrototype, TO_STRING_TAG, true, true);
|
||
|
if (IS_PURE) Iterators[TO_STRING_TAG] = returnThis;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// fix Array#{values, @@iterator}.name in V8 / FF
|
||
|
if (DEFAULT == VALUES && nativeIterator && nativeIterator.name !== VALUES) {
|
||
|
INCORRECT_VALUES_NAME = true;
|
||
|
defaultIterator = function values() { return nativeIterator.call(this); };
|
||
|
}
|
||
|
|
||
|
// define iterator
|
||
|
if ((!IS_PURE || FORCED) && IterablePrototype[ITERATOR] !== defaultIterator) {
|
||
|
hide(IterablePrototype, ITERATOR, defaultIterator);
|
||
|
}
|
||
|
Iterators[NAME] = defaultIterator;
|
||
|
|
||
|
// export additional methods
|
||
|
if (DEFAULT) {
|
||
|
methods = {
|
||
|
values: getIterationMethod(VALUES),
|
||
|
keys: IS_SET ? defaultIterator : getIterationMethod(KEYS),
|
||
|
entries: getIterationMethod(ENTRIES)
|
||
|
};
|
||
|
if (FORCED) for (KEY in methods) {
|
||
|
if (BUGGY_SAFARI_ITERATORS || INCORRECT_VALUES_NAME || !(KEY in IterablePrototype)) {
|
||
|
redefine(IterablePrototype, KEY, methods[KEY]);
|
||
|
}
|
||
|
} else $({ target: NAME, proto: true, forced: BUGGY_SAFARI_ITERATORS || INCORRECT_VALUES_NAME }, methods);
|
||
|
}
|
||
|
|
||
|
return methods;
|
||
|
};
|