chain.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. var setPrototypeOf = require("es5-ext/object/set-prototype-of")
  3. , d = require("d")
  4. , Iterator = require("../")
  5. , validIterable = require("../valid-iterable")
  6. , push = Array.prototype.push
  7. , defineProperties = Object.defineProperties
  8. , IteratorChain;
  9. IteratorChain = function (iterators) {
  10. defineProperties(this, {
  11. __iterators__: d("", iterators),
  12. __current__: d("w", iterators.shift())
  13. });
  14. };
  15. if (setPrototypeOf) setPrototypeOf(IteratorChain, Iterator);
  16. IteratorChain.prototype = Object.create(Iterator.prototype, {
  17. constructor: d(IteratorChain),
  18. next: d(function () {
  19. var result;
  20. if (!this.__current__) return { done: true, value: undefined };
  21. result = this.__current__.next();
  22. while (result.done) {
  23. this.__current__ = this.__iterators__.shift();
  24. if (!this.__current__) return { done: true, value: undefined };
  25. result = this.__current__.next();
  26. }
  27. return result;
  28. })
  29. });
  30. module.exports = function () {
  31. var iterators = [this];
  32. push.apply(iterators, arguments);
  33. iterators.forEach(validIterable);
  34. return new IteratorChain(iterators);
  35. };