[79a0317] | 1 | 'use strict';
|
---|
| 2 | var bind = require('../internals/function-bind-context');
|
---|
| 3 | var call = require('../internals/function-call');
|
---|
| 4 | var anObject = require('../internals/an-object');
|
---|
| 5 | var tryToString = require('../internals/try-to-string');
|
---|
| 6 | var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
|
---|
| 7 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
| 8 | var isPrototypeOf = require('../internals/object-is-prototype-of');
|
---|
| 9 | var getIterator = require('../internals/get-iterator');
|
---|
| 10 | var getIteratorMethod = require('../internals/get-iterator-method');
|
---|
| 11 | var iteratorClose = require('../internals/iterator-close');
|
---|
| 12 |
|
---|
| 13 | var $TypeError = TypeError;
|
---|
| 14 |
|
---|
| 15 | var Result = function (stopped, result) {
|
---|
| 16 | this.stopped = stopped;
|
---|
| 17 | this.result = result;
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | var ResultPrototype = Result.prototype;
|
---|
| 21 |
|
---|
| 22 | module.exports = function (iterable, unboundFunction, options) {
|
---|
| 23 | var that = options && options.that;
|
---|
| 24 | var AS_ENTRIES = !!(options && options.AS_ENTRIES);
|
---|
| 25 | var IS_RECORD = !!(options && options.IS_RECORD);
|
---|
| 26 | var IS_ITERATOR = !!(options && options.IS_ITERATOR);
|
---|
| 27 | var INTERRUPTED = !!(options && options.INTERRUPTED);
|
---|
| 28 | var fn = bind(unboundFunction, that);
|
---|
| 29 | var iterator, iterFn, index, length, result, next, step;
|
---|
| 30 |
|
---|
| 31 | var stop = function (condition) {
|
---|
| 32 | if (iterator) iteratorClose(iterator, 'normal', condition);
|
---|
| 33 | return new Result(true, condition);
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | var callFn = function (value) {
|
---|
| 37 | if (AS_ENTRIES) {
|
---|
| 38 | anObject(value);
|
---|
| 39 | return INTERRUPTED ? fn(value[0], value[1], stop) : fn(value[0], value[1]);
|
---|
| 40 | } return INTERRUPTED ? fn(value, stop) : fn(value);
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | if (IS_RECORD) {
|
---|
| 44 | iterator = iterable.iterator;
|
---|
| 45 | } else if (IS_ITERATOR) {
|
---|
| 46 | iterator = iterable;
|
---|
| 47 | } else {
|
---|
| 48 | iterFn = getIteratorMethod(iterable);
|
---|
| 49 | if (!iterFn) throw new $TypeError(tryToString(iterable) + ' is not iterable');
|
---|
| 50 | // optimisation for array iterators
|
---|
| 51 | if (isArrayIteratorMethod(iterFn)) {
|
---|
| 52 | for (index = 0, length = lengthOfArrayLike(iterable); length > index; index++) {
|
---|
| 53 | result = callFn(iterable[index]);
|
---|
| 54 | if (result && isPrototypeOf(ResultPrototype, result)) return result;
|
---|
| 55 | } return new Result(false);
|
---|
| 56 | }
|
---|
| 57 | iterator = getIterator(iterable, iterFn);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | next = IS_RECORD ? iterable.next : iterator.next;
|
---|
| 61 | while (!(step = call(next, iterator)).done) {
|
---|
| 62 | try {
|
---|
| 63 | result = callFn(step.value);
|
---|
| 64 | } catch (error) {
|
---|
| 65 | iteratorClose(iterator, 'throw', error);
|
---|
| 66 | }
|
---|
| 67 | if (typeof result == 'object' && result && isPrototypeOf(ResultPrototype, result)) return result;
|
---|
| 68 | } return new Result(false);
|
---|
| 69 | };
|
---|