1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var Get = require('es-abstract/2024/Get');
|
---|
6 | var GetIterator = require('es-abstract/2024/GetIterator');
|
---|
7 | var GetIteratorFlattenable = require('../aos/GetIteratorFlattenable');
|
---|
8 | var GetOptionsObject = require('../aos/GetOptionsObject');
|
---|
9 | var IfAbruptCloseIterators = require('../aos/IfAbruptCloseIterators');
|
---|
10 | var IteratorClose = require('es-abstract/2024/IteratorClose');
|
---|
11 | var IteratorStepValue = require('es-abstract/2024/IteratorStepValue');
|
---|
12 | var IteratorZip = require('../aos/IteratorZip');
|
---|
13 | var NormalCompletion = require('es-abstract/2024/NormalCompletion');
|
---|
14 | var ThrowCompletion = require('es-abstract/2024/ThrowCompletion');
|
---|
15 | var Type = require('es-abstract/2024/Type');
|
---|
16 |
|
---|
17 | module.exports = function zip(iterables) {
|
---|
18 | if (this instanceof zip) {
|
---|
19 | throw new $TypeError('`Iterator.zip` is not a constructor');
|
---|
20 | }
|
---|
21 |
|
---|
22 | if (Type(iterables) !== 'Object') {
|
---|
23 | throw new $TypeError('`iterables` must be an Object'); // step 1
|
---|
24 | }
|
---|
25 |
|
---|
26 | var options = GetOptionsObject(arguments.length > 1 ? arguments[1] : undefined); // step 2
|
---|
27 |
|
---|
28 | var mode = Get(options, 'mode'); // step 3
|
---|
29 |
|
---|
30 | if (typeof mode === 'undefined') {
|
---|
31 | mode = 'shortest'; // step 4
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (mode !== 'shortest' && mode !== 'longest' && mode !== 'strict') {
|
---|
35 | throw new $TypeError('`mode` must be one of "shortest", "longest", or "strict"'); // step 5
|
---|
36 | }
|
---|
37 |
|
---|
38 | var paddingOption; // step 6
|
---|
39 |
|
---|
40 | if (mode === 'longest') {
|
---|
41 | paddingOption = Get(options, 'padding'); // step 7
|
---|
42 | if (typeof paddingOption !== 'undefined' && Type(paddingOption) !== 'Object') {
|
---|
43 | throw new $TypeError('`padding` option must be an Object'); // step 7.1
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | var iters = []; // step 8
|
---|
48 |
|
---|
49 | var padding = []; // step 9
|
---|
50 |
|
---|
51 | var inputIter = GetIterator(iterables, 'SYNC'); // step 10
|
---|
52 |
|
---|
53 | var next; // = 'NOT-STARTED'; // step 11
|
---|
54 |
|
---|
55 | while (!inputIter['[[Done]]']) { // step 12
|
---|
56 | try {
|
---|
57 | next = IteratorStepValue(inputIter); // step 12.a
|
---|
58 | } catch (e) {
|
---|
59 | IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 12.b
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (!inputIter['[[Done]]']) { // step 12.c
|
---|
63 | var iter;
|
---|
64 | try {
|
---|
65 | iter = GetIteratorFlattenable(next, 'REJECT-STRINGS'); // step 12.c.i
|
---|
66 | } catch (e) {
|
---|
67 | IfAbruptCloseIterators(ThrowCompletion(e), [].concat(inputIter, iters)); // step 12.c.ii
|
---|
68 | }
|
---|
69 | iters[iters.length] = iter; // step 12.c.iii
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | var iterCount = iters.length; // step 13
|
---|
74 |
|
---|
75 | if (mode === 'longest') { // step 14
|
---|
76 | if (typeof paddingOption === 'undefined') { // step 14.a
|
---|
77 | for (var i = 0; i < iterCount; i++) { // step 14.a.i
|
---|
78 | padding[padding.length] = void undefined; // step 14.a.i.1
|
---|
79 | }
|
---|
80 | } else { // step 14.b
|
---|
81 | var paddingIter;
|
---|
82 | try {
|
---|
83 | paddingIter = GetIterator(paddingOption, 'SYNC'); // step 14.b.i
|
---|
84 | } catch (e) {
|
---|
85 | IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.ii
|
---|
86 | }
|
---|
87 | var usingIterator = true; // step 14.b.iii
|
---|
88 | for (var j = 0; j < iterCount; j++) { // step 14.b.iv
|
---|
89 | if (usingIterator) { // step 14.b.iv.1
|
---|
90 | try {
|
---|
91 | next = IteratorStepValue(paddingIter); // step 14.b.iv.1.a
|
---|
92 | } catch (e) {
|
---|
93 | IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.iv.1.b
|
---|
94 | }
|
---|
95 | if (paddingIter['[[Done]]']) { // step 14.b.iv.1.c
|
---|
96 | usingIterator = false; // step 14.b.iv.1.c.i
|
---|
97 | } else { // step 14.b.iv.1.d
|
---|
98 | padding[padding.length] = next; // step 14.b.iv.1.d.i
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (!usingIterator) {
|
---|
102 | padding[padding.length] = void undefined; // step 14.b.iv.2
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (usingIterator) { // step 14.b.v
|
---|
107 | try {
|
---|
108 | IteratorClose(paddingIter, NormalCompletion(undefined)); // step 14.b.v.1
|
---|
109 | } catch (e) {
|
---|
110 | IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.v.2
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | // eslint-disable-next-line no-sequences
|
---|
117 | var finishResults = (0, function (results) { // step 15
|
---|
118 | return results; // step 15.1
|
---|
119 | });
|
---|
120 |
|
---|
121 | return IteratorZip(iters, mode, padding, finishResults); // step 16
|
---|
122 | };
|
---|