@ls-stack/utils 3.26.0 → 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.
- package/docs/filterObjectOrArrayKeys.md +30 -3
- package/docs/testUtils.md +10 -1
- package/lib/chunk-MVN6FKX7.js +557 -0
- package/lib/filterObjectOrArrayKeys.cjs +352 -29
- package/lib/filterObjectOrArrayKeys.d.cts +22 -3
- package/lib/filterObjectOrArrayKeys.d.ts +22 -3
- package/lib/filterObjectOrArrayKeys.js +3 -1
- package/lib/testUtils.cjs +355 -30
- package/lib/testUtils.d.cts +14 -40
- package/lib/testUtils.d.ts +14 -40
- package/lib/testUtils.js +6 -2
- package/package.json +1 -1
- package/lib/chunk-FXRZ4RQU.js +0 -257
package/lib/testUtils.cjs
CHANGED
|
@@ -59,6 +59,30 @@ function filterAndMap(array, mapFilter) {
|
|
|
59
59
|
}
|
|
60
60
|
return result;
|
|
61
61
|
}
|
|
62
|
+
function sortBy(arr, sortByValue, props = "asc") {
|
|
63
|
+
const order = Array.isArray(props) || typeof props === "string" ? props : props.order ?? "asc";
|
|
64
|
+
return [...arr].sort((a, b) => {
|
|
65
|
+
const _aPriority = sortByValue(a);
|
|
66
|
+
const _bPriority = sortByValue(b);
|
|
67
|
+
const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];
|
|
68
|
+
const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];
|
|
69
|
+
for (let i = 0; i < aPriority.length; i++) {
|
|
70
|
+
const levelOrder = typeof order === "string" ? order : order[i] ?? "asc";
|
|
71
|
+
const aP = aPriority[i] ?? 0;
|
|
72
|
+
const bP = bPriority[i] ?? 0;
|
|
73
|
+
if (aP === bP) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (bP === Infinity || aP === -Infinity || aP < bP) {
|
|
77
|
+
return levelOrder === "asc" ? -1 : 1;
|
|
78
|
+
}
|
|
79
|
+
if (aP === Infinity || bP === -Infinity || aP > bP) {
|
|
80
|
+
return levelOrder === "asc" ? 1 : -1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return 0;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
62
86
|
function arrayWithPrevAndIndex(array) {
|
|
63
87
|
return array.map((item, i) => ({
|
|
64
88
|
item,
|
|
@@ -135,16 +159,104 @@ function deepEqual(foo, bar, maxDepth = 20) {
|
|
|
135
159
|
function filterObjectOrArrayKeys(objOrArray, {
|
|
136
160
|
filterKeys,
|
|
137
161
|
rejectKeys,
|
|
138
|
-
rejectEmptyObjectsInArray = true
|
|
162
|
+
rejectEmptyObjectsInArray = true,
|
|
163
|
+
sortKeys = "simpleValuesFirst",
|
|
164
|
+
sortPatterns
|
|
139
165
|
}) {
|
|
166
|
+
function getNestedValue(obj, path) {
|
|
167
|
+
const parts = path.split(".");
|
|
168
|
+
let current = obj;
|
|
169
|
+
for (const part of parts) {
|
|
170
|
+
if (current == null || typeof current !== "object") {
|
|
171
|
+
return void 0;
|
|
172
|
+
}
|
|
173
|
+
current = current[part];
|
|
174
|
+
}
|
|
175
|
+
return current;
|
|
176
|
+
}
|
|
177
|
+
function evaluateCondition(item, condition) {
|
|
178
|
+
const value = getNestedValue(item, condition.property);
|
|
179
|
+
let valueStr = String(value);
|
|
180
|
+
if (condition.caseInsensitive) {
|
|
181
|
+
valueStr = valueStr.toLowerCase();
|
|
182
|
+
}
|
|
183
|
+
const processValue = (v) => condition.caseInsensitive ? v.toLowerCase() : v;
|
|
184
|
+
switch (condition.operator) {
|
|
185
|
+
case "=":
|
|
186
|
+
return condition.values.some((v) => valueStr === processValue(v));
|
|
187
|
+
case "!=":
|
|
188
|
+
return condition.values.every((v) => valueStr !== processValue(v));
|
|
189
|
+
case "*=":
|
|
190
|
+
return condition.values.some((v) => valueStr.includes(processValue(v)));
|
|
191
|
+
case "!*=":
|
|
192
|
+
return condition.values.every(
|
|
193
|
+
(v) => !valueStr.includes(processValue(v))
|
|
194
|
+
);
|
|
195
|
+
case "^=":
|
|
196
|
+
return condition.values.some(
|
|
197
|
+
(v) => valueStr.startsWith(processValue(v))
|
|
198
|
+
);
|
|
199
|
+
case "!^=":
|
|
200
|
+
return condition.values.every(
|
|
201
|
+
(v) => !valueStr.startsWith(processValue(v))
|
|
202
|
+
);
|
|
203
|
+
case "$=":
|
|
204
|
+
return condition.values.some((v) => valueStr.endsWith(processValue(v)));
|
|
205
|
+
case "!$=":
|
|
206
|
+
return condition.values.every(
|
|
207
|
+
(v) => !valueStr.endsWith(processValue(v))
|
|
208
|
+
);
|
|
209
|
+
default:
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
140
213
|
const toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
|
|
141
214
|
const filterPatternsRaw = toArray(filterKeys);
|
|
142
215
|
const rejectPatternsRaw = toArray(rejectKeys);
|
|
143
216
|
const hasFilters = filterPatternsRaw.length > 0;
|
|
144
217
|
const hasRejects = rejectPatternsRaw.length > 0;
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
|
|
218
|
+
const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
|
|
219
|
+
const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
|
|
220
|
+
const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
|
|
221
|
+
expandedFilterPatterns
|
|
222
|
+
);
|
|
223
|
+
const filterPatterns = filterOnlyPatterns.map(parsePattern);
|
|
224
|
+
const rejectPatterns = expandedRejectPatterns.map(parsePattern);
|
|
225
|
+
const sortPatternsRaw = toArray(sortPatterns);
|
|
226
|
+
const expandedSortPatterns = sortPatternsRaw.flatMap(expandPatterns);
|
|
227
|
+
const sortPatternsParsed = expandedSortPatterns.map(parsePattern);
|
|
228
|
+
let dataToProcess = objOrArray;
|
|
229
|
+
if (combinedPatterns.length > 0) {
|
|
230
|
+
const groupedByFilter = /* @__PURE__ */ new Map();
|
|
231
|
+
for (const { filterPart, fieldPart } of combinedPatterns) {
|
|
232
|
+
if (!groupedByFilter.has(filterPart)) {
|
|
233
|
+
groupedByFilter.set(filterPart, []);
|
|
234
|
+
}
|
|
235
|
+
groupedByFilter.get(filterPart).push(fieldPart);
|
|
236
|
+
}
|
|
237
|
+
const combinedResult = Array.isArray(objOrArray) ? [] : {};
|
|
238
|
+
for (const [filterPart, fieldParts] of groupedByFilter) {
|
|
239
|
+
const filteredResult = filterObjectOrArrayKeys(objOrArray, {
|
|
240
|
+
filterKeys: [filterPart],
|
|
241
|
+
rejectKeys,
|
|
242
|
+
rejectEmptyObjectsInArray
|
|
243
|
+
});
|
|
244
|
+
const fieldSelectedResult = filterObjectOrArrayKeys(filteredResult, {
|
|
245
|
+
filterKeys: fieldParts,
|
|
246
|
+
rejectEmptyObjectsInArray
|
|
247
|
+
});
|
|
248
|
+
if (Array.isArray(combinedResult) && Array.isArray(fieldSelectedResult)) {
|
|
249
|
+
combinedResult.push(...fieldSelectedResult);
|
|
250
|
+
} else if (!Array.isArray(combinedResult) && !Array.isArray(fieldSelectedResult)) {
|
|
251
|
+
Object.assign(combinedResult, fieldSelectedResult);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (filterOnlyPatterns.length === 0) {
|
|
255
|
+
return combinedResult;
|
|
256
|
+
}
|
|
257
|
+
dataToProcess = combinedResult;
|
|
258
|
+
}
|
|
259
|
+
function matchPath(path, pattern, value) {
|
|
148
260
|
function rec(pi, pti) {
|
|
149
261
|
if (pti >= pattern.length) return pi === path.length;
|
|
150
262
|
const pt = pattern[pti];
|
|
@@ -171,11 +283,6 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
171
283
|
return rec(pi + 1, pti + 1);
|
|
172
284
|
if (ct.type === "INDEX") return rec(pi + 1, pti);
|
|
173
285
|
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
286
|
case "INDEX":
|
|
180
287
|
if (ct.type === "INDEX" && ct.index === pt.index)
|
|
181
288
|
return rec(pi + 1, pti + 1);
|
|
@@ -190,12 +297,91 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
190
297
|
if (okLower && okUpper) return rec(pi + 1, pti + 1);
|
|
191
298
|
}
|
|
192
299
|
return false;
|
|
300
|
+
case "INDEX_FILTER":
|
|
301
|
+
if (ct.type === "INDEX" && value !== void 0) {
|
|
302
|
+
const results = pt.conditions.map(
|
|
303
|
+
(cond) => evaluateCondition(value, cond)
|
|
304
|
+
);
|
|
305
|
+
const matches = pt.logic === "AND" ? results.every((r) => r) : results.some((r) => r);
|
|
306
|
+
if (matches) return rec(pi + 1, pti + 1);
|
|
307
|
+
}
|
|
308
|
+
return false;
|
|
193
309
|
}
|
|
194
310
|
}
|
|
195
311
|
return rec(0, 0);
|
|
196
312
|
}
|
|
197
|
-
const matchesAnyFilter = (path) => filterPatterns.some((p) => matchPath(path, p));
|
|
198
|
-
const matchesAnyReject = (path) => rejectPatterns.some((p) => matchPath(path, p));
|
|
313
|
+
const matchesAnyFilter = (path, value) => filterPatterns.some((p) => matchPath(path, p, value));
|
|
314
|
+
const matchesAnyReject = (path, value) => rejectPatterns.some((p) => matchPath(path, p, value));
|
|
315
|
+
function getSortPriority(path) {
|
|
316
|
+
for (let i = 0; i < sortPatternsParsed.length; i++) {
|
|
317
|
+
if (matchPath(path, sortPatternsParsed[i])) {
|
|
318
|
+
return i;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return sortPatternsParsed.length;
|
|
322
|
+
}
|
|
323
|
+
function applySortKeys(keys, obj, sortOrder) {
|
|
324
|
+
if (sortOrder === "asc") {
|
|
325
|
+
return [...keys].sort();
|
|
326
|
+
}
|
|
327
|
+
if (sortOrder === "desc") {
|
|
328
|
+
return [...keys].sort().reverse();
|
|
329
|
+
}
|
|
330
|
+
return sortBy(
|
|
331
|
+
sortBy(keys, (k) => k),
|
|
332
|
+
(key) => {
|
|
333
|
+
const value = obj[key];
|
|
334
|
+
if (value !== void 0 && value !== null) {
|
|
335
|
+
if (Array.isArray(value) && value.length === 0) return 0;
|
|
336
|
+
if (isPlainObject(value)) {
|
|
337
|
+
const objLength = Object.keys(value).length;
|
|
338
|
+
return 1.99 + objLength * -1e-3;
|
|
339
|
+
}
|
|
340
|
+
if (Array.isArray(value)) {
|
|
341
|
+
const allItemsArePrimitives = value.every(
|
|
342
|
+
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0
|
|
343
|
+
);
|
|
344
|
+
if (allItemsArePrimitives) {
|
|
345
|
+
return 1.9 + value.length * -1e-3;
|
|
346
|
+
} else {
|
|
347
|
+
return 1.5 + value.length * -0.01;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (typeof value === "boolean") return 4;
|
|
351
|
+
if (typeof value === "number") return 3.5;
|
|
352
|
+
if (typeof value === "string" && value.length < 20) return 3;
|
|
353
|
+
return 2;
|
|
354
|
+
}
|
|
355
|
+
return 0;
|
|
356
|
+
},
|
|
357
|
+
"desc"
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
function sortKeysWithPatterns(keys, obj, currentPath) {
|
|
361
|
+
if (!sortKeys && sortPatternsParsed.length === 0) {
|
|
362
|
+
return keys;
|
|
363
|
+
}
|
|
364
|
+
if (sortPatternsParsed.length === 0) {
|
|
365
|
+
return sortKeys ? applySortKeys(keys, obj, sortKeys) : keys;
|
|
366
|
+
}
|
|
367
|
+
const sortedKeys = [...keys].sort((a, b) => {
|
|
368
|
+
const pathA = currentPath.concat({ type: "KEY", name: a });
|
|
369
|
+
const pathB = currentPath.concat({ type: "KEY", name: b });
|
|
370
|
+
const priorityA = getSortPriority(pathA);
|
|
371
|
+
const priorityB = getSortPriority(pathB);
|
|
372
|
+
if (priorityA !== priorityB) {
|
|
373
|
+
return priorityA - priorityB;
|
|
374
|
+
}
|
|
375
|
+
if (sortKeys === "desc") {
|
|
376
|
+
return b.localeCompare(a);
|
|
377
|
+
}
|
|
378
|
+
if (sortKeys === "asc") {
|
|
379
|
+
return a.localeCompare(b);
|
|
380
|
+
}
|
|
381
|
+
return 0;
|
|
382
|
+
});
|
|
383
|
+
return sortedKeys;
|
|
384
|
+
}
|
|
199
385
|
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
200
386
|
if (Array.isArray(value)) {
|
|
201
387
|
if (stack2.has(value)) {
|
|
@@ -206,9 +392,9 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
206
392
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
207
393
|
for (let index = 0; index < value.length; index += 1) {
|
|
208
394
|
const childPath = path.concat({ type: "INDEX", index });
|
|
209
|
-
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
210
395
|
const child = value[index];
|
|
211
|
-
|
|
396
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
397
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
212
398
|
const childAllowed = includeAllChildren || directInclude;
|
|
213
399
|
if (isPlainObject(child) || Array.isArray(child)) {
|
|
214
400
|
const builtChild = build(
|
|
@@ -243,7 +429,8 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
243
429
|
stack2.add(value);
|
|
244
430
|
const result = {};
|
|
245
431
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
246
|
-
|
|
432
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
433
|
+
for (const key of sortedKeys) {
|
|
247
434
|
const childPath = path.concat({ type: "KEY", name: key });
|
|
248
435
|
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
249
436
|
const val = value[key];
|
|
@@ -289,16 +476,149 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
289
476
|
const initialAllowed = !hasFilters;
|
|
290
477
|
const stack = /* @__PURE__ */ new WeakSet();
|
|
291
478
|
const built = build(
|
|
292
|
-
|
|
479
|
+
dataToProcess,
|
|
293
480
|
startPath,
|
|
294
481
|
initialAllowed,
|
|
295
482
|
stack,
|
|
296
483
|
true,
|
|
297
484
|
false
|
|
298
485
|
);
|
|
299
|
-
if (built === void 0) return Array.isArray(
|
|
486
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
300
487
|
return built;
|
|
301
488
|
}
|
|
489
|
+
function parseFilterConditions(filterContent) {
|
|
490
|
+
const conditions = [];
|
|
491
|
+
let logic = "AND";
|
|
492
|
+
const caseInsensitive = filterContent.startsWith("i");
|
|
493
|
+
const content = caseInsensitive ? filterContent.slice(1) : filterContent;
|
|
494
|
+
const hasAnd = content.includes("&&");
|
|
495
|
+
const hasOr = content.includes(" || ");
|
|
496
|
+
if (hasAnd && hasOr) {
|
|
497
|
+
throw new Error(
|
|
498
|
+
"Mixing && and || operators in the same filter is not supported. Use separate filter patterns instead."
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
const andGroups = content.split("&&").map((s) => s.trim());
|
|
502
|
+
for (const andGroup of andGroups) {
|
|
503
|
+
if (andGroup.includes(" || ")) {
|
|
504
|
+
logic = "OR";
|
|
505
|
+
const orConditions = andGroup.split(" || ").map((s) => s.trim());
|
|
506
|
+
for (const orCondition of orConditions) {
|
|
507
|
+
const parsed = parseSingleCondition(orCondition, caseInsensitive);
|
|
508
|
+
if (parsed) {
|
|
509
|
+
conditions.push(parsed);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
} else {
|
|
513
|
+
const parsed = parseSingleCondition(andGroup, caseInsensitive);
|
|
514
|
+
if (parsed) {
|
|
515
|
+
conditions.push(parsed);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (conditions.length === 0) {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
return {
|
|
523
|
+
type: "INDEX_FILTER",
|
|
524
|
+
conditions,
|
|
525
|
+
logic
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function parseSingleCondition(condition, caseInsensitive = false) {
|
|
529
|
+
const cleanCondition = condition.startsWith("%") ? condition.slice(1) : condition;
|
|
530
|
+
let operator = null;
|
|
531
|
+
let operatorIndex = -1;
|
|
532
|
+
let operatorLength = 0;
|
|
533
|
+
const operators = [
|
|
534
|
+
["!*=", "!*="],
|
|
535
|
+
["!^=", "!^="],
|
|
536
|
+
["!$=", "!$="],
|
|
537
|
+
["!=", "!="],
|
|
538
|
+
["*=", "*="],
|
|
539
|
+
["^=", "^="],
|
|
540
|
+
["$=", "$="],
|
|
541
|
+
["=", "="]
|
|
542
|
+
];
|
|
543
|
+
for (const [op, opType] of operators) {
|
|
544
|
+
const index = cleanCondition.indexOf(op);
|
|
545
|
+
if (index !== -1) {
|
|
546
|
+
operator = opType;
|
|
547
|
+
operatorIndex = index;
|
|
548
|
+
operatorLength = op.length;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (operator === null || operatorIndex === -1) {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
const property = cleanCondition.slice(0, operatorIndex).trim();
|
|
556
|
+
const valueStr = cleanCondition.slice(operatorIndex + operatorLength).trim();
|
|
557
|
+
const values = [];
|
|
558
|
+
if (valueStr.includes(" | ")) {
|
|
559
|
+
const parts = valueStr.split(" | ");
|
|
560
|
+
for (const part of parts) {
|
|
561
|
+
const trimmed = part.trim();
|
|
562
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
563
|
+
values.push(value);
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
const trimmed = valueStr.trim();
|
|
567
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
568
|
+
values.push(value);
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
property,
|
|
572
|
+
operator,
|
|
573
|
+
values,
|
|
574
|
+
caseInsensitive
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
function separateFilterPatterns(patterns) {
|
|
578
|
+
const filterOnlyPatterns = [];
|
|
579
|
+
const combinedPatterns = [];
|
|
580
|
+
for (const pattern of patterns) {
|
|
581
|
+
const filterMatch = pattern.match(/^(.+\[[i%][^[\]]*\])\.(.+)$/);
|
|
582
|
+
if (filterMatch?.[1] && filterMatch[2]) {
|
|
583
|
+
const filterPart = filterMatch[1];
|
|
584
|
+
const fieldPart = filterMatch[2];
|
|
585
|
+
const baseArrayPath = filterPart.replace(/\[[i%][^[\]]*\]/, "[*]");
|
|
586
|
+
combinedPatterns.push({
|
|
587
|
+
filterPart,
|
|
588
|
+
fieldPart: `${baseArrayPath}.${fieldPart}`
|
|
589
|
+
});
|
|
590
|
+
} else {
|
|
591
|
+
filterOnlyPatterns.push(pattern);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return { filterOnlyPatterns, combinedPatterns };
|
|
595
|
+
}
|
|
596
|
+
function expandPatterns(pattern) {
|
|
597
|
+
function expandSingle(str) {
|
|
598
|
+
const start = str.indexOf("(");
|
|
599
|
+
if (start === -1) {
|
|
600
|
+
return [str];
|
|
601
|
+
}
|
|
602
|
+
const end = str.indexOf(")", start);
|
|
603
|
+
if (end === -1) {
|
|
604
|
+
return [str];
|
|
605
|
+
}
|
|
606
|
+
const before = str.slice(0, start);
|
|
607
|
+
const inside = str.slice(start + 1, end);
|
|
608
|
+
const after = str.slice(end + 1);
|
|
609
|
+
if (!inside.includes("|")) {
|
|
610
|
+
return expandSingle(before + inside + after);
|
|
611
|
+
}
|
|
612
|
+
const options = inside.split("|").filter((option) => option.trim().length > 0);
|
|
613
|
+
const results = [];
|
|
614
|
+
for (const option of options) {
|
|
615
|
+
const newStr = before + option + after;
|
|
616
|
+
results.push(...expandSingle(newStr));
|
|
617
|
+
}
|
|
618
|
+
return results;
|
|
619
|
+
}
|
|
620
|
+
return expandSingle(pattern);
|
|
621
|
+
}
|
|
302
622
|
function parsePattern(pattern) {
|
|
303
623
|
const tokens = [];
|
|
304
624
|
let i = 0;
|
|
@@ -316,7 +636,20 @@ function parsePattern(pattern) {
|
|
|
316
636
|
if (ch === "[") {
|
|
317
637
|
const end = pattern.indexOf("]", i + 1);
|
|
318
638
|
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
319
|
-
if (inside
|
|
639
|
+
if (inside.startsWith("%") || inside.startsWith("i%")) {
|
|
640
|
+
let filterContent;
|
|
641
|
+
if (inside.startsWith("i%")) {
|
|
642
|
+
filterContent = `i${inside.slice(2)}`;
|
|
643
|
+
} else if (inside.startsWith("%")) {
|
|
644
|
+
filterContent = inside.slice(1);
|
|
645
|
+
} else {
|
|
646
|
+
filterContent = inside;
|
|
647
|
+
}
|
|
648
|
+
const filterToken = parseFilterConditions(filterContent);
|
|
649
|
+
if (filterToken) {
|
|
650
|
+
tokens.push(filterToken);
|
|
651
|
+
}
|
|
652
|
+
} else if (inside === "*") {
|
|
320
653
|
tokens.push({ type: "INDEX_ANY" });
|
|
321
654
|
} else if (inside.includes("-")) {
|
|
322
655
|
const parts = inside.split("-");
|
|
@@ -336,18 +669,6 @@ function parsePattern(pattern) {
|
|
|
336
669
|
i = end === -1 ? n : end + 1;
|
|
337
670
|
continue;
|
|
338
671
|
}
|
|
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
672
|
if (ch === "*") {
|
|
352
673
|
if (pattern[i + 1] === "*") {
|
|
353
674
|
tokens.push({ type: "WILDCARD_ANY" });
|
|
@@ -1024,6 +1345,8 @@ function compactSnapshot(value, {
|
|
|
1024
1345
|
showBooleansAs = true,
|
|
1025
1346
|
rejectKeys,
|
|
1026
1347
|
filterKeys,
|
|
1348
|
+
sortKeys,
|
|
1349
|
+
sortPatterns,
|
|
1027
1350
|
...options
|
|
1028
1351
|
} = {}) {
|
|
1029
1352
|
let processedValue = value;
|
|
@@ -1031,7 +1354,9 @@ function compactSnapshot(value, {
|
|
|
1031
1354
|
if (isPlainObject(processedValue) || Array.isArray(processedValue)) {
|
|
1032
1355
|
processedValue = filterObjectOrArrayKeys(processedValue, {
|
|
1033
1356
|
rejectKeys,
|
|
1034
|
-
filterKeys
|
|
1357
|
+
filterKeys,
|
|
1358
|
+
sortKeys,
|
|
1359
|
+
sortPatterns
|
|
1035
1360
|
});
|
|
1036
1361
|
}
|
|
1037
1362
|
}
|
package/lib/testUtils.d.cts
CHANGED
|
@@ -69,6 +69,15 @@ 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`
|
|
74
|
+
* - Array filtering by value:
|
|
75
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
76
|
+
* - `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name` property equal to `John` or `Jane`
|
|
77
|
+
* - `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by using `&&` and `||`, nesting logical operators is not supported yet
|
|
78
|
+
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
79
|
+
*
|
|
80
|
+
* Check more supported patterns in {@link filterObjectOrArrayKeys} docs.
|
|
72
81
|
*
|
|
73
82
|
* @param value - The value to snapshot.
|
|
74
83
|
* @param options - The options for the snapshot.
|
|
@@ -79,9 +88,11 @@ declare function waitController(): {
|
|
|
79
88
|
* @param options.rejectKeys - The keys to reject.
|
|
80
89
|
* @param options.filterKeys - The keys to filter.
|
|
81
90
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
+
* @param options.sortKeys - Sort all keys by a specific order (default: `simpleValuesFirst`).
|
|
92
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
82
93
|
* @returns The compact snapshot of the value.
|
|
83
94
|
*/
|
|
84
|
-
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, ...options }?: YamlStringifyOptions & {
|
|
95
|
+
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
85
96
|
showBooleansAs?: boolean | {
|
|
86
97
|
props?: Record<string, {
|
|
87
98
|
trueText?: string;
|
|
@@ -91,47 +102,10 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
91
102
|
trueText?: string;
|
|
92
103
|
falseText?: string;
|
|
93
104
|
};
|
|
94
|
-
/**
|
|
95
|
-
* Reject (exclude) keys from the snapshot using pattern matching.
|
|
96
|
-
*
|
|
97
|
-
* **Examples:**
|
|
98
|
-
* ```typescript
|
|
99
|
-
* // Reject root-level 'secret' only
|
|
100
|
-
* rejectKeys: ['secret']
|
|
101
|
-
*
|
|
102
|
-
* // Reject nested 'password' properties only
|
|
103
|
-
* rejectKeys: ['*.password']
|
|
104
|
-
*
|
|
105
|
-
* // Reject any 'apiKey' property at any level
|
|
106
|
-
* rejectKeys: ['*apiKey']
|
|
107
|
-
*
|
|
108
|
-
* // Reject specific nested path
|
|
109
|
-
* rejectKeys: ['user.settings.theme']
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
105
|
rejectKeys?: string[] | string;
|
|
113
|
-
/**
|
|
114
|
-
* Filter (include only) keys that match the specified patterns.
|
|
115
|
-
*
|
|
116
|
-
* **Examples:**
|
|
117
|
-
* ```typescript
|
|
118
|
-
* // Include only root-level 'user'
|
|
119
|
-
* filterKeys: ['user']
|
|
120
|
-
*
|
|
121
|
-
* // Include all 'name' properties at any level
|
|
122
|
-
* filterKeys: ['*name']
|
|
123
|
-
*
|
|
124
|
-
* // Include only nested 'id' properties
|
|
125
|
-
* filterKeys: ['*.id']
|
|
126
|
-
*
|
|
127
|
-
* // Include specific nested paths
|
|
128
|
-
* filterKeys: ['user.profile.email', 'settings.theme']
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* **Note:** When filtering, parent paths are automatically included if needed
|
|
132
|
-
* to preserve the structure for nested matches.
|
|
133
|
-
*/
|
|
134
106
|
filterKeys?: string[] | string;
|
|
107
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
108
|
+
sortPatterns?: string[];
|
|
135
109
|
}): string;
|
|
136
110
|
|
|
137
111
|
export { compactSnapshot, createLoggerStore, getResultFn, waitController };
|
package/lib/testUtils.d.ts
CHANGED
|
@@ -69,6 +69,15 @@ 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`
|
|
74
|
+
* - Array filtering by value:
|
|
75
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
76
|
+
* - `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name` property equal to `John` or `Jane`
|
|
77
|
+
* - `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by using `&&` and `||`, nesting logical operators is not supported yet
|
|
78
|
+
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
79
|
+
*
|
|
80
|
+
* Check more supported patterns in {@link filterObjectOrArrayKeys} docs.
|
|
72
81
|
*
|
|
73
82
|
* @param value - The value to snapshot.
|
|
74
83
|
* @param options - The options for the snapshot.
|
|
@@ -79,9 +88,11 @@ declare function waitController(): {
|
|
|
79
88
|
* @param options.rejectKeys - The keys to reject.
|
|
80
89
|
* @param options.filterKeys - The keys to filter.
|
|
81
90
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
+
* @param options.sortKeys - Sort all keys by a specific order (default: `simpleValuesFirst`).
|
|
92
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
82
93
|
* @returns The compact snapshot of the value.
|
|
83
94
|
*/
|
|
84
|
-
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, ...options }?: YamlStringifyOptions & {
|
|
95
|
+
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
85
96
|
showBooleansAs?: boolean | {
|
|
86
97
|
props?: Record<string, {
|
|
87
98
|
trueText?: string;
|
|
@@ -91,47 +102,10 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
91
102
|
trueText?: string;
|
|
92
103
|
falseText?: string;
|
|
93
104
|
};
|
|
94
|
-
/**
|
|
95
|
-
* Reject (exclude) keys from the snapshot using pattern matching.
|
|
96
|
-
*
|
|
97
|
-
* **Examples:**
|
|
98
|
-
* ```typescript
|
|
99
|
-
* // Reject root-level 'secret' only
|
|
100
|
-
* rejectKeys: ['secret']
|
|
101
|
-
*
|
|
102
|
-
* // Reject nested 'password' properties only
|
|
103
|
-
* rejectKeys: ['*.password']
|
|
104
|
-
*
|
|
105
|
-
* // Reject any 'apiKey' property at any level
|
|
106
|
-
* rejectKeys: ['*apiKey']
|
|
107
|
-
*
|
|
108
|
-
* // Reject specific nested path
|
|
109
|
-
* rejectKeys: ['user.settings.theme']
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
105
|
rejectKeys?: string[] | string;
|
|
113
|
-
/**
|
|
114
|
-
* Filter (include only) keys that match the specified patterns.
|
|
115
|
-
*
|
|
116
|
-
* **Examples:**
|
|
117
|
-
* ```typescript
|
|
118
|
-
* // Include only root-level 'user'
|
|
119
|
-
* filterKeys: ['user']
|
|
120
|
-
*
|
|
121
|
-
* // Include all 'name' properties at any level
|
|
122
|
-
* filterKeys: ['*name']
|
|
123
|
-
*
|
|
124
|
-
* // Include only nested 'id' properties
|
|
125
|
-
* filterKeys: ['*.id']
|
|
126
|
-
*
|
|
127
|
-
* // Include specific nested paths
|
|
128
|
-
* filterKeys: ['user.profile.email', 'settings.theme']
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* **Note:** When filtering, parent paths are automatically included if needed
|
|
132
|
-
* to preserve the structure for nested matches.
|
|
133
|
-
*/
|
|
134
106
|
filterKeys?: string[] | string;
|
|
107
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
108
|
+
sortPatterns?: string[];
|
|
135
109
|
}): string;
|
|
136
110
|
|
|
137
111
|
export { compactSnapshot, createLoggerStore, getResultFn, waitController };
|
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-
|
|
14
|
+
} from "./chunk-MVN6FKX7.js";
|
|
15
15
|
import {
|
|
16
16
|
defer
|
|
17
17
|
} from "./chunk-DFXNVEH6.js";
|
|
@@ -263,6 +263,8 @@ function compactSnapshot(value, {
|
|
|
263
263
|
showBooleansAs = true,
|
|
264
264
|
rejectKeys,
|
|
265
265
|
filterKeys,
|
|
266
|
+
sortKeys,
|
|
267
|
+
sortPatterns,
|
|
266
268
|
...options
|
|
267
269
|
} = {}) {
|
|
268
270
|
let processedValue = value;
|
|
@@ -270,7 +272,9 @@ function compactSnapshot(value, {
|
|
|
270
272
|
if (isPlainObject(processedValue) || Array.isArray(processedValue)) {
|
|
271
273
|
processedValue = filterObjectOrArrayKeys(processedValue, {
|
|
272
274
|
rejectKeys,
|
|
273
|
-
filterKeys
|
|
275
|
+
filterKeys,
|
|
276
|
+
sortKeys,
|
|
277
|
+
sortPatterns
|
|
274
278
|
});
|
|
275
279
|
}
|
|
276
280
|
}
|