+var global = require('../internals/global');
+var bind = require('../internals/function-bind-context');
+var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
+var tryToString = require('../internals/try-to-string');
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
-var toLength = require('../internals/to-length');
-var bind = require('../internals/function-bind-context');
+var lengthOfArrayLike = require('../internals/length-of-array-like');
+var isPrototypeOf = require('../internals/object-is-prototype-of');
+var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
-var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');
+var iteratorClose = require('../internals/iterator-close');
+
+var TypeError = global.TypeError;
var Result = function (stopped, result) {
this.stopped = stopped;
this.result = result;
};
-var iterate = module.exports = function (iterable, fn, that, AS_ENTRIES, IS_ITERATOR) {
- var boundFunction = bind(fn, that, AS_ENTRIES ? 2 : 1);
+var ResultPrototype = Result.prototype;
+
+module.exports = function (iterable, unboundFunction, options) {
+ var that = options && options.that;
+ var AS_ENTRIES = !!(options && options.AS_ENTRIES);
+ var IS_ITERATOR = !!(options && options.IS_ITERATOR);
+ var INTERRUPTED = !!(options && options.INTERRUPTED);
+ var fn = bind(unboundFunction, that);
var iterator, iterFn, index, length, result, next, step;
+ var stop = function (condition) {
+ if (iterator) iteratorClose(iterator, 'normal', condition);
+ return new Result(true, condition);
+ };
+
+ var callFn = function (value) {
+ if (AS_ENTRIES) {
+ anObject(value);
+ return INTERRUPTED ? fn(value[0], value[1], stop) : fn(value[0], value[1]);
+ } return INTERRUPTED ? fn(value, stop) : fn(value);
+ };
+
if (IS_ITERATOR) {
iterator = iterable;
} else {
iterFn = getIteratorMethod(iterable);
- if (typeof iterFn != 'function') throw TypeError('Target is not iterable');
+ if (!iterFn) throw TypeError(tryToString(iterable) + ' is not iterable');
// optimisation for array iterators
if (isArrayIteratorMethod(iterFn)) {
- for (index = 0, length = toLength(iterable.length); length > index; index++) {
- result = AS_ENTRIES
- ? boundFunction(anObject(step = iterable[index])[0], step[1])
- : boundFunction(iterable[index]);
- if (result && result instanceof Result) return result;
+ for (index = 0, length = lengthOfArrayLike(iterable); length > index; index++) {
+ result = callFn(iterable[index]);
+ if (result && isPrototypeOf(ResultPrototype, result)) return result;
} return new Result(false);
}
- iterator = iterFn.call(iterable);
+ iterator = getIterator(iterable, iterFn);
}
next = iterator.next;
- while (!(step = next.call(iterator)).done) {
- result = callWithSafeIterationClosing(iterator, boundFunction, step.value, AS_ENTRIES);
- if (typeof result == 'object' && result && result instanceof Result) return result;
+ while (!(step = call(next, iterator)).done) {
+ try {
+ result = callFn(step.value);
+ } catch (error) {
+ iteratorClose(iterator, 'throw', error);
+ }
+ if (typeof result == 'object' && result && isPrototypeOf(ResultPrototype, result)) return result;
} return new Result(false);
};
-
-iterate.stop = function (result) {
- return new Result(true, result);
-};