@ls-stack/utils 3.26.1 → 3.27.0

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:42](https://github.com/lucasols/utils/blob/main/packages/utils/src/filterObjectOrArrayKeys.ts#L42)
17
+ Defined in: [packages/utils/src/filterObjectOrArrayKeys.ts:58](https://github.com/lucasols/utils/blob/main/packages/utils/src/filterObjectOrArrayKeys.ts#L58)
18
18
 
19
19
  Filters the keys of an object based on the provided patterns.
20
20
 
@@ -46,6 +46,19 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
46
46
  - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
47
47
  - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
48
48
  - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
49
+ - Array filtering by value:
50
+ - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
51
+ - `'users[%name="John" | "Jane"]'` - Value-level OR using `|` for multiple values of same property
52
+ - `'users[%name="Alice" || %age=35]'` - Property-level OR using `||` for different properties
53
+ - `'users[%age=30 && %role="admin"]'` - Property-level AND using `&&` for different properties
54
+ - Note: Mixing `&&` and `||` in the same filter is not supported - use separate filter patterns instead
55
+ - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
56
+ - `'users[%name*="oh"]'` - Contains operator (*=) - filters users where name contains "oh"
57
+ - `'users[%name^="Jo"]'` - Starts with operator (^=) - filters users where name starts with "Jo"
58
+ - `'users[%name$="hn"]'` - Ends with operator ($=) - filters users where name ends with "hn"
59
+ - `'users[%name!="John"]'` - Not equal operator (!=) - filters users where name is not "John"
60
+ - `'users[%name!*="admin"]'` - Not contains operator (!*=) - filters users where name doesn't contain "admin"
61
+ - `'users[i%name="john"]'` - Case-insensitive matching (i% prefix) - matches "John", "JOHN", "john", etc.
49
62
 
50
63
  #### Parameters
51
64
 
@@ -77,6 +90,18 @@ Whether to reject empty objects in arrays (default: true).
77
90
 
78
91
  The keys to reject.
79
92
 
93
+ ###### sortKeys?
94
+
95
+ `false` \| `"asc"` \| `"simpleValuesFirst"` \| `"desc"` = `'simpleValuesFirst'`
96
+
97
+ Sort all keys by a specific order (optional, preserves original order when not specified).
98
+
99
+ ###### sortPatterns?
100
+
101
+ `string`[]
102
+
103
+ Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
104
+
80
105
  #### Returns
81
106
 
82
107
  `Record`\<`string`, `any`\> \| `Record`\<`string`, `any`\>[]
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:354](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L354)
17
+ Defined in: [packages/utils/src/testUtils.ts:363](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L363)
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.
@@ -45,6 +45,13 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
45
45
  - `'[4-*]'` - All items of the array from the fourth index to the end
46
46
  - Expanding the patterns with parentheses:
47
47
  - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3.prop4`
48
+ - Array filtering by value:
49
+ - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
50
+ - `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name` property equal to `John` or `Jane`
51
+ - `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by using `&&` and `||`, nesting logical operators is not supported yet
52
+ - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
53
+
54
+ Check more supported patterns in [filterObjectOrArrayKeys](filterObjectOrArrayKeys.md#filterobjectorarraykeys) docs.
48
55
 
49
56
  #### Parameters
50
57
 
@@ -0,0 +1,557 @@
1
+ import {
2
+ sortBy
3
+ } from "./chunk-SRVMMYSW.js";
4
+ import {
5
+ isPlainObject
6
+ } from "./chunk-JF2MDHOJ.js";
7
+
8
+ // src/filterObjectOrArrayKeys.ts
9
+ function filterObjectOrArrayKeys(objOrArray, {
10
+ filterKeys,
11
+ rejectKeys,
12
+ rejectEmptyObjectsInArray = true,
13
+ sortKeys = "simpleValuesFirst",
14
+ sortPatterns
15
+ }) {
16
+ function getNestedValue(obj, path) {
17
+ const parts = path.split(".");
18
+ let current = obj;
19
+ for (const part of parts) {
20
+ if (current == null || typeof current !== "object") {
21
+ return void 0;
22
+ }
23
+ current = current[part];
24
+ }
25
+ return current;
26
+ }
27
+ function evaluateCondition(item, condition) {
28
+ const value = getNestedValue(item, condition.property);
29
+ let valueStr = String(value);
30
+ if (condition.caseInsensitive) {
31
+ valueStr = valueStr.toLowerCase();
32
+ }
33
+ const processValue = (v) => condition.caseInsensitive ? v.toLowerCase() : v;
34
+ switch (condition.operator) {
35
+ case "=":
36
+ return condition.values.some((v) => valueStr === processValue(v));
37
+ case "!=":
38
+ return condition.values.every((v) => valueStr !== processValue(v));
39
+ case "*=":
40
+ return condition.values.some((v) => valueStr.includes(processValue(v)));
41
+ case "!*=":
42
+ return condition.values.every(
43
+ (v) => !valueStr.includes(processValue(v))
44
+ );
45
+ case "^=":
46
+ return condition.values.some(
47
+ (v) => valueStr.startsWith(processValue(v))
48
+ );
49
+ case "!^=":
50
+ return condition.values.every(
51
+ (v) => !valueStr.startsWith(processValue(v))
52
+ );
53
+ case "$=":
54
+ return condition.values.some((v) => valueStr.endsWith(processValue(v)));
55
+ case "!$=":
56
+ return condition.values.every(
57
+ (v) => !valueStr.endsWith(processValue(v))
58
+ );
59
+ default:
60
+ return false;
61
+ }
62
+ }
63
+ const toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
64
+ const filterPatternsRaw = toArray(filterKeys);
65
+ const rejectPatternsRaw = toArray(rejectKeys);
66
+ const hasFilters = filterPatternsRaw.length > 0;
67
+ const hasRejects = rejectPatternsRaw.length > 0;
68
+ const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
69
+ const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
70
+ const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
71
+ expandedFilterPatterns
72
+ );
73
+ const filterPatterns = filterOnlyPatterns.map(parsePattern);
74
+ const rejectPatterns = expandedRejectPatterns.map(parsePattern);
75
+ const sortPatternsRaw = toArray(sortPatterns);
76
+ const expandedSortPatterns = sortPatternsRaw.flatMap(expandPatterns);
77
+ const sortPatternsParsed = expandedSortPatterns.map(parsePattern);
78
+ let dataToProcess = objOrArray;
79
+ if (combinedPatterns.length > 0) {
80
+ const groupedByFilter = /* @__PURE__ */ new Map();
81
+ for (const { filterPart, fieldPart } of combinedPatterns) {
82
+ if (!groupedByFilter.has(filterPart)) {
83
+ groupedByFilter.set(filterPart, []);
84
+ }
85
+ groupedByFilter.get(filterPart).push(fieldPart);
86
+ }
87
+ const combinedResult = Array.isArray(objOrArray) ? [] : {};
88
+ for (const [filterPart, fieldParts] of groupedByFilter) {
89
+ const filteredResult = filterObjectOrArrayKeys(objOrArray, {
90
+ filterKeys: [filterPart],
91
+ rejectKeys,
92
+ rejectEmptyObjectsInArray
93
+ });
94
+ const fieldSelectedResult = filterObjectOrArrayKeys(filteredResult, {
95
+ filterKeys: fieldParts,
96
+ rejectEmptyObjectsInArray
97
+ });
98
+ if (Array.isArray(combinedResult) && Array.isArray(fieldSelectedResult)) {
99
+ combinedResult.push(...fieldSelectedResult);
100
+ } else if (!Array.isArray(combinedResult) && !Array.isArray(fieldSelectedResult)) {
101
+ Object.assign(combinedResult, fieldSelectedResult);
102
+ }
103
+ }
104
+ if (filterOnlyPatterns.length === 0) {
105
+ return combinedResult;
106
+ }
107
+ dataToProcess = combinedResult;
108
+ }
109
+ function matchPath(path, pattern, value) {
110
+ function rec(pi, pti) {
111
+ if (pti >= pattern.length) return pi === path.length;
112
+ const pt = pattern[pti];
113
+ if (pt.type === "WILDCARD_ANY") {
114
+ if (rec(pi, pti + 1)) return true;
115
+ if (pi < path.length) return rec(pi + 1, pti);
116
+ return false;
117
+ }
118
+ if (pt.type === "WILDCARD_ONE") {
119
+ let j = pi;
120
+ let sawKey = false;
121
+ while (j < path.length) {
122
+ if (path[j].type === "KEY") sawKey = true;
123
+ if (sawKey && rec(j, pti + 1)) return true;
124
+ j += 1;
125
+ }
126
+ return false;
127
+ }
128
+ if (pi >= path.length) return false;
129
+ const ct = path[pi];
130
+ switch (pt.type) {
131
+ case "KEY":
132
+ if (ct.type === "KEY" && ct.name === pt.name)
133
+ return rec(pi + 1, pti + 1);
134
+ if (ct.type === "INDEX") return rec(pi + 1, pti);
135
+ return false;
136
+ case "INDEX":
137
+ if (ct.type === "INDEX" && ct.index === pt.index)
138
+ return rec(pi + 1, pti + 1);
139
+ return false;
140
+ case "INDEX_ANY":
141
+ if (ct.type === "INDEX") return rec(pi + 1, pti + 1);
142
+ return false;
143
+ case "INDEX_RANGE":
144
+ if (ct.type === "INDEX") {
145
+ const okLower = ct.index >= pt.start;
146
+ const okUpper = pt.end === null ? true : ct.index <= pt.end;
147
+ if (okLower && okUpper) return rec(pi + 1, pti + 1);
148
+ }
149
+ return false;
150
+ case "INDEX_FILTER":
151
+ if (ct.type === "INDEX" && value !== void 0) {
152
+ const results = pt.conditions.map(
153
+ (cond) => evaluateCondition(value, cond)
154
+ );
155
+ const matches = pt.logic === "AND" ? results.every((r) => r) : results.some((r) => r);
156
+ if (matches) return rec(pi + 1, pti + 1);
157
+ }
158
+ return false;
159
+ }
160
+ }
161
+ return rec(0, 0);
162
+ }
163
+ const matchesAnyFilter = (path, value) => filterPatterns.some((p) => matchPath(path, p, value));
164
+ const matchesAnyReject = (path, value) => rejectPatterns.some((p) => matchPath(path, p, value));
165
+ function getSortPriority(path) {
166
+ for (let i = 0; i < sortPatternsParsed.length; i++) {
167
+ if (matchPath(path, sortPatternsParsed[i])) {
168
+ return i;
169
+ }
170
+ }
171
+ return sortPatternsParsed.length;
172
+ }
173
+ function applySortKeys(keys, obj, sortOrder) {
174
+ if (sortOrder === "asc") {
175
+ return [...keys].sort();
176
+ }
177
+ if (sortOrder === "desc") {
178
+ return [...keys].sort().reverse();
179
+ }
180
+ return sortBy(
181
+ sortBy(keys, (k) => k),
182
+ (key) => {
183
+ const value = obj[key];
184
+ if (value !== void 0 && value !== null) {
185
+ if (Array.isArray(value) && value.length === 0) return 0;
186
+ if (isPlainObject(value)) {
187
+ const objLength = Object.keys(value).length;
188
+ return 1.99 + objLength * -1e-3;
189
+ }
190
+ if (Array.isArray(value)) {
191
+ const allItemsArePrimitives = value.every(
192
+ (item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0
193
+ );
194
+ if (allItemsArePrimitives) {
195
+ return 1.9 + value.length * -1e-3;
196
+ } else {
197
+ return 1.5 + value.length * -0.01;
198
+ }
199
+ }
200
+ if (typeof value === "boolean") return 4;
201
+ if (typeof value === "number") return 3.5;
202
+ if (typeof value === "string" && value.length < 20) return 3;
203
+ return 2;
204
+ }
205
+ return 0;
206
+ },
207
+ "desc"
208
+ );
209
+ }
210
+ function sortKeysWithPatterns(keys, obj, currentPath) {
211
+ if (!sortKeys && sortPatternsParsed.length === 0) {
212
+ return keys;
213
+ }
214
+ if (sortPatternsParsed.length === 0) {
215
+ return sortKeys ? applySortKeys(keys, obj, sortKeys) : keys;
216
+ }
217
+ const sortedKeys = [...keys].sort((a, b) => {
218
+ const pathA = currentPath.concat({ type: "KEY", name: a });
219
+ const pathB = currentPath.concat({ type: "KEY", name: b });
220
+ const priorityA = getSortPriority(pathA);
221
+ const priorityB = getSortPriority(pathB);
222
+ if (priorityA !== priorityB) {
223
+ return priorityA - priorityB;
224
+ }
225
+ if (sortKeys === "desc") {
226
+ return b.localeCompare(a);
227
+ }
228
+ if (sortKeys === "asc") {
229
+ return a.localeCompare(b);
230
+ }
231
+ return 0;
232
+ });
233
+ return sortedKeys;
234
+ }
235
+ const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
236
+ if (Array.isArray(value)) {
237
+ if (stack2.has(value)) {
238
+ throw new TypeError("Circular references are not supported");
239
+ }
240
+ stack2.add(value);
241
+ const out = [];
242
+ const includeAllChildren = allowedByFilter || !hasFilters;
243
+ for (let index = 0; index < value.length; index += 1) {
244
+ const childPath = path.concat({ type: "INDEX", index });
245
+ const child = value[index];
246
+ if (hasRejects && matchesAnyReject(childPath, child)) continue;
247
+ const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
248
+ const childAllowed = includeAllChildren || directInclude;
249
+ if (isPlainObject(child) || Array.isArray(child)) {
250
+ const builtChild = build(
251
+ child,
252
+ childPath,
253
+ childAllowed,
254
+ stack2,
255
+ false,
256
+ true
257
+ );
258
+ if (builtChild !== void 0) {
259
+ out.push(builtChild);
260
+ }
261
+ } else {
262
+ if (childAllowed) {
263
+ out.push(child);
264
+ }
265
+ }
266
+ }
267
+ stack2.delete(value);
268
+ const filteredOut = rejectEmptyObjectsInArray ? out.filter(
269
+ (item) => !(isPlainObject(item) && Object.keys(item).length === 0)
270
+ ) : out;
271
+ if (filteredOut.length === 0 && !allowedByFilter && !isRoot)
272
+ return void 0;
273
+ return filteredOut;
274
+ }
275
+ if (isPlainObject(value)) {
276
+ if (stack2.has(value)) {
277
+ throw new TypeError("Circular references are not supported");
278
+ }
279
+ stack2.add(value);
280
+ const result = {};
281
+ const includeAllChildren = allowedByFilter || !hasFilters;
282
+ const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
283
+ for (const key of sortedKeys) {
284
+ const childPath = path.concat({ type: "KEY", name: key });
285
+ if (hasRejects && matchesAnyReject(childPath)) continue;
286
+ const val = value[key];
287
+ const directInclude = hasFilters ? matchesAnyFilter(childPath) : true;
288
+ const childAllowed = includeAllChildren || directInclude;
289
+ if (isPlainObject(val) || Array.isArray(val)) {
290
+ const builtChild = build(
291
+ val,
292
+ childPath,
293
+ childAllowed,
294
+ stack2,
295
+ false,
296
+ false
297
+ );
298
+ if (builtChild === void 0) {
299
+ continue;
300
+ }
301
+ if (Array.isArray(builtChild) && builtChild.length === 0 && !childAllowed) {
302
+ continue;
303
+ }
304
+ if (isPlainObject(builtChild) && Object.keys(builtChild).length === 0 && !childAllowed) {
305
+ continue;
306
+ }
307
+ result[key] = builtChild;
308
+ } else {
309
+ if (childAllowed) {
310
+ result[key] = val;
311
+ }
312
+ }
313
+ }
314
+ stack2.delete(value);
315
+ if (Object.keys(result).length === 0 && !allowedByFilter && !isRoot) {
316
+ if (parentIsArray && !rejectEmptyObjectsInArray) {
317
+ return {};
318
+ }
319
+ return void 0;
320
+ }
321
+ return result;
322
+ }
323
+ return allowedByFilter || !hasFilters ? value : void 0;
324
+ };
325
+ const startPath = [];
326
+ const initialAllowed = !hasFilters;
327
+ const stack = /* @__PURE__ */ new WeakSet();
328
+ const built = build(
329
+ dataToProcess,
330
+ startPath,
331
+ initialAllowed,
332
+ stack,
333
+ true,
334
+ false
335
+ );
336
+ if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
337
+ return built;
338
+ }
339
+ function parseFilterConditions(filterContent) {
340
+ const conditions = [];
341
+ let logic = "AND";
342
+ const caseInsensitive = filterContent.startsWith("i");
343
+ const content = caseInsensitive ? filterContent.slice(1) : filterContent;
344
+ const hasAnd = content.includes("&&");
345
+ const hasOr = content.includes(" || ");
346
+ if (hasAnd && hasOr) {
347
+ throw new Error(
348
+ "Mixing && and || operators in the same filter is not supported. Use separate filter patterns instead."
349
+ );
350
+ }
351
+ const andGroups = content.split("&&").map((s) => s.trim());
352
+ for (const andGroup of andGroups) {
353
+ if (andGroup.includes(" || ")) {
354
+ logic = "OR";
355
+ const orConditions = andGroup.split(" || ").map((s) => s.trim());
356
+ for (const orCondition of orConditions) {
357
+ const parsed = parseSingleCondition(orCondition, caseInsensitive);
358
+ if (parsed) {
359
+ conditions.push(parsed);
360
+ }
361
+ }
362
+ } else {
363
+ const parsed = parseSingleCondition(andGroup, caseInsensitive);
364
+ if (parsed) {
365
+ conditions.push(parsed);
366
+ }
367
+ }
368
+ }
369
+ if (conditions.length === 0) {
370
+ return null;
371
+ }
372
+ return {
373
+ type: "INDEX_FILTER",
374
+ conditions,
375
+ logic
376
+ };
377
+ }
378
+ function parseSingleCondition(condition, caseInsensitive = false) {
379
+ const cleanCondition = condition.startsWith("%") ? condition.slice(1) : condition;
380
+ let operator = null;
381
+ let operatorIndex = -1;
382
+ let operatorLength = 0;
383
+ const operators = [
384
+ ["!*=", "!*="],
385
+ ["!^=", "!^="],
386
+ ["!$=", "!$="],
387
+ ["!=", "!="],
388
+ ["*=", "*="],
389
+ ["^=", "^="],
390
+ ["$=", "$="],
391
+ ["=", "="]
392
+ ];
393
+ for (const [op, opType] of operators) {
394
+ const index = cleanCondition.indexOf(op);
395
+ if (index !== -1) {
396
+ operator = opType;
397
+ operatorIndex = index;
398
+ operatorLength = op.length;
399
+ break;
400
+ }
401
+ }
402
+ if (operator === null || operatorIndex === -1) {
403
+ return null;
404
+ }
405
+ const property = cleanCondition.slice(0, operatorIndex).trim();
406
+ const valueStr = cleanCondition.slice(operatorIndex + operatorLength).trim();
407
+ const values = [];
408
+ if (valueStr.includes(" | ")) {
409
+ const parts = valueStr.split(" | ");
410
+ for (const part of parts) {
411
+ const trimmed = part.trim();
412
+ const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
413
+ values.push(value);
414
+ }
415
+ } else {
416
+ const trimmed = valueStr.trim();
417
+ const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
418
+ values.push(value);
419
+ }
420
+ return {
421
+ property,
422
+ operator,
423
+ values,
424
+ caseInsensitive
425
+ };
426
+ }
427
+ function separateFilterPatterns(patterns) {
428
+ const filterOnlyPatterns = [];
429
+ const combinedPatterns = [];
430
+ for (const pattern of patterns) {
431
+ const filterMatch = pattern.match(/^(.+\[[i%][^[\]]*\])\.(.+)$/);
432
+ if (filterMatch?.[1] && filterMatch[2]) {
433
+ const filterPart = filterMatch[1];
434
+ const fieldPart = filterMatch[2];
435
+ const baseArrayPath = filterPart.replace(/\[[i%][^[\]]*\]/, "[*]");
436
+ combinedPatterns.push({
437
+ filterPart,
438
+ fieldPart: `${baseArrayPath}.${fieldPart}`
439
+ });
440
+ } else {
441
+ filterOnlyPatterns.push(pattern);
442
+ }
443
+ }
444
+ return { filterOnlyPatterns, combinedPatterns };
445
+ }
446
+ function expandPatterns(pattern) {
447
+ function expandSingle(str) {
448
+ const start = str.indexOf("(");
449
+ if (start === -1) {
450
+ return [str];
451
+ }
452
+ const end = str.indexOf(")", start);
453
+ if (end === -1) {
454
+ return [str];
455
+ }
456
+ const before = str.slice(0, start);
457
+ const inside = str.slice(start + 1, end);
458
+ const after = str.slice(end + 1);
459
+ if (!inside.includes("|")) {
460
+ return expandSingle(before + inside + after);
461
+ }
462
+ const options = inside.split("|").filter((option) => option.trim().length > 0);
463
+ const results = [];
464
+ for (const option of options) {
465
+ const newStr = before + option + after;
466
+ results.push(...expandSingle(newStr));
467
+ }
468
+ return results;
469
+ }
470
+ return expandSingle(pattern);
471
+ }
472
+ function parsePattern(pattern) {
473
+ const tokens = [];
474
+ let i = 0;
475
+ const n = pattern.length;
476
+ const pushKey = (name) => {
477
+ if (name.length === 0) return;
478
+ tokens.push({ type: "KEY", name });
479
+ };
480
+ while (i < n) {
481
+ const ch = pattern[i];
482
+ if (ch === ".") {
483
+ i += 1;
484
+ continue;
485
+ }
486
+ if (ch === "[") {
487
+ const end = pattern.indexOf("]", i + 1);
488
+ const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
489
+ if (inside.startsWith("%") || inside.startsWith("i%")) {
490
+ let filterContent;
491
+ if (inside.startsWith("i%")) {
492
+ filterContent = `i${inside.slice(2)}`;
493
+ } else if (inside.startsWith("%")) {
494
+ filterContent = inside.slice(1);
495
+ } else {
496
+ filterContent = inside;
497
+ }
498
+ const filterToken = parseFilterConditions(filterContent);
499
+ if (filterToken) {
500
+ tokens.push(filterToken);
501
+ }
502
+ } else if (inside === "*") {
503
+ tokens.push({ type: "INDEX_ANY" });
504
+ } else if (inside.includes("-")) {
505
+ const parts = inside.split("-");
506
+ const startStr = parts[0] ?? "";
507
+ const endStr = parts[1] ?? "";
508
+ const start = parseInt(startStr, 10);
509
+ const endNum = endStr === "*" ? null : parseInt(endStr, 10);
510
+ tokens.push({
511
+ type: "INDEX_RANGE",
512
+ start,
513
+ end: endNum === null || Number.isFinite(endNum) ? endNum : null
514
+ });
515
+ } else if (inside.length > 0) {
516
+ const idx = parseInt(inside, 10);
517
+ tokens.push({ type: "INDEX", index: idx });
518
+ }
519
+ i = end === -1 ? n : end + 1;
520
+ continue;
521
+ }
522
+ if (ch === "*") {
523
+ if (pattern[i + 1] === "*") {
524
+ tokens.push({ type: "WILDCARD_ANY" });
525
+ i += 2;
526
+ let j2 = i;
527
+ while (j2 < n) {
528
+ const c = pattern[j2];
529
+ if (c === "." || c === "[") break;
530
+ j2 += 1;
531
+ }
532
+ if (j2 > i) {
533
+ pushKey(pattern.slice(i, j2));
534
+ i = j2;
535
+ }
536
+ continue;
537
+ } else {
538
+ tokens.push({ type: "WILDCARD_ONE" });
539
+ i += 1;
540
+ continue;
541
+ }
542
+ }
543
+ let j = i;
544
+ while (j < n) {
545
+ const c = pattern[j];
546
+ if (c === "." || c === "[") break;
547
+ j += 1;
548
+ }
549
+ pushKey(pattern.slice(i, j));
550
+ i = j;
551
+ }
552
+ return tokens;
553
+ }
554
+
555
+ export {
556
+ filterObjectOrArrayKeys
557
+ };