@ls-stack/utils 3.26.0 → 3.26.1

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.
@@ -14,7 +14,7 @@
14
14
  function filterObjectOrArrayKeys(objOrArray, options): Record<string, any> | Record<string, any>[];
15
15
  ```
16
16
 
17
- Defined in: [packages/utils/src/filterObjectOrArrayKeys.ts:40](https://github.com/lucasols/utils/blob/main/packages/utils/src/filterObjectOrArrayKeys.ts#L40)
17
+ Defined in: [packages/utils/src/filterObjectOrArrayKeys.ts:42](https://github.com/lucasols/utils/blob/main/packages/utils/src/filterObjectOrArrayKeys.ts#L42)
18
18
 
19
19
  Filters the keys of an object based on the provided patterns.
20
20
 
@@ -42,8 +42,10 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
42
42
  - `'[*]**nested'` - all `nested` props of all items of the array
43
43
  - `'[0-2]'` - The first three items of the array
44
44
  - `'[4-*]'` - All items of the array from the fourth index to the end
45
- - Selecting multiple properties:
46
- - `'prop.test.(prop1|prop2|prop3)'` - The `prop1`, `prop2`, and `prop3` properties of `prop.test` object
45
+ - Pattern expansion with parentheses:
46
+ - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
47
+ - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
48
+ - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
47
49
 
48
50
  #### Parameters
49
51
 
package/docs/testUtils.md CHANGED
@@ -14,7 +14,7 @@
14
14
  function compactSnapshot(value, options): string;
15
15
  ```
16
16
 
17
- Defined in: [packages/utils/src/testUtils.ts:352](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L352)
17
+ Defined in: [packages/utils/src/testUtils.ts:354](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L354)
18
18
 
19
19
  Produces a more compact and readable snapshot of a value using yaml.
20
20
  By default booleans are shown as `✅` and `❌`, use `showBooleansAs` to disable/configure this.
@@ -43,6 +43,8 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
43
43
  - `'[*]**nested'` - all `nested` props of all items of the array
44
44
  - `'[0-2]'` - The first three items of the array
45
45
  - `'[4-*]'` - All items of the array from the fourth index to the end
46
+ - Expanding the patterns with parentheses:
47
+ - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3.prop4`
46
48
 
47
49
  #### Parameters
48
50
 
@@ -13,8 +13,10 @@ function filterObjectOrArrayKeys(objOrArray, {
13
13
  const rejectPatternsRaw = toArray(rejectKeys);
14
14
  const hasFilters = filterPatternsRaw.length > 0;
15
15
  const hasRejects = rejectPatternsRaw.length > 0;
16
- const filterPatterns = filterPatternsRaw.map(parsePattern);
17
- const rejectPatterns = rejectPatternsRaw.map(parsePattern);
16
+ const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
17
+ const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
18
+ const filterPatterns = expandedFilterPatterns.map(parsePattern);
19
+ const rejectPatterns = expandedRejectPatterns.map(parsePattern);
18
20
  function matchPath(path, pattern) {
19
21
  function rec(pi, pti) {
20
22
  if (pti >= pattern.length) return pi === path.length;
@@ -42,11 +44,6 @@ function filterObjectOrArrayKeys(objOrArray, {
42
44
  return rec(pi + 1, pti + 1);
43
45
  if (ct.type === "INDEX") return rec(pi + 1, pti);
44
46
  return false;
45
- case "MULTI_KEY":
46
- if (ct.type === "KEY" && pt.names.includes(ct.name))
47
- return rec(pi + 1, pti + 1);
48
- if (ct.type === "INDEX") return rec(pi + 1, pti);
49
- return false;
50
47
  case "INDEX":
51
48
  if (ct.type === "INDEX" && ct.index === pt.index)
52
49
  return rec(pi + 1, pti + 1);
@@ -170,6 +167,32 @@ function filterObjectOrArrayKeys(objOrArray, {
170
167
  if (built === void 0) return Array.isArray(objOrArray) ? [] : {};
171
168
  return built;
172
169
  }
170
+ function expandPatterns(pattern) {
171
+ function expandSingle(str) {
172
+ const start = str.indexOf("(");
173
+ if (start === -1) {
174
+ return [str];
175
+ }
176
+ const end = str.indexOf(")", start);
177
+ if (end === -1) {
178
+ return [str];
179
+ }
180
+ const before = str.slice(0, start);
181
+ const inside = str.slice(start + 1, end);
182
+ const after = str.slice(end + 1);
183
+ if (!inside.includes("|")) {
184
+ return expandSingle(before + inside + after);
185
+ }
186
+ const options = inside.split("|").filter((option) => option.trim().length > 0);
187
+ const results = [];
188
+ for (const option of options) {
189
+ const newStr = before + option + after;
190
+ results.push(...expandSingle(newStr));
191
+ }
192
+ return results;
193
+ }
194
+ return expandSingle(pattern);
195
+ }
173
196
  function parsePattern(pattern) {
174
197
  const tokens = [];
175
198
  let i = 0;
@@ -207,18 +230,6 @@ function parsePattern(pattern) {
207
230
  i = end === -1 ? n : end + 1;
208
231
  continue;
209
232
  }
210
- if (ch === "(") {
211
- const end = pattern.indexOf(")", i + 1);
212
- const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
213
- if (inside.includes("|") && inside.trim().length > 0) {
214
- const names = inside.split("|").filter((name) => name.length > 0);
215
- if (names.length > 0) {
216
- tokens.push({ type: "MULTI_KEY", names });
217
- }
218
- }
219
- i = end === -1 ? n : end + 1;
220
- continue;
221
- }
222
233
  if (ch === "*") {
223
234
  if (pattern[i + 1] === "*") {
224
235
  tokens.push({ type: "WILDCARD_ANY" });
@@ -48,8 +48,10 @@ function filterObjectOrArrayKeys(objOrArray, {
48
48
  const rejectPatternsRaw = toArray(rejectKeys);
49
49
  const hasFilters = filterPatternsRaw.length > 0;
50
50
  const hasRejects = rejectPatternsRaw.length > 0;
51
- const filterPatterns = filterPatternsRaw.map(parsePattern);
52
- const rejectPatterns = rejectPatternsRaw.map(parsePattern);
51
+ const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
52
+ const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
53
+ const filterPatterns = expandedFilterPatterns.map(parsePattern);
54
+ const rejectPatterns = expandedRejectPatterns.map(parsePattern);
53
55
  function matchPath(path, pattern) {
54
56
  function rec(pi, pti) {
55
57
  if (pti >= pattern.length) return pi === path.length;
@@ -77,11 +79,6 @@ function filterObjectOrArrayKeys(objOrArray, {
77
79
  return rec(pi + 1, pti + 1);
78
80
  if (ct.type === "INDEX") return rec(pi + 1, pti);
79
81
  return false;
80
- case "MULTI_KEY":
81
- if (ct.type === "KEY" && pt.names.includes(ct.name))
82
- return rec(pi + 1, pti + 1);
83
- if (ct.type === "INDEX") return rec(pi + 1, pti);
84
- return false;
85
82
  case "INDEX":
86
83
  if (ct.type === "INDEX" && ct.index === pt.index)
87
84
  return rec(pi + 1, pti + 1);
@@ -205,6 +202,32 @@ function filterObjectOrArrayKeys(objOrArray, {
205
202
  if (built === void 0) return Array.isArray(objOrArray) ? [] : {};
206
203
  return built;
207
204
  }
205
+ function expandPatterns(pattern) {
206
+ function expandSingle(str) {
207
+ const start = str.indexOf("(");
208
+ if (start === -1) {
209
+ return [str];
210
+ }
211
+ const end = str.indexOf(")", start);
212
+ if (end === -1) {
213
+ return [str];
214
+ }
215
+ const before = str.slice(0, start);
216
+ const inside = str.slice(start + 1, end);
217
+ const after = str.slice(end + 1);
218
+ if (!inside.includes("|")) {
219
+ return expandSingle(before + inside + after);
220
+ }
221
+ const options = inside.split("|").filter((option) => option.trim().length > 0);
222
+ const results = [];
223
+ for (const option of options) {
224
+ const newStr = before + option + after;
225
+ results.push(...expandSingle(newStr));
226
+ }
227
+ return results;
228
+ }
229
+ return expandSingle(pattern);
230
+ }
208
231
  function parsePattern(pattern) {
209
232
  const tokens = [];
210
233
  let i = 0;
@@ -242,18 +265,6 @@ function parsePattern(pattern) {
242
265
  i = end === -1 ? n : end + 1;
243
266
  continue;
244
267
  }
245
- if (ch === "(") {
246
- const end = pattern.indexOf(")", i + 1);
247
- const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
248
- if (inside.includes("|") && inside.trim().length > 0) {
249
- const names = inside.split("|").filter((name) => name.length > 0);
250
- if (names.length > 0) {
251
- tokens.push({ type: "MULTI_KEY", names });
252
- }
253
- }
254
- i = end === -1 ? n : end + 1;
255
- continue;
256
- }
257
268
  if (ch === "*") {
258
269
  if (pattern[i + 1] === "*") {
259
270
  tokens.push({ type: "WILDCARD_ANY" });
@@ -25,8 +25,10 @@
25
25
  * - `'[*]**nested'` - all `nested` props of all items of the array
26
26
  * - `'[0-2]'` - The first three items of the array
27
27
  * - `'[4-*]'` - All items of the array from the fourth index to the end
28
- * - Selecting multiple properties:
29
- * - `'prop.test.(prop1|prop2|prop3)'` - The `prop1`, `prop2`, and `prop3` properties of `prop.test` object
28
+ * - Pattern expansion with parentheses:
29
+ * - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
30
+ * - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
31
+ * - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
30
32
  *
31
33
  * @param objOrArray - The object or array to filter.
32
34
  * @param options - The options for the filter.
@@ -25,8 +25,10 @@
25
25
  * - `'[*]**nested'` - all `nested` props of all items of the array
26
26
  * - `'[0-2]'` - The first three items of the array
27
27
  * - `'[4-*]'` - All items of the array from the fourth index to the end
28
- * - Selecting multiple properties:
29
- * - `'prop.test.(prop1|prop2|prop3)'` - The `prop1`, `prop2`, and `prop3` properties of `prop.test` object
28
+ * - Pattern expansion with parentheses:
29
+ * - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
30
+ * - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
31
+ * - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
30
32
  *
31
33
  * @param objOrArray - The object or array to filter.
32
34
  * @param options - The options for the filter.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  filterObjectOrArrayKeys
3
- } from "./chunk-FXRZ4RQU.js";
3
+ } from "./chunk-2WZGT4NA.js";
4
4
  import "./chunk-JF2MDHOJ.js";
5
5
  export {
6
6
  filterObjectOrArrayKeys
package/lib/testUtils.cjs CHANGED
@@ -142,8 +142,10 @@ function filterObjectOrArrayKeys(objOrArray, {
142
142
  const rejectPatternsRaw = toArray(rejectKeys);
143
143
  const hasFilters = filterPatternsRaw.length > 0;
144
144
  const hasRejects = rejectPatternsRaw.length > 0;
145
- const filterPatterns = filterPatternsRaw.map(parsePattern);
146
- const rejectPatterns = rejectPatternsRaw.map(parsePattern);
145
+ const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
146
+ const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
147
+ const filterPatterns = expandedFilterPatterns.map(parsePattern);
148
+ const rejectPatterns = expandedRejectPatterns.map(parsePattern);
147
149
  function matchPath(path, pattern) {
148
150
  function rec(pi, pti) {
149
151
  if (pti >= pattern.length) return pi === path.length;
@@ -171,11 +173,6 @@ function filterObjectOrArrayKeys(objOrArray, {
171
173
  return rec(pi + 1, pti + 1);
172
174
  if (ct.type === "INDEX") return rec(pi + 1, pti);
173
175
  return false;
174
- case "MULTI_KEY":
175
- if (ct.type === "KEY" && pt.names.includes(ct.name))
176
- return rec(pi + 1, pti + 1);
177
- if (ct.type === "INDEX") return rec(pi + 1, pti);
178
- return false;
179
176
  case "INDEX":
180
177
  if (ct.type === "INDEX" && ct.index === pt.index)
181
178
  return rec(pi + 1, pti + 1);
@@ -299,6 +296,32 @@ function filterObjectOrArrayKeys(objOrArray, {
299
296
  if (built === void 0) return Array.isArray(objOrArray) ? [] : {};
300
297
  return built;
301
298
  }
299
+ function expandPatterns(pattern) {
300
+ function expandSingle(str) {
301
+ const start = str.indexOf("(");
302
+ if (start === -1) {
303
+ return [str];
304
+ }
305
+ const end = str.indexOf(")", start);
306
+ if (end === -1) {
307
+ return [str];
308
+ }
309
+ const before = str.slice(0, start);
310
+ const inside = str.slice(start + 1, end);
311
+ const after = str.slice(end + 1);
312
+ if (!inside.includes("|")) {
313
+ return expandSingle(before + inside + after);
314
+ }
315
+ const options = inside.split("|").filter((option) => option.trim().length > 0);
316
+ const results = [];
317
+ for (const option of options) {
318
+ const newStr = before + option + after;
319
+ results.push(...expandSingle(newStr));
320
+ }
321
+ return results;
322
+ }
323
+ return expandSingle(pattern);
324
+ }
302
325
  function parsePattern(pattern) {
303
326
  const tokens = [];
304
327
  let i = 0;
@@ -336,18 +359,6 @@ function parsePattern(pattern) {
336
359
  i = end === -1 ? n : end + 1;
337
360
  continue;
338
361
  }
339
- if (ch === "(") {
340
- const end = pattern.indexOf(")", i + 1);
341
- const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
342
- if (inside.includes("|") && inside.trim().length > 0) {
343
- const names = inside.split("|").filter((name) => name.length > 0);
344
- if (names.length > 0) {
345
- tokens.push({ type: "MULTI_KEY", names });
346
- }
347
- }
348
- i = end === -1 ? n : end + 1;
349
- continue;
350
- }
351
362
  if (ch === "*") {
352
363
  if (pattern[i + 1] === "*") {
353
364
  tokens.push({ type: "WILDCARD_ANY" });
@@ -69,6 +69,8 @@ declare function waitController(): {
69
69
  * - `'[*]**nested'` - all `nested` props of all items of the array
70
70
  * - `'[0-2]'` - The first three items of the array
71
71
  * - `'[4-*]'` - All items of the array from the fourth index to the end
72
+ * - Expanding the patterns with parentheses:
73
+ * - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3.prop4`
72
74
  *
73
75
  * @param value - The value to snapshot.
74
76
  * @param options - The options for the snapshot.
@@ -69,6 +69,8 @@ declare function waitController(): {
69
69
  * - `'[*]**nested'` - all `nested` props of all items of the array
70
70
  * - `'[0-2]'` - The first three items of the array
71
71
  * - `'[4-*]'` - All items of the array from the fourth index to the end
72
+ * - Expanding the patterns with parentheses:
73
+ * - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3.prop4`
72
74
  *
73
75
  * @param value - The value to snapshot.
74
76
  * @param options - The options for the snapshot.
package/lib/testUtils.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-JQFUKJU5.js";
12
12
  import {
13
13
  filterObjectOrArrayKeys
14
- } from "./chunk-FXRZ4RQU.js";
14
+ } from "./chunk-2WZGT4NA.js";
15
15
  import {
16
16
  defer
17
17
  } from "./chunk-DFXNVEH6.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.26.0",
4
+ "version": "3.26.1",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",