@ls-stack/utils 3.26.1 → 3.27.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.
- package/docs/filterObjectOrArrayKeys.md +26 -1
- package/docs/testUtils.md +8 -1
- package/lib/chunk-TUJEGBFW.js +558 -0
- package/lib/filterObjectOrArrayKeys.cjs +324 -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 +327 -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
|
@@ -37,12 +37,87 @@ function isPlainObject(value) {
|
|
|
37
37
|
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// src/arrayUtils.ts
|
|
41
|
+
function sortBy(arr, sortByValue, props = "asc") {
|
|
42
|
+
const order = Array.isArray(props) || typeof props === "string" ? props : props.order ?? "asc";
|
|
43
|
+
return [...arr].sort((a, b) => {
|
|
44
|
+
const _aPriority = sortByValue(a);
|
|
45
|
+
const _bPriority = sortByValue(b);
|
|
46
|
+
const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];
|
|
47
|
+
const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];
|
|
48
|
+
for (let i = 0; i < aPriority.length; i++) {
|
|
49
|
+
const levelOrder = typeof order === "string" ? order : order[i] ?? "asc";
|
|
50
|
+
const aP = aPriority[i] ?? 0;
|
|
51
|
+
const bP = bPriority[i] ?? 0;
|
|
52
|
+
if (aP === bP) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (bP === Infinity || aP === -Infinity || aP < bP) {
|
|
56
|
+
return levelOrder === "asc" ? -1 : 1;
|
|
57
|
+
}
|
|
58
|
+
if (aP === Infinity || bP === -Infinity || aP > bP) {
|
|
59
|
+
return levelOrder === "asc" ? 1 : -1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return 0;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
40
66
|
// src/filterObjectOrArrayKeys.ts
|
|
41
67
|
function filterObjectOrArrayKeys(objOrArray, {
|
|
42
68
|
filterKeys,
|
|
43
69
|
rejectKeys,
|
|
44
|
-
rejectEmptyObjectsInArray = true
|
|
70
|
+
rejectEmptyObjectsInArray = true,
|
|
71
|
+
sortKeys = "simpleValuesFirst",
|
|
72
|
+
sortPatterns
|
|
45
73
|
}) {
|
|
74
|
+
function getNestedValue(obj, path) {
|
|
75
|
+
const parts = path.split(".");
|
|
76
|
+
let current = obj;
|
|
77
|
+
for (const part of parts) {
|
|
78
|
+
if (current == null || typeof current !== "object") {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
current = current[part];
|
|
82
|
+
}
|
|
83
|
+
return current;
|
|
84
|
+
}
|
|
85
|
+
function evaluateCondition(item, condition) {
|
|
86
|
+
const value = getNestedValue(item, condition.property);
|
|
87
|
+
let valueStr = String(value);
|
|
88
|
+
if (condition.caseInsensitive) {
|
|
89
|
+
valueStr = valueStr.toLowerCase();
|
|
90
|
+
}
|
|
91
|
+
const processValue = (v) => condition.caseInsensitive ? v.toLowerCase() : v;
|
|
92
|
+
switch (condition.operator) {
|
|
93
|
+
case "=":
|
|
94
|
+
return condition.values.some((v) => valueStr === processValue(v));
|
|
95
|
+
case "!=":
|
|
96
|
+
return condition.values.every((v) => valueStr !== processValue(v));
|
|
97
|
+
case "*=":
|
|
98
|
+
return condition.values.some((v) => valueStr.includes(processValue(v)));
|
|
99
|
+
case "!*=":
|
|
100
|
+
return condition.values.every(
|
|
101
|
+
(v) => !valueStr.includes(processValue(v))
|
|
102
|
+
);
|
|
103
|
+
case "^=":
|
|
104
|
+
return condition.values.some(
|
|
105
|
+
(v) => valueStr.startsWith(processValue(v))
|
|
106
|
+
);
|
|
107
|
+
case "!^=":
|
|
108
|
+
return condition.values.every(
|
|
109
|
+
(v) => !valueStr.startsWith(processValue(v))
|
|
110
|
+
);
|
|
111
|
+
case "$=":
|
|
112
|
+
return condition.values.some((v) => valueStr.endsWith(processValue(v)));
|
|
113
|
+
case "!$=":
|
|
114
|
+
return condition.values.every(
|
|
115
|
+
(v) => !valueStr.endsWith(processValue(v))
|
|
116
|
+
);
|
|
117
|
+
default:
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
46
121
|
const toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
|
|
47
122
|
const filterPatternsRaw = toArray(filterKeys);
|
|
48
123
|
const rejectPatternsRaw = toArray(rejectKeys);
|
|
@@ -50,9 +125,46 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
50
125
|
const hasRejects = rejectPatternsRaw.length > 0;
|
|
51
126
|
const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
|
|
52
127
|
const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
|
|
53
|
-
const
|
|
128
|
+
const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
|
|
129
|
+
expandedFilterPatterns
|
|
130
|
+
);
|
|
131
|
+
const filterPatterns = filterOnlyPatterns.map(parsePattern);
|
|
54
132
|
const rejectPatterns = expandedRejectPatterns.map(parsePattern);
|
|
55
|
-
|
|
133
|
+
const sortPatternsRaw = toArray(sortPatterns);
|
|
134
|
+
const expandedSortPatterns = sortPatternsRaw.flatMap(expandPatterns);
|
|
135
|
+
const sortPatternsParsed = expandedSortPatterns.map(parsePattern);
|
|
136
|
+
let dataToProcess = objOrArray;
|
|
137
|
+
if (combinedPatterns.length > 0) {
|
|
138
|
+
const groupedByFilter = /* @__PURE__ */ new Map();
|
|
139
|
+
for (const { filterPart, fieldPart } of combinedPatterns) {
|
|
140
|
+
if (!groupedByFilter.has(filterPart)) {
|
|
141
|
+
groupedByFilter.set(filterPart, []);
|
|
142
|
+
}
|
|
143
|
+
groupedByFilter.get(filterPart).push(fieldPart);
|
|
144
|
+
}
|
|
145
|
+
const combinedResult = Array.isArray(objOrArray) ? [] : {};
|
|
146
|
+
for (const [filterPart, fieldParts] of groupedByFilter) {
|
|
147
|
+
const filteredResult = filterObjectOrArrayKeys(objOrArray, {
|
|
148
|
+
filterKeys: [filterPart],
|
|
149
|
+
rejectKeys,
|
|
150
|
+
rejectEmptyObjectsInArray
|
|
151
|
+
});
|
|
152
|
+
const fieldSelectedResult = filterObjectOrArrayKeys(filteredResult, {
|
|
153
|
+
filterKeys: fieldParts,
|
|
154
|
+
rejectEmptyObjectsInArray
|
|
155
|
+
});
|
|
156
|
+
if (Array.isArray(combinedResult) && Array.isArray(fieldSelectedResult)) {
|
|
157
|
+
combinedResult.push(...fieldSelectedResult);
|
|
158
|
+
} else if (!Array.isArray(combinedResult) && !Array.isArray(fieldSelectedResult)) {
|
|
159
|
+
Object.assign(combinedResult, fieldSelectedResult);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (filterOnlyPatterns.length === 0) {
|
|
163
|
+
return combinedResult;
|
|
164
|
+
}
|
|
165
|
+
dataToProcess = combinedResult;
|
|
166
|
+
}
|
|
167
|
+
function matchPath(path, pattern, value) {
|
|
56
168
|
function rec(pi, pti) {
|
|
57
169
|
if (pti >= pattern.length) return pi === path.length;
|
|
58
170
|
const pt = pattern[pti];
|
|
@@ -93,12 +205,92 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
93
205
|
if (okLower && okUpper) return rec(pi + 1, pti + 1);
|
|
94
206
|
}
|
|
95
207
|
return false;
|
|
208
|
+
case "INDEX_FILTER":
|
|
209
|
+
if (ct.type === "INDEX" && value !== void 0) {
|
|
210
|
+
const results = pt.conditions.map(
|
|
211
|
+
(cond) => evaluateCondition(value, cond)
|
|
212
|
+
);
|
|
213
|
+
const matches = pt.logic === "AND" ? results.every((r) => r) : results.some((r) => r);
|
|
214
|
+
if (matches) return rec(pi + 1, pti + 1);
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
96
217
|
}
|
|
97
218
|
}
|
|
98
219
|
return rec(0, 0);
|
|
99
220
|
}
|
|
100
|
-
const matchesAnyFilter = (path) => filterPatterns.some((p) => matchPath(path, p));
|
|
101
|
-
const matchesAnyReject = (path) => rejectPatterns.some((p) => matchPath(path, p));
|
|
221
|
+
const matchesAnyFilter = (path, value) => filterPatterns.some((p) => matchPath(path, p, value));
|
|
222
|
+
const matchesAnyReject = (path, value) => rejectPatterns.some((p) => matchPath(path, p, value));
|
|
223
|
+
function getSortPriority(path) {
|
|
224
|
+
for (let i = 0; i < sortPatternsParsed.length; i++) {
|
|
225
|
+
if (matchPath(path, sortPatternsParsed[i])) {
|
|
226
|
+
return i;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return sortPatternsParsed.length;
|
|
230
|
+
}
|
|
231
|
+
function applySortKeys(keys, obj, sortOrder) {
|
|
232
|
+
if (sortOrder === "asc") {
|
|
233
|
+
return [...keys].sort();
|
|
234
|
+
}
|
|
235
|
+
if (sortOrder === "desc") {
|
|
236
|
+
return [...keys].sort().reverse();
|
|
237
|
+
}
|
|
238
|
+
return sortBy(
|
|
239
|
+
sortBy(keys, (k) => k),
|
|
240
|
+
(key) => {
|
|
241
|
+
const value = obj[key];
|
|
242
|
+
if (value !== void 0 && value !== null) {
|
|
243
|
+
if (Array.isArray(value) && value.length === 0) return 0;
|
|
244
|
+
if (isPlainObject(value)) {
|
|
245
|
+
const objLength = Object.keys(value).length;
|
|
246
|
+
return 1.99 + objLength * -1e-3;
|
|
247
|
+
}
|
|
248
|
+
if (Array.isArray(value)) {
|
|
249
|
+
const allItemsArePrimitives = value.every(
|
|
250
|
+
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0
|
|
251
|
+
);
|
|
252
|
+
if (allItemsArePrimitives) {
|
|
253
|
+
return 1.9 + value.length * -1e-3;
|
|
254
|
+
} else {
|
|
255
|
+
return 1.5 + value.length * -0.01;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (typeof value === "boolean") return 4;
|
|
259
|
+
if (typeof value === "number") return 3.5;
|
|
260
|
+
if (typeof value === "string" && value.length < 20) return 3;
|
|
261
|
+
return 2;
|
|
262
|
+
}
|
|
263
|
+
return 0;
|
|
264
|
+
},
|
|
265
|
+
"desc"
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
function sortKeysWithPatterns(keys_, obj, currentPath) {
|
|
269
|
+
if (!sortKeys && sortPatternsParsed.length === 0) {
|
|
270
|
+
return keys_;
|
|
271
|
+
}
|
|
272
|
+
let keysToSort = keys_;
|
|
273
|
+
if (sortKeys) {
|
|
274
|
+
keysToSort = applySortKeys(keysToSort, obj, sortKeys);
|
|
275
|
+
}
|
|
276
|
+
const sortedKeys = [...keysToSort].sort((a, b) => {
|
|
277
|
+
const pathA = currentPath.concat({ type: "KEY", name: a });
|
|
278
|
+
const pathB = currentPath.concat({ type: "KEY", name: b });
|
|
279
|
+
const priorityA = getSortPriority(pathA);
|
|
280
|
+
const priorityB = getSortPriority(pathB);
|
|
281
|
+
if (priorityA !== priorityB) {
|
|
282
|
+
return priorityA - priorityB;
|
|
283
|
+
}
|
|
284
|
+
if (sortKeys === "desc") {
|
|
285
|
+
return b.localeCompare(a);
|
|
286
|
+
}
|
|
287
|
+
if (sortKeys === "asc") {
|
|
288
|
+
return a.localeCompare(b);
|
|
289
|
+
}
|
|
290
|
+
return 0;
|
|
291
|
+
});
|
|
292
|
+
return sortedKeys;
|
|
293
|
+
}
|
|
102
294
|
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
103
295
|
if (Array.isArray(value)) {
|
|
104
296
|
if (stack2.has(value)) {
|
|
@@ -109,9 +301,9 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
109
301
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
110
302
|
for (let index = 0; index < value.length; index += 1) {
|
|
111
303
|
const childPath = path.concat({ type: "INDEX", index });
|
|
112
|
-
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
113
304
|
const child = value[index];
|
|
114
|
-
|
|
305
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
306
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
115
307
|
const childAllowed = includeAllChildren || directInclude;
|
|
116
308
|
if (isPlainObject(child) || Array.isArray(child)) {
|
|
117
309
|
const builtChild = build(
|
|
@@ -146,7 +338,8 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
146
338
|
stack2.add(value);
|
|
147
339
|
const result = {};
|
|
148
340
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
149
|
-
|
|
341
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
342
|
+
for (const key of sortedKeys) {
|
|
150
343
|
const childPath = path.concat({ type: "KEY", name: key });
|
|
151
344
|
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
152
345
|
const val = value[key];
|
|
@@ -192,16 +385,123 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
192
385
|
const initialAllowed = !hasFilters;
|
|
193
386
|
const stack = /* @__PURE__ */ new WeakSet();
|
|
194
387
|
const built = build(
|
|
195
|
-
|
|
388
|
+
dataToProcess,
|
|
196
389
|
startPath,
|
|
197
390
|
initialAllowed,
|
|
198
391
|
stack,
|
|
199
392
|
true,
|
|
200
393
|
false
|
|
201
394
|
);
|
|
202
|
-
if (built === void 0) return Array.isArray(
|
|
395
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
203
396
|
return built;
|
|
204
397
|
}
|
|
398
|
+
function parseFilterConditions(filterContent) {
|
|
399
|
+
const conditions = [];
|
|
400
|
+
let logic = "AND";
|
|
401
|
+
const caseInsensitive = filterContent.startsWith("i");
|
|
402
|
+
const content = caseInsensitive ? filterContent.slice(1) : filterContent;
|
|
403
|
+
const hasAnd = content.includes("&&");
|
|
404
|
+
const hasOr = content.includes(" || ");
|
|
405
|
+
if (hasAnd && hasOr) {
|
|
406
|
+
throw new Error(
|
|
407
|
+
"Mixing && and || operators in the same filter is not supported. Use separate filter patterns instead."
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
const andGroups = content.split("&&").map((s) => s.trim());
|
|
411
|
+
for (const andGroup of andGroups) {
|
|
412
|
+
if (andGroup.includes(" || ")) {
|
|
413
|
+
logic = "OR";
|
|
414
|
+
const orConditions = andGroup.split(" || ").map((s) => s.trim());
|
|
415
|
+
for (const orCondition of orConditions) {
|
|
416
|
+
const parsed = parseSingleCondition(orCondition, caseInsensitive);
|
|
417
|
+
if (parsed) {
|
|
418
|
+
conditions.push(parsed);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
} else {
|
|
422
|
+
const parsed = parseSingleCondition(andGroup, caseInsensitive);
|
|
423
|
+
if (parsed) {
|
|
424
|
+
conditions.push(parsed);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (conditions.length === 0) {
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
type: "INDEX_FILTER",
|
|
433
|
+
conditions,
|
|
434
|
+
logic
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
function parseSingleCondition(condition, caseInsensitive = false) {
|
|
438
|
+
const cleanCondition = condition.startsWith("%") ? condition.slice(1) : condition;
|
|
439
|
+
let operator = null;
|
|
440
|
+
let operatorIndex = -1;
|
|
441
|
+
let operatorLength = 0;
|
|
442
|
+
const operators = [
|
|
443
|
+
["!*=", "!*="],
|
|
444
|
+
["!^=", "!^="],
|
|
445
|
+
["!$=", "!$="],
|
|
446
|
+
["!=", "!="],
|
|
447
|
+
["*=", "*="],
|
|
448
|
+
["^=", "^="],
|
|
449
|
+
["$=", "$="],
|
|
450
|
+
["=", "="]
|
|
451
|
+
];
|
|
452
|
+
for (const [op, opType] of operators) {
|
|
453
|
+
const index = cleanCondition.indexOf(op);
|
|
454
|
+
if (index !== -1) {
|
|
455
|
+
operator = opType;
|
|
456
|
+
operatorIndex = index;
|
|
457
|
+
operatorLength = op.length;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (operator === null || operatorIndex === -1) {
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
const property = cleanCondition.slice(0, operatorIndex).trim();
|
|
465
|
+
const valueStr = cleanCondition.slice(operatorIndex + operatorLength).trim();
|
|
466
|
+
const values = [];
|
|
467
|
+
if (valueStr.includes(" | ")) {
|
|
468
|
+
const parts = valueStr.split(" | ");
|
|
469
|
+
for (const part of parts) {
|
|
470
|
+
const trimmed = part.trim();
|
|
471
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
472
|
+
values.push(value);
|
|
473
|
+
}
|
|
474
|
+
} else {
|
|
475
|
+
const trimmed = valueStr.trim();
|
|
476
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
477
|
+
values.push(value);
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
property,
|
|
481
|
+
operator,
|
|
482
|
+
values,
|
|
483
|
+
caseInsensitive
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
function separateFilterPatterns(patterns) {
|
|
487
|
+
const filterOnlyPatterns = [];
|
|
488
|
+
const combinedPatterns = [];
|
|
489
|
+
for (const pattern of patterns) {
|
|
490
|
+
const filterMatch = pattern.match(/^(.+\[[i%][^[\]]*\])\.(.+)$/);
|
|
491
|
+
if (filterMatch?.[1] && filterMatch[2]) {
|
|
492
|
+
const filterPart = filterMatch[1];
|
|
493
|
+
const fieldPart = filterMatch[2];
|
|
494
|
+
const baseArrayPath = filterPart.replace(/\[[i%][^[\]]*\]/, "[*]");
|
|
495
|
+
combinedPatterns.push({
|
|
496
|
+
filterPart,
|
|
497
|
+
fieldPart: `${baseArrayPath}.${fieldPart}`
|
|
498
|
+
});
|
|
499
|
+
} else {
|
|
500
|
+
filterOnlyPatterns.push(pattern);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return { filterOnlyPatterns, combinedPatterns };
|
|
504
|
+
}
|
|
205
505
|
function expandPatterns(pattern) {
|
|
206
506
|
function expandSingle(str) {
|
|
207
507
|
const start = str.indexOf("(");
|
|
@@ -245,7 +545,20 @@ function parsePattern(pattern) {
|
|
|
245
545
|
if (ch === "[") {
|
|
246
546
|
const end = pattern.indexOf("]", i + 1);
|
|
247
547
|
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
248
|
-
if (inside
|
|
548
|
+
if (inside.startsWith("%") || inside.startsWith("i%")) {
|
|
549
|
+
let filterContent;
|
|
550
|
+
if (inside.startsWith("i%")) {
|
|
551
|
+
filterContent = `i${inside.slice(2)}`;
|
|
552
|
+
} else if (inside.startsWith("%")) {
|
|
553
|
+
filterContent = inside.slice(1);
|
|
554
|
+
} else {
|
|
555
|
+
filterContent = inside;
|
|
556
|
+
}
|
|
557
|
+
const filterToken = parseFilterConditions(filterContent);
|
|
558
|
+
if (filterToken) {
|
|
559
|
+
tokens.push(filterToken);
|
|
560
|
+
}
|
|
561
|
+
} else if (inside === "*") {
|
|
249
562
|
tokens.push({ type: "INDEX_ANY" });
|
|
250
563
|
} else if (inside.includes("-")) {
|
|
251
564
|
const parts = inside.split("-");
|
|
@@ -29,18 +29,35 @@
|
|
|
29
29
|
* - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
|
|
30
30
|
* - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
|
|
31
31
|
* - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
|
|
32
|
+
* - Array filtering by value:
|
|
33
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
34
|
+
* - `'users[%name="John" | "Jane"]'` - Value-level OR using `|` for multiple values of same property
|
|
35
|
+
* - `'users[%name="Alice" || %age=35]'` - Property-level OR using `||` for different properties
|
|
36
|
+
* - `'users[%age=30 && %role="admin"]'` - Property-level AND using `&&` for different properties
|
|
37
|
+
* - Note: Mixing `&&` and `||` in the same filter is not supported - use separate filter patterns instead
|
|
38
|
+
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
39
|
+
* - `'users[%name*="oh"]'` - Contains operator (*=) - filters users where name contains "oh"
|
|
40
|
+
* - `'users[%name^="Jo"]'` - Starts with operator (^=) - filters users where name starts with "Jo"
|
|
41
|
+
* - `'users[%name$="hn"]'` - Ends with operator ($=) - filters users where name ends with "hn"
|
|
42
|
+
* - `'users[%name!="John"]'` - Not equal operator (!=) - filters users where name is not "John"
|
|
43
|
+
* - `'users[%name!*="admin"]'` - Not contains operator (!*=) - filters users where name doesn't contain "admin"
|
|
44
|
+
* - `'users[i%name="john"]'` - Case-insensitive matching (i% prefix) - matches "John", "JOHN", "john", etc.
|
|
32
45
|
*
|
|
33
46
|
* @param objOrArray - The object or array to filter.
|
|
34
47
|
* @param options - The options for the filter.
|
|
35
48
|
* @param options.filterKeys - The keys to filter.
|
|
36
49
|
* @param options.rejectKeys - The keys to reject.
|
|
37
50
|
* @param options.rejectEmptyObjectsInArray - Whether to reject empty objects in arrays (default: true).
|
|
51
|
+
* @param options.sortKeys - Sort all keys by a specific order (optional, preserves original order when not specified).
|
|
52
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
38
53
|
* @returns The filtered object or array.
|
|
39
54
|
*/
|
|
40
|
-
declare function filterObjectOrArrayKeys(objOrArray: Record<string, any> | Record<string, any>[], { filterKeys, rejectKeys, rejectEmptyObjectsInArray, }: {
|
|
55
|
+
declare function filterObjectOrArrayKeys(objOrArray: Record<string, any> | Record<string, any>[], { filterKeys, rejectKeys, rejectEmptyObjectsInArray, sortKeys, sortPatterns, }: {
|
|
41
56
|
filterKeys?: string[] | string;
|
|
42
57
|
rejectKeys?: string[] | string;
|
|
43
58
|
rejectEmptyObjectsInArray?: boolean;
|
|
59
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
60
|
+
sortPatterns?: string[];
|
|
44
61
|
}): Record<string, any> | Record<string, any>[];
|
|
45
62
|
|
|
46
63
|
export { filterObjectOrArrayKeys };
|
|
@@ -29,18 +29,35 @@
|
|
|
29
29
|
* - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
|
|
30
30
|
* - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
|
|
31
31
|
* - `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
|
|
32
|
+
* - Array filtering by value:
|
|
33
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
34
|
+
* - `'users[%name="John" | "Jane"]'` - Value-level OR using `|` for multiple values of same property
|
|
35
|
+
* - `'users[%name="Alice" || %age=35]'` - Property-level OR using `||` for different properties
|
|
36
|
+
* - `'users[%age=30 && %role="admin"]'` - Property-level AND using `&&` for different properties
|
|
37
|
+
* - Note: Mixing `&&` and `||` in the same filter is not supported - use separate filter patterns instead
|
|
38
|
+
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
39
|
+
* - `'users[%name*="oh"]'` - Contains operator (*=) - filters users where name contains "oh"
|
|
40
|
+
* - `'users[%name^="Jo"]'` - Starts with operator (^=) - filters users where name starts with "Jo"
|
|
41
|
+
* - `'users[%name$="hn"]'` - Ends with operator ($=) - filters users where name ends with "hn"
|
|
42
|
+
* - `'users[%name!="John"]'` - Not equal operator (!=) - filters users where name is not "John"
|
|
43
|
+
* - `'users[%name!*="admin"]'` - Not contains operator (!*=) - filters users where name doesn't contain "admin"
|
|
44
|
+
* - `'users[i%name="john"]'` - Case-insensitive matching (i% prefix) - matches "John", "JOHN", "john", etc.
|
|
32
45
|
*
|
|
33
46
|
* @param objOrArray - The object or array to filter.
|
|
34
47
|
* @param options - The options for the filter.
|
|
35
48
|
* @param options.filterKeys - The keys to filter.
|
|
36
49
|
* @param options.rejectKeys - The keys to reject.
|
|
37
50
|
* @param options.rejectEmptyObjectsInArray - Whether to reject empty objects in arrays (default: true).
|
|
51
|
+
* @param options.sortKeys - Sort all keys by a specific order (optional, preserves original order when not specified).
|
|
52
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
38
53
|
* @returns The filtered object or array.
|
|
39
54
|
*/
|
|
40
|
-
declare function filterObjectOrArrayKeys(objOrArray: Record<string, any> | Record<string, any>[], { filterKeys, rejectKeys, rejectEmptyObjectsInArray, }: {
|
|
55
|
+
declare function filterObjectOrArrayKeys(objOrArray: Record<string, any> | Record<string, any>[], { filterKeys, rejectKeys, rejectEmptyObjectsInArray, sortKeys, sortPatterns, }: {
|
|
41
56
|
filterKeys?: string[] | string;
|
|
42
57
|
rejectKeys?: string[] | string;
|
|
43
58
|
rejectEmptyObjectsInArray?: boolean;
|
|
59
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
60
|
+
sortPatterns?: string[];
|
|
44
61
|
}): Record<string, any> | Record<string, any>[];
|
|
45
62
|
|
|
46
63
|
export { filterObjectOrArrayKeys };
|