@gjsify/lightningcss-wasm 0.4.28 → 0.4.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/async.mjs +57 -51
- package/src/browserslistToTargets.js +35 -35
- package/src/composeVisitors.js +346 -346
- package/src/flags.js +24 -24
- package/src/index.mjs +47 -47
- package/src/napi-wasm.mjs +1338 -1323
package/src/composeVisitors.js
CHANGED
|
@@ -4,142 +4,142 @@
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Composes multiple visitor objects into a single one.
|
|
7
|
-
* @param {(Visitor | VisitorFunction)[]} visitors
|
|
7
|
+
* @param {(Visitor | VisitorFunction)[]} visitors
|
|
8
8
|
* @return {Visitor | VisitorFunction}
|
|
9
9
|
*/
|
|
10
10
|
function composeVisitors(visitors) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
11
|
+
if (visitors.length === 1) {
|
|
12
|
+
return visitors[0];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (visitors.some((v) => typeof v === 'function')) {
|
|
16
|
+
return (opts) => {
|
|
17
|
+
let v = visitors.map((v) => (typeof v === 'function' ? v(opts) : v));
|
|
18
|
+
return composeVisitors(v);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @type Visitor */
|
|
23
|
+
let res = {};
|
|
24
|
+
composeSimpleVisitors(res, visitors, 'StyleSheet');
|
|
25
|
+
composeSimpleVisitors(res, visitors, 'StyleSheetExit');
|
|
26
|
+
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapCustomAndUnknownAtRule);
|
|
27
|
+
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapCustomAndUnknownAtRule);
|
|
28
|
+
composeObjectVisitors(res, visitors, 'Declaration', declarationVisitor, wrapCustomProperty);
|
|
29
|
+
composeObjectVisitors(res, visitors, 'DeclarationExit', declarationVisitor, wrapCustomProperty);
|
|
30
|
+
composeSimpleVisitors(res, visitors, 'Url');
|
|
31
|
+
composeSimpleVisitors(res, visitors, 'Color');
|
|
32
|
+
composeSimpleVisitors(res, visitors, 'Image');
|
|
33
|
+
composeSimpleVisitors(res, visitors, 'ImageExit');
|
|
34
|
+
composeSimpleVisitors(res, visitors, 'Length');
|
|
35
|
+
composeSimpleVisitors(res, visitors, 'Angle');
|
|
36
|
+
composeSimpleVisitors(res, visitors, 'Ratio');
|
|
37
|
+
composeSimpleVisitors(res, visitors, 'Resolution');
|
|
38
|
+
composeSimpleVisitors(res, visitors, 'Time');
|
|
39
|
+
composeSimpleVisitors(res, visitors, 'CustomIdent');
|
|
40
|
+
composeSimpleVisitors(res, visitors, 'DashedIdent');
|
|
41
|
+
composeArrayFunctions(res, visitors, 'MediaQuery');
|
|
42
|
+
composeArrayFunctions(res, visitors, 'MediaQueryExit');
|
|
43
|
+
composeSimpleVisitors(res, visitors, 'SupportsCondition');
|
|
44
|
+
composeSimpleVisitors(res, visitors, 'SupportsConditionExit');
|
|
45
|
+
composeArrayFunctions(res, visitors, 'Selector');
|
|
46
|
+
composeTokenVisitors(res, visitors, 'Token', 'token', false);
|
|
47
|
+
composeTokenVisitors(res, visitors, 'Function', 'function', false);
|
|
48
|
+
composeTokenVisitors(res, visitors, 'FunctionExit', 'function', true);
|
|
49
|
+
composeTokenVisitors(res, visitors, 'Variable', 'var', false);
|
|
50
|
+
composeTokenVisitors(res, visitors, 'VariableExit', 'var', true);
|
|
51
|
+
composeTokenVisitors(res, visitors, 'EnvironmentVariable', 'env', false);
|
|
52
|
+
composeTokenVisitors(res, visitors, 'EnvironmentVariableExit', 'env', true);
|
|
53
|
+
return res;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
export { composeVisitors };
|
|
57
57
|
|
|
58
58
|
function wrapCustomAndUnknownAtRule(k, f) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
if (k === 'unknown') {
|
|
60
|
+
return (value) => f({ type: 'unknown', value });
|
|
61
|
+
}
|
|
62
|
+
if (k === 'custom') {
|
|
63
|
+
return (value) => f({ type: 'custom', value });
|
|
64
|
+
}
|
|
65
|
+
return f;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
function wrapCustomProperty(k, f) {
|
|
69
|
-
|
|
69
|
+
return k === 'custom' ? (value) => f({ property: 'custom', value }) : f;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* @param {import('./index').Visitor['Rule']} f
|
|
74
|
-
* @param {import('./ast').Rule} item
|
|
73
|
+
* @param {import('./index').Visitor['Rule']} f
|
|
74
|
+
* @param {import('./ast').Rule} item
|
|
75
75
|
*/
|
|
76
76
|
function ruleVisitor(f, item) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
if (typeof f === 'object') {
|
|
78
|
+
if (item.type === 'unknown') {
|
|
79
|
+
let v = f.unknown;
|
|
80
|
+
if (typeof v === 'object') {
|
|
81
|
+
v = v[item.value.name];
|
|
82
|
+
}
|
|
83
|
+
return v?.(item.value);
|
|
84
|
+
}
|
|
85
|
+
if (item.type === 'custom') {
|
|
86
|
+
let v = f.custom;
|
|
87
|
+
if (typeof v === 'object') {
|
|
88
|
+
v = v[item.value.name];
|
|
89
|
+
}
|
|
90
|
+
return v?.(item.value);
|
|
91
|
+
}
|
|
92
|
+
return f[item.type]?.(item);
|
|
91
93
|
}
|
|
92
|
-
return f
|
|
93
|
-
}
|
|
94
|
-
return f?.(item);
|
|
94
|
+
return f?.(item);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* @param {import('./index').Visitor['Declaration']} f
|
|
99
|
-
* @param {import('./ast').Declaration} item
|
|
98
|
+
* @param {import('./index').Visitor['Declaration']} f
|
|
99
|
+
* @param {import('./ast').Declaration} item
|
|
100
100
|
*/
|
|
101
101
|
function declarationVisitor(f, item) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
102
|
+
if (typeof f === 'object') {
|
|
103
|
+
/** @type {string} */
|
|
104
|
+
let name = item.property;
|
|
105
|
+
if (item.property === 'unparsed') {
|
|
106
|
+
name = item.value.propertyId.property;
|
|
107
|
+
} else if (item.property === 'custom') {
|
|
108
|
+
let v = f.custom;
|
|
109
|
+
if (typeof v === 'object') {
|
|
110
|
+
v = v[item.value.name];
|
|
111
|
+
}
|
|
112
|
+
return v?.(item.value);
|
|
113
|
+
}
|
|
114
|
+
return f[name]?.(item);
|
|
113
115
|
}
|
|
114
|
-
return f
|
|
115
|
-
}
|
|
116
|
-
return f?.(item);
|
|
116
|
+
return f?.(item);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
*
|
|
121
|
-
* @param {Visitor[]} visitors
|
|
122
|
-
* @param {string} key
|
|
120
|
+
*
|
|
121
|
+
* @param {Visitor[]} visitors
|
|
122
|
+
* @param {string} key
|
|
123
123
|
* @returns {[any[], boolean, Set<string>]}
|
|
124
124
|
*/
|
|
125
125
|
function extractObjectsOrFunctions(visitors, key) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
126
|
+
let values = [];
|
|
127
|
+
let hasFunction = false;
|
|
128
|
+
let allKeys = new Set();
|
|
129
|
+
for (let visitor of visitors) {
|
|
130
|
+
let v = visitor[key];
|
|
131
|
+
if (v) {
|
|
132
|
+
if (typeof v === 'function') {
|
|
133
|
+
hasFunction = true;
|
|
134
|
+
} else {
|
|
135
|
+
for (let key in v) {
|
|
136
|
+
allKeys.add(key);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
values.push(v);
|
|
137
140
|
}
|
|
138
|
-
}
|
|
139
|
-
values.push(v);
|
|
140
141
|
}
|
|
141
|
-
|
|
142
|
-
return [values, hasFunction, allKeys];
|
|
142
|
+
return [values, hasFunction, allKeys];
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
/**
|
|
@@ -147,304 +147,304 @@ function extractObjectsOrFunctions(visitors, key) {
|
|
|
147
147
|
* @param {Visitor} res
|
|
148
148
|
* @param {Visitor[]} visitors
|
|
149
149
|
* @param {K} key
|
|
150
|
-
* @param {(visitor: Visitor[K], item: any) => any | any[] | void} apply
|
|
151
|
-
* @param {(k: string, f: any) => any} wrapKey
|
|
150
|
+
* @param {(visitor: Visitor[K], item: any) => any | any[] | void} apply
|
|
151
|
+
* @param {(k: string, f: any) => any} wrapKey
|
|
152
152
|
*/
|
|
153
153
|
function composeObjectVisitors(res, visitors, key, apply, wrapKey) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
154
|
+
let [values, hasFunction, allKeys] = extractObjectsOrFunctions(visitors, key);
|
|
155
|
+
if (values.length === 0) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (values.length === 1) {
|
|
160
|
+
res[key] = values[0];
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let f = createArrayVisitor(visitors, (visitor, item) => apply(visitor[key], item));
|
|
165
|
+
if (hasFunction) {
|
|
166
|
+
res[key] = f;
|
|
167
|
+
} else {
|
|
168
|
+
/** @type {any} */
|
|
169
|
+
let v = {};
|
|
170
|
+
for (let k of allKeys) {
|
|
171
|
+
v[k] = wrapKey(k, f);
|
|
172
|
+
}
|
|
173
|
+
res[key] = v;
|
|
172
174
|
}
|
|
173
|
-
res[key] = v;
|
|
174
|
-
}
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* @param {Visitor} res
|
|
179
|
-
* @param {Visitor[]} visitors
|
|
180
|
-
* @param {string} key
|
|
181
|
-
* @param {import('./ast').TokenOrValue['type']} type
|
|
182
|
-
* @param {boolean} isExit
|
|
178
|
+
* @param {Visitor} res
|
|
179
|
+
* @param {Visitor[]} visitors
|
|
180
|
+
* @param {string} key
|
|
181
|
+
* @param {import('./ast').TokenOrValue['type']} type
|
|
182
|
+
* @param {boolean} isExit
|
|
183
183
|
*/
|
|
184
184
|
function composeTokenVisitors(res, visitors, key, type, isExit) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
185
|
+
let [values, hasFunction, allKeys] = extractObjectsOrFunctions(visitors, key);
|
|
186
|
+
if (values.length === 0) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (values.length === 1) {
|
|
191
|
+
res[key] = values[0];
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let f = createTokenVisitor(visitors, type, isExit);
|
|
196
|
+
if (hasFunction) {
|
|
197
|
+
res[key] = f;
|
|
198
|
+
} else {
|
|
199
|
+
let v = {};
|
|
200
|
+
for (let key of allKeys) {
|
|
201
|
+
v[key] = f;
|
|
202
|
+
}
|
|
203
|
+
res[key] = v;
|
|
202
204
|
}
|
|
203
|
-
res[key] = v;
|
|
204
|
-
}
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
|
-
* @param {Visitor[]} visitors
|
|
209
|
-
* @param {import('./ast').TokenOrValue['type']} type
|
|
208
|
+
* @param {Visitor[]} visitors
|
|
209
|
+
* @param {import('./ast').TokenOrValue['type']} type
|
|
210
210
|
*/
|
|
211
211
|
function createTokenVisitor(visitors, type, isExit) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
212
|
+
let v = createArrayVisitor(visitors, (visitor, /** @type {import('./ast').TokenOrValue} */ item) => {
|
|
213
|
+
let f;
|
|
214
|
+
switch (item.type) {
|
|
215
|
+
case 'token':
|
|
216
|
+
f = visitor.Token;
|
|
217
|
+
if (typeof f === 'object') {
|
|
218
|
+
f = f[item.value.type];
|
|
219
|
+
}
|
|
220
|
+
break;
|
|
221
|
+
case 'function':
|
|
222
|
+
f = isExit ? visitor.FunctionExit : visitor.Function;
|
|
223
|
+
if (typeof f === 'object') {
|
|
224
|
+
f = f[item.value.name];
|
|
225
|
+
}
|
|
226
|
+
break;
|
|
227
|
+
case 'var':
|
|
228
|
+
f = isExit ? visitor.VariableExit : visitor.Variable;
|
|
229
|
+
break;
|
|
230
|
+
case 'env':
|
|
231
|
+
f = isExit ? visitor.EnvironmentVariableExit : visitor.EnvironmentVariable;
|
|
232
|
+
if (typeof f === 'object') {
|
|
233
|
+
let name;
|
|
234
|
+
switch (item.value.name.type) {
|
|
235
|
+
case 'ua':
|
|
236
|
+
case 'unknown':
|
|
237
|
+
name = item.value.name.value;
|
|
238
|
+
break;
|
|
239
|
+
case 'custom':
|
|
240
|
+
name = item.value.name.ident;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
f = f[name];
|
|
244
|
+
}
|
|
245
|
+
break;
|
|
246
|
+
case 'color':
|
|
247
|
+
f = visitor.Color;
|
|
248
|
+
break;
|
|
249
|
+
case 'url':
|
|
250
|
+
f = visitor.Url;
|
|
251
|
+
break;
|
|
252
|
+
case 'length':
|
|
253
|
+
f = visitor.Length;
|
|
254
|
+
break;
|
|
255
|
+
case 'angle':
|
|
256
|
+
f = visitor.Angle;
|
|
257
|
+
break;
|
|
258
|
+
case 'time':
|
|
259
|
+
f = visitor.Time;
|
|
260
|
+
break;
|
|
261
|
+
case 'resolution':
|
|
262
|
+
f = visitor.Resolution;
|
|
263
|
+
break;
|
|
264
|
+
case 'dashed-ident':
|
|
265
|
+
f = visitor.DashedIdent;
|
|
266
|
+
break;
|
|
219
267
|
}
|
|
220
|
-
break;
|
|
221
|
-
case 'function':
|
|
222
|
-
f = isExit ? visitor.FunctionExit : visitor.Function;
|
|
223
|
-
if (typeof f === 'object') {
|
|
224
|
-
f = f[item.value.name];
|
|
225
|
-
}
|
|
226
|
-
break;
|
|
227
|
-
case 'var':
|
|
228
|
-
f = isExit ? visitor.VariableExit : visitor.Variable;
|
|
229
|
-
break;
|
|
230
|
-
case 'env':
|
|
231
|
-
f = isExit ? visitor.EnvironmentVariableExit : visitor.EnvironmentVariable;
|
|
232
|
-
if (typeof f === 'object') {
|
|
233
|
-
let name;
|
|
234
|
-
switch (item.value.name.type) {
|
|
235
|
-
case 'ua':
|
|
236
|
-
case 'unknown':
|
|
237
|
-
name = item.value.name.value;
|
|
238
|
-
break;
|
|
239
|
-
case 'custom':
|
|
240
|
-
name = item.value.name.ident;
|
|
241
|
-
break;
|
|
242
|
-
}
|
|
243
|
-
f = f[name];
|
|
244
|
-
}
|
|
245
|
-
break;
|
|
246
|
-
case 'color':
|
|
247
|
-
f = visitor.Color;
|
|
248
|
-
break;
|
|
249
|
-
case 'url':
|
|
250
|
-
f = visitor.Url;
|
|
251
|
-
break;
|
|
252
|
-
case 'length':
|
|
253
|
-
f = visitor.Length;
|
|
254
|
-
break;
|
|
255
|
-
case 'angle':
|
|
256
|
-
f = visitor.Angle;
|
|
257
|
-
break;
|
|
258
|
-
case 'time':
|
|
259
|
-
f = visitor.Time;
|
|
260
|
-
break;
|
|
261
|
-
case 'resolution':
|
|
262
|
-
f = visitor.Resolution;
|
|
263
|
-
break;
|
|
264
|
-
case 'dashed-ident':
|
|
265
|
-
f = visitor.DashedIdent;
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
268
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
269
|
+
if (!f) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
272
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
273
|
+
let res = f(item.value);
|
|
274
|
+
switch (item.type) {
|
|
275
|
+
case 'color':
|
|
276
|
+
case 'url':
|
|
277
|
+
case 'length':
|
|
278
|
+
case 'angle':
|
|
279
|
+
case 'time':
|
|
280
|
+
case 'resolution':
|
|
281
|
+
case 'dashed-ident':
|
|
282
|
+
if (Array.isArray(res)) {
|
|
283
|
+
res = res.map((value) => ({ type: item.type, value }));
|
|
284
|
+
} else if (res) {
|
|
285
|
+
res = { type: item.type, value: res };
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
286
288
|
}
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
289
|
|
|
290
|
-
|
|
291
|
-
|
|
290
|
+
return res;
|
|
291
|
+
});
|
|
292
292
|
|
|
293
|
-
|
|
293
|
+
return (value) => v({ type, value });
|
|
294
294
|
}
|
|
295
295
|
|
|
296
296
|
/**
|
|
297
|
-
* @param {Visitor[]} visitors
|
|
298
|
-
* @param {string} key
|
|
297
|
+
* @param {Visitor[]} visitors
|
|
298
|
+
* @param {string} key
|
|
299
299
|
*/
|
|
300
300
|
function extractFunctions(visitors, key) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
301
|
+
let functions = [];
|
|
302
|
+
for (let visitor of visitors) {
|
|
303
|
+
let f = visitor[key];
|
|
304
|
+
if (f) {
|
|
305
|
+
functions.push(f);
|
|
306
|
+
}
|
|
306
307
|
}
|
|
307
|
-
|
|
308
|
-
return functions;
|
|
308
|
+
return functions;
|
|
309
309
|
}
|
|
310
310
|
|
|
311
311
|
/**
|
|
312
|
-
* @param {Visitor} res
|
|
313
|
-
* @param {Visitor[]} visitors
|
|
314
|
-
* @param {string} key
|
|
312
|
+
* @param {Visitor} res
|
|
313
|
+
* @param {Visitor[]} visitors
|
|
314
|
+
* @param {string} key
|
|
315
315
|
*/
|
|
316
316
|
function composeSimpleVisitors(res, visitors, key) {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
res[key] = arg => {
|
|
328
|
-
let mutated = false;
|
|
329
|
-
for (let f of functions) {
|
|
330
|
-
let res = f(arg);
|
|
331
|
-
if (res) {
|
|
332
|
-
arg = res;
|
|
333
|
-
mutated = true;
|
|
334
|
-
}
|
|
317
|
+
let functions = extractFunctions(visitors, key);
|
|
318
|
+
if (functions.length === 0) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (functions.length === 1) {
|
|
323
|
+
res[key] = functions[0];
|
|
324
|
+
return;
|
|
335
325
|
}
|
|
336
326
|
|
|
337
|
-
|
|
338
|
-
|
|
327
|
+
res[key] = (arg) => {
|
|
328
|
+
let mutated = false;
|
|
329
|
+
for (let f of functions) {
|
|
330
|
+
let res = f(arg);
|
|
331
|
+
if (res) {
|
|
332
|
+
arg = res;
|
|
333
|
+
mutated = true;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return mutated ? arg : undefined;
|
|
338
|
+
};
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
/**
|
|
342
|
-
* @param {Visitor} res
|
|
343
|
-
* @param {Visitor[]} visitors
|
|
344
|
-
* @param {string} key
|
|
342
|
+
* @param {Visitor} res
|
|
343
|
+
* @param {Visitor[]} visitors
|
|
344
|
+
* @param {string} key
|
|
345
345
|
*/
|
|
346
346
|
function composeArrayFunctions(res, visitors, key) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
347
|
+
let functions = extractFunctions(visitors, key);
|
|
348
|
+
if (functions.length === 0) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
351
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
352
|
+
if (functions.length === 1) {
|
|
353
|
+
res[key] = functions[0];
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
356
|
|
|
357
|
-
|
|
357
|
+
res[key] = createArrayVisitor(functions, (f, item) => f(item));
|
|
358
358
|
}
|
|
359
359
|
|
|
360
360
|
/**
|
|
361
361
|
* @template T
|
|
362
362
|
* @template V
|
|
363
|
-
* @param {T[]} visitors
|
|
364
|
-
* @param {(visitor: T, item: V) => V | V[] | void} apply
|
|
363
|
+
* @param {T[]} visitors
|
|
364
|
+
* @param {(visitor: T, item: V) => V | V[] | void} apply
|
|
365
365
|
* @returns {(item: V) => V | V[] | void}
|
|
366
366
|
*/
|
|
367
367
|
function createArrayVisitor(visitors, apply) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
368
|
+
let seen = new Bitset(visitors.length);
|
|
369
|
+
return (arg) => {
|
|
370
|
+
let arr = [arg];
|
|
371
|
+
let mutated = false;
|
|
372
|
+
seen.clear();
|
|
373
|
+
for (let i = 0; i < arr.length; i++) {
|
|
374
|
+
// For each value, call all visitors. If a visitor returns a new value,
|
|
375
|
+
// we start over, but skip the visitor that generated the value or saw
|
|
376
|
+
// it before (to avoid cycles). This way, visitors can be composed in any order.
|
|
377
|
+
for (let v = 0; v < visitors.length && i < arr.length; ) {
|
|
378
|
+
if (seen.get(v)) {
|
|
379
|
+
v++;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
let item = arr[i];
|
|
384
|
+
let visitor = visitors[v];
|
|
385
|
+
let res = apply(visitor, item);
|
|
386
|
+
if (Array.isArray(res)) {
|
|
387
|
+
if (res.length === 0) {
|
|
388
|
+
arr.splice(i, 1);
|
|
389
|
+
} else if (res.length === 1) {
|
|
390
|
+
arr[i] = res[0];
|
|
391
|
+
} else {
|
|
392
|
+
arr.splice(i, 1, ...res);
|
|
393
|
+
}
|
|
394
|
+
mutated = true;
|
|
395
|
+
seen.set(v);
|
|
396
|
+
v = 0;
|
|
397
|
+
} else if (res) {
|
|
398
|
+
arr[i] = res;
|
|
399
|
+
mutated = true;
|
|
400
|
+
seen.set(v);
|
|
401
|
+
v = 0;
|
|
402
|
+
} else {
|
|
403
|
+
v++;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
381
406
|
}
|
|
382
407
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
let res = apply(visitor, item);
|
|
386
|
-
if (Array.isArray(res)) {
|
|
387
|
-
if (res.length === 0) {
|
|
388
|
-
arr.splice(i, 1);
|
|
389
|
-
} else if (res.length === 1) {
|
|
390
|
-
arr[i] = res[0];
|
|
391
|
-
} else {
|
|
392
|
-
arr.splice(i, 1, ...res);
|
|
393
|
-
}
|
|
394
|
-
mutated = true;
|
|
395
|
-
seen.set(v);
|
|
396
|
-
v = 0;
|
|
397
|
-
} else if (res) {
|
|
398
|
-
arr[i] = res;
|
|
399
|
-
mutated = true;
|
|
400
|
-
seen.set(v);
|
|
401
|
-
v = 0;
|
|
402
|
-
} else {
|
|
403
|
-
v++;
|
|
408
|
+
if (!mutated) {
|
|
409
|
+
return;
|
|
404
410
|
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
if (!mutated) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
411
|
|
|
412
|
-
|
|
413
|
-
|
|
412
|
+
return arr.length === 1 ? arr[0] : arr;
|
|
413
|
+
};
|
|
414
414
|
}
|
|
415
415
|
|
|
416
416
|
class Bitset {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/** @param {number} bit */
|
|
423
|
-
get(bit) {
|
|
424
|
-
if (bit >= 32 && this.more) {
|
|
425
|
-
let i = Math.floor((bit - 32) / 32);
|
|
426
|
-
let b = bit % 32;
|
|
427
|
-
return Boolean(this.more[i] & (1 << b));
|
|
428
|
-
} else {
|
|
429
|
-
return Boolean(this.bits & (1 << bit));
|
|
417
|
+
constructor(maxBits = 32) {
|
|
418
|
+
this.bits = 0;
|
|
419
|
+
this.more = maxBits > 32 ? new Uint32Array(Math.ceil((maxBits - 32) / 32)) : null;
|
|
430
420
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
421
|
+
|
|
422
|
+
/** @param {number} bit */
|
|
423
|
+
get(bit) {
|
|
424
|
+
if (bit >= 32 && this.more) {
|
|
425
|
+
let i = Math.floor((bit - 32) / 32);
|
|
426
|
+
let b = bit % 32;
|
|
427
|
+
return Boolean(this.more[i] & (1 << b));
|
|
428
|
+
} else {
|
|
429
|
+
return Boolean(this.bits & (1 << bit));
|
|
430
|
+
}
|
|
441
431
|
}
|
|
442
|
-
}
|
|
443
432
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
433
|
+
/** @param {number} bit */
|
|
434
|
+
set(bit) {
|
|
435
|
+
if (bit >= 32 && this.more) {
|
|
436
|
+
let i = Math.floor((bit - 32) / 32);
|
|
437
|
+
let b = bit % 32;
|
|
438
|
+
this.more[i] |= 1 << b;
|
|
439
|
+
} else {
|
|
440
|
+
this.bits |= 1 << bit;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
clear() {
|
|
445
|
+
this.bits = 0;
|
|
446
|
+
if (this.more) {
|
|
447
|
+
this.more.fill(0);
|
|
448
|
+
}
|
|
448
449
|
}
|
|
449
|
-
}
|
|
450
450
|
}
|