iterate.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. var $TypeError = TypeError;
  13. var Result = function (stopped, result) {
  14. this.stopped = stopped;
  15. this.result = result;
  16. };
  17. var ResultPrototype = Result.prototype;
  18. module.exports = function (iterable, unboundFunction, options) {
  19. var that = options && options.that;
  20. var AS_ENTRIES = !!(options && options.AS_ENTRIES);
  21. var IS_RECORD = !!(options && options.IS_RECORD);
  22. var IS_ITERATOR = !!(options && options.IS_ITERATOR);
  23. var INTERRUPTED = !!(options && options.INTERRUPTED);
  24. var fn = bind(unboundFunction, that);
  25. var iterator, iterFn, index, length, result, next, step;
  26. var stop = function (condition) {
  27. var $iterator = iterator;
  28. iterator = undefined;
  29. if ($iterator) iteratorClose($iterator, 'normal');
  30. return new Result(true, condition);
  31. };
  32. var callFn = function (value) {
  33. if (AS_ENTRIES) {
  34. anObject(value);
  35. return INTERRUPTED ? fn(value[0], value[1], stop) : fn(value[0], value[1]);
  36. } return INTERRUPTED ? fn(value, stop) : fn(value);
  37. };
  38. if (IS_RECORD) {
  39. iterator = iterable.iterator;
  40. } else if (IS_ITERATOR) {
  41. iterator = iterable;
  42. } else {
  43. iterFn = getIteratorMethod(iterable);
  44. if (!iterFn) throw new $TypeError(tryToString(iterable) + ' is not iterable');
  45. // optimisation for array iterators
  46. if (isArrayIteratorMethod(iterFn)) {
  47. for (index = 0, length = lengthOfArrayLike(iterable); length > index; index++) {
  48. result = callFn(iterable[index]);
  49. if (result && isPrototypeOf(ResultPrototype, result)) return result;
  50. } return new Result(false);
  51. }
  52. iterator = getIterator(iterable, iterFn);
  53. }
  54. next = IS_RECORD ? iterable.next : iterator.next;
  55. while (!(step = call(next, iterator)).done) {
  56. // `IteratorValue` errors should propagate without closing the iterator
  57. var value = step.value;
  58. try {
  59. result = callFn(value);
  60. } catch (error) {
  61. if (iterator) iteratorClose(iterator, 'throw', error);
  62. else throw error;
  63. }
  64. if (typeof result == 'object' && result && isPrototypeOf(ResultPrototype, result)) return result;
  65. } return new Result(false);
  66. };