_for-of.js 1.1 KB

12345678910111213141516171819202122232425
  1. var ctx = require('./_ctx');
  2. var call = require('./_iter-call');
  3. var isArrayIter = require('./_is-array-iter');
  4. var anObject = require('./_an-object');
  5. var toLength = require('./_to-length');
  6. var getIterFn = require('./core.get-iterator-method');
  7. var BREAK = {};
  8. var RETURN = {};
  9. var exports = module.exports = function (iterable, entries, fn, that, ITERATOR) {
  10. var iterFn = ITERATOR ? function () { return iterable; } : getIterFn(iterable);
  11. var f = ctx(fn, that, entries ? 2 : 1);
  12. var index = 0;
  13. var length, step, iterator, result;
  14. if (typeof iterFn != 'function') throw TypeError(iterable + ' is not iterable!');
  15. // fast case for arrays with default iterator
  16. if (isArrayIter(iterFn)) for (length = toLength(iterable.length); length > index; index++) {
  17. result = entries ? f(anObject(step = iterable[index])[0], step[1]) : f(iterable[index]);
  18. if (result === BREAK || result === RETURN) return result;
  19. } else for (iterator = iterFn.call(iterable); !(step = iterator.next()).done;) {
  20. result = call(iterator, f, step.value, entries);
  21. if (result === BREAK || result === RETURN) return result;
  22. }
  23. };
  24. exports.BREAK = BREAK;
  25. exports.RETURN = RETURN;