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