@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.
- package/docs/filterObjectOrArrayKeys.md +26 -1
- package/docs/testUtils.md +8 -1
- package/lib/chunk-MVN6FKX7.js +557 -0
- package/lib/filterObjectOrArrayKeys.cjs +323 -11
- package/lib/filterObjectOrArrayKeys.d.cts +18 -1
- package/lib/filterObjectOrArrayKeys.d.ts +18 -1
- package/lib/filterObjectOrArrayKeys.js +3 -1
- package/lib/testUtils.cjs +326 -12
- package/lib/testUtils.d.cts +12 -40
- package/lib/testUtils.d.ts +12 -40
- package/lib/testUtils.js +6 -2
- package/package.json +1 -1
- package/lib/chunk-2WZGT4NA.js +0 -268
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,8 +159,57 @@ 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);
|
|
@@ -144,9 +217,46 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
144
217
|
const hasRejects = rejectPatternsRaw.length > 0;
|
|
145
218
|
const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
|
|
146
219
|
const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
|
|
147
|
-
const
|
|
220
|
+
const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
|
|
221
|
+
expandedFilterPatterns
|
|
222
|
+
);
|
|
223
|
+
const filterPatterns = filterOnlyPatterns.map(parsePattern);
|
|
148
224
|
const rejectPatterns = expandedRejectPatterns.map(parsePattern);
|
|
149
|
-
|
|
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) {
|
|
150
260
|
function rec(pi, pti) {
|
|
151
261
|
if (pti >= pattern.length) return pi === path.length;
|
|
152
262
|
const pt = pattern[pti];
|
|
@@ -187,12 +297,91 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
187
297
|
if (okLower && okUpper) return rec(pi + 1, pti + 1);
|
|
188
298
|
}
|
|
189
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;
|
|
190
309
|
}
|
|
191
310
|
}
|
|
192
311
|
return rec(0, 0);
|
|
193
312
|
}
|
|
194
|
-
const matchesAnyFilter = (path) => filterPatterns.some((p) => matchPath(path, p));
|
|
195
|
-
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
|
+
}
|
|
196
385
|
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
197
386
|
if (Array.isArray(value)) {
|
|
198
387
|
if (stack2.has(value)) {
|
|
@@ -203,9 +392,9 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
203
392
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
204
393
|
for (let index = 0; index < value.length; index += 1) {
|
|
205
394
|
const childPath = path.concat({ type: "INDEX", index });
|
|
206
|
-
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
207
395
|
const child = value[index];
|
|
208
|
-
|
|
396
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
397
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
209
398
|
const childAllowed = includeAllChildren || directInclude;
|
|
210
399
|
if (isPlainObject(child) || Array.isArray(child)) {
|
|
211
400
|
const builtChild = build(
|
|
@@ -240,7 +429,8 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
240
429
|
stack2.add(value);
|
|
241
430
|
const result = {};
|
|
242
431
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
243
|
-
|
|
432
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
433
|
+
for (const key of sortedKeys) {
|
|
244
434
|
const childPath = path.concat({ type: "KEY", name: key });
|
|
245
435
|
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
246
436
|
const val = value[key];
|
|
@@ -286,16 +476,123 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
286
476
|
const initialAllowed = !hasFilters;
|
|
287
477
|
const stack = /* @__PURE__ */ new WeakSet();
|
|
288
478
|
const built = build(
|
|
289
|
-
|
|
479
|
+
dataToProcess,
|
|
290
480
|
startPath,
|
|
291
481
|
initialAllowed,
|
|
292
482
|
stack,
|
|
293
483
|
true,
|
|
294
484
|
false
|
|
295
485
|
);
|
|
296
|
-
if (built === void 0) return Array.isArray(
|
|
486
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
297
487
|
return built;
|
|
298
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
|
+
}
|
|
299
596
|
function expandPatterns(pattern) {
|
|
300
597
|
function expandSingle(str) {
|
|
301
598
|
const start = str.indexOf("(");
|
|
@@ -339,7 +636,20 @@ function parsePattern(pattern) {
|
|
|
339
636
|
if (ch === "[") {
|
|
340
637
|
const end = pattern.indexOf("]", i + 1);
|
|
341
638
|
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
342
|
-
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 === "*") {
|
|
343
653
|
tokens.push({ type: "INDEX_ANY" });
|
|
344
654
|
} else if (inside.includes("-")) {
|
|
345
655
|
const parts = inside.split("-");
|
|
@@ -1035,6 +1345,8 @@ function compactSnapshot(value, {
|
|
|
1035
1345
|
showBooleansAs = true,
|
|
1036
1346
|
rejectKeys,
|
|
1037
1347
|
filterKeys,
|
|
1348
|
+
sortKeys,
|
|
1349
|
+
sortPatterns,
|
|
1038
1350
|
...options
|
|
1039
1351
|
} = {}) {
|
|
1040
1352
|
let processedValue = value;
|
|
@@ -1042,7 +1354,9 @@ function compactSnapshot(value, {
|
|
|
1042
1354
|
if (isPlainObject(processedValue) || Array.isArray(processedValue)) {
|
|
1043
1355
|
processedValue = filterObjectOrArrayKeys(processedValue, {
|
|
1044
1356
|
rejectKeys,
|
|
1045
|
-
filterKeys
|
|
1357
|
+
filterKeys,
|
|
1358
|
+
sortKeys,
|
|
1359
|
+
sortPatterns
|
|
1046
1360
|
});
|
|
1047
1361
|
}
|
|
1048
1362
|
}
|
package/lib/testUtils.d.cts
CHANGED
|
@@ -71,6 +71,13 @@ declare function waitController(): {
|
|
|
71
71
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
72
72
|
* - Expanding the patterns with parentheses:
|
|
73
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.
|
|
74
81
|
*
|
|
75
82
|
* @param value - The value to snapshot.
|
|
76
83
|
* @param options - The options for the snapshot.
|
|
@@ -81,9 +88,11 @@ declare function waitController(): {
|
|
|
81
88
|
* @param options.rejectKeys - The keys to reject.
|
|
82
89
|
* @param options.filterKeys - The keys to filter.
|
|
83
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.
|
|
84
93
|
* @returns The compact snapshot of the value.
|
|
85
94
|
*/
|
|
86
|
-
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 & {
|
|
87
96
|
showBooleansAs?: boolean | {
|
|
88
97
|
props?: Record<string, {
|
|
89
98
|
trueText?: string;
|
|
@@ -93,47 +102,10 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
93
102
|
trueText?: string;
|
|
94
103
|
falseText?: string;
|
|
95
104
|
};
|
|
96
|
-
/**
|
|
97
|
-
* Reject (exclude) keys from the snapshot using pattern matching.
|
|
98
|
-
*
|
|
99
|
-
* **Examples:**
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Reject root-level 'secret' only
|
|
102
|
-
* rejectKeys: ['secret']
|
|
103
|
-
*
|
|
104
|
-
* // Reject nested 'password' properties only
|
|
105
|
-
* rejectKeys: ['*.password']
|
|
106
|
-
*
|
|
107
|
-
* // Reject any 'apiKey' property at any level
|
|
108
|
-
* rejectKeys: ['*apiKey']
|
|
109
|
-
*
|
|
110
|
-
* // Reject specific nested path
|
|
111
|
-
* rejectKeys: ['user.settings.theme']
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
105
|
rejectKeys?: string[] | string;
|
|
115
|
-
/**
|
|
116
|
-
* Filter (include only) keys that match the specified patterns.
|
|
117
|
-
*
|
|
118
|
-
* **Examples:**
|
|
119
|
-
* ```typescript
|
|
120
|
-
* // Include only root-level 'user'
|
|
121
|
-
* filterKeys: ['user']
|
|
122
|
-
*
|
|
123
|
-
* // Include all 'name' properties at any level
|
|
124
|
-
* filterKeys: ['*name']
|
|
125
|
-
*
|
|
126
|
-
* // Include only nested 'id' properties
|
|
127
|
-
* filterKeys: ['*.id']
|
|
128
|
-
*
|
|
129
|
-
* // Include specific nested paths
|
|
130
|
-
* filterKeys: ['user.profile.email', 'settings.theme']
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* **Note:** When filtering, parent paths are automatically included if needed
|
|
134
|
-
* to preserve the structure for nested matches.
|
|
135
|
-
*/
|
|
136
106
|
filterKeys?: string[] | string;
|
|
107
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
108
|
+
sortPatterns?: string[];
|
|
137
109
|
}): string;
|
|
138
110
|
|
|
139
111
|
export { compactSnapshot, createLoggerStore, getResultFn, waitController };
|
package/lib/testUtils.d.ts
CHANGED
|
@@ -71,6 +71,13 @@ declare function waitController(): {
|
|
|
71
71
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
72
72
|
* - Expanding the patterns with parentheses:
|
|
73
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.
|
|
74
81
|
*
|
|
75
82
|
* @param value - The value to snapshot.
|
|
76
83
|
* @param options - The options for the snapshot.
|
|
@@ -81,9 +88,11 @@ declare function waitController(): {
|
|
|
81
88
|
* @param options.rejectKeys - The keys to reject.
|
|
82
89
|
* @param options.filterKeys - The keys to filter.
|
|
83
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.
|
|
84
93
|
* @returns The compact snapshot of the value.
|
|
85
94
|
*/
|
|
86
|
-
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 & {
|
|
87
96
|
showBooleansAs?: boolean | {
|
|
88
97
|
props?: Record<string, {
|
|
89
98
|
trueText?: string;
|
|
@@ -93,47 +102,10 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
93
102
|
trueText?: string;
|
|
94
103
|
falseText?: string;
|
|
95
104
|
};
|
|
96
|
-
/**
|
|
97
|
-
* Reject (exclude) keys from the snapshot using pattern matching.
|
|
98
|
-
*
|
|
99
|
-
* **Examples:**
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Reject root-level 'secret' only
|
|
102
|
-
* rejectKeys: ['secret']
|
|
103
|
-
*
|
|
104
|
-
* // Reject nested 'password' properties only
|
|
105
|
-
* rejectKeys: ['*.password']
|
|
106
|
-
*
|
|
107
|
-
* // Reject any 'apiKey' property at any level
|
|
108
|
-
* rejectKeys: ['*apiKey']
|
|
109
|
-
*
|
|
110
|
-
* // Reject specific nested path
|
|
111
|
-
* rejectKeys: ['user.settings.theme']
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
105
|
rejectKeys?: string[] | string;
|
|
115
|
-
/**
|
|
116
|
-
* Filter (include only) keys that match the specified patterns.
|
|
117
|
-
*
|
|
118
|
-
* **Examples:**
|
|
119
|
-
* ```typescript
|
|
120
|
-
* // Include only root-level 'user'
|
|
121
|
-
* filterKeys: ['user']
|
|
122
|
-
*
|
|
123
|
-
* // Include all 'name' properties at any level
|
|
124
|
-
* filterKeys: ['*name']
|
|
125
|
-
*
|
|
126
|
-
* // Include only nested 'id' properties
|
|
127
|
-
* filterKeys: ['*.id']
|
|
128
|
-
*
|
|
129
|
-
* // Include specific nested paths
|
|
130
|
-
* filterKeys: ['user.profile.email', 'settings.theme']
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* **Note:** When filtering, parent paths are automatically included if needed
|
|
134
|
-
* to preserve the structure for nested matches.
|
|
135
|
-
*/
|
|
136
106
|
filterKeys?: string[] | string;
|
|
107
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
108
|
+
sortPatterns?: string[];
|
|
137
109
|
}): string;
|
|
138
110
|
|
|
139
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
|
}
|