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