esnext.iterator.zip.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var anObject = require('../internals/an-object');
  4. var anObjectOrUndefined = require('../internals/an-object-or-undefined');
  5. var call = require('../internals/function-call');
  6. var uncurryThis = require('../internals/function-uncurry-this');
  7. var getIteratorRecord = require('../internals/get-iterator-record');
  8. var getIteratorFlattenable = require('../internals/get-iterator-flattenable');
  9. var getModeOption = require('../internals/get-mode-option');
  10. var iteratorClose = require('../internals/iterator-close');
  11. var iteratorCloseAll = require('../internals/iterator-close-all');
  12. var iteratorZip = require('../internals/iterator-zip');
  13. var concat = uncurryThis([].concat);
  14. var push = uncurryThis([].push);
  15. var THROW = 'throw';
  16. // `Iterator.zip` method
  17. // https://github.com/tc39/proposal-joint-iteration
  18. $({ target: 'Iterator', stat: true, forced: true }, {
  19. zip: function zip(iterables /* , options */) {
  20. anObject(iterables);
  21. var options = arguments.length > 1 ? anObjectOrUndefined(arguments[1]) : undefined;
  22. var mode = getModeOption(options);
  23. var paddingOption = mode === 'longest' ? anObjectOrUndefined(options && options.padding) : undefined;
  24. var iters = [];
  25. var padding = [];
  26. var inputIter = getIteratorRecord(iterables);
  27. var iter, done, next;
  28. while (!done) {
  29. try {
  30. next = anObject(call(inputIter.next, inputIter.iterator));
  31. done = next.done;
  32. } catch (error) {
  33. return iteratorCloseAll(iters, THROW, error);
  34. }
  35. if (!done) {
  36. try {
  37. iter = getIteratorFlattenable(next.value, true);
  38. } catch (error) {
  39. return iteratorCloseAll(concat([inputIter.iterator], iters), THROW, error);
  40. }
  41. push(iters, iter);
  42. }
  43. }
  44. var iterCount = iters.length;
  45. var i, paddingDone, paddingIter;
  46. if (mode === 'longest') {
  47. if (paddingOption === undefined) {
  48. for (i = 0; i < iterCount; i++) push(padding, undefined);
  49. } else {
  50. try {
  51. paddingIter = getIteratorRecord(paddingOption);
  52. } catch (error) {
  53. return iteratorCloseAll(iters, THROW, error);
  54. }
  55. var usingIterator = true;
  56. for (i = 0; i < iterCount; i++) {
  57. if (usingIterator) {
  58. try {
  59. next = anObject(call(paddingIter.next, paddingIter.iterator));
  60. paddingDone = next.done;
  61. next = next.value;
  62. } catch (error) {
  63. return iteratorCloseAll(iters, THROW, error);
  64. }
  65. if (paddingDone) {
  66. usingIterator = false;
  67. } else {
  68. push(padding, next);
  69. }
  70. } else {
  71. push(padding, undefined);
  72. }
  73. }
  74. if (usingIterator) {
  75. try {
  76. iteratorClose(paddingIter.iterator, 'normal');
  77. } catch (error) {
  78. return iteratorCloseAll(iters, THROW, error);
  79. }
  80. }
  81. }
  82. }
  83. return iteratorZip(iters, mode, padding);
  84. }
  85. });