@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
|
@@ -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,91 @@ 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
|
+
if (sortPatternsParsed.length === 0) {
|
|
273
|
+
return sortKeys ? applySortKeys(keys, obj, sortKeys) : keys;
|
|
274
|
+
}
|
|
275
|
+
const sortedKeys = [...keys].sort((a, b) => {
|
|
276
|
+
const pathA = currentPath.concat({ type: "KEY", name: a });
|
|
277
|
+
const pathB = currentPath.concat({ type: "KEY", name: b });
|
|
278
|
+
const priorityA = getSortPriority(pathA);
|
|
279
|
+
const priorityB = getSortPriority(pathB);
|
|
280
|
+
if (priorityA !== priorityB) {
|
|
281
|
+
return priorityA - priorityB;
|
|
282
|
+
}
|
|
283
|
+
if (sortKeys === "desc") {
|
|
284
|
+
return b.localeCompare(a);
|
|
285
|
+
}
|
|
286
|
+
if (sortKeys === "asc") {
|
|
287
|
+
return a.localeCompare(b);
|
|
288
|
+
}
|
|
289
|
+
return 0;
|
|
290
|
+
});
|
|
291
|
+
return sortedKeys;
|
|
292
|
+
}
|
|
102
293
|
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
103
294
|
if (Array.isArray(value)) {
|
|
104
295
|
if (stack2.has(value)) {
|
|
@@ -109,9 +300,9 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
109
300
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
110
301
|
for (let index = 0; index < value.length; index += 1) {
|
|
111
302
|
const childPath = path.concat({ type: "INDEX", index });
|
|
112
|
-
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
113
303
|
const child = value[index];
|
|
114
|
-
|
|
304
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
305
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
115
306
|
const childAllowed = includeAllChildren || directInclude;
|
|
116
307
|
if (isPlainObject(child) || Array.isArray(child)) {
|
|
117
308
|
const builtChild = build(
|
|
@@ -146,7 +337,8 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
146
337
|
stack2.add(value);
|
|
147
338
|
const result = {};
|
|
148
339
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
149
|
-
|
|
340
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
341
|
+
for (const key of sortedKeys) {
|
|
150
342
|
const childPath = path.concat({ type: "KEY", name: key });
|
|
151
343
|
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
152
344
|
const val = value[key];
|
|
@@ -192,16 +384,123 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
192
384
|
const initialAllowed = !hasFilters;
|
|
193
385
|
const stack = /* @__PURE__ */ new WeakSet();
|
|
194
386
|
const built = build(
|
|
195
|
-
|
|
387
|
+
dataToProcess,
|
|
196
388
|
startPath,
|
|
197
389
|
initialAllowed,
|
|
198
390
|
stack,
|
|
199
391
|
true,
|
|
200
392
|
false
|
|
201
393
|
);
|
|
202
|
-
if (built === void 0) return Array.isArray(
|
|
394
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
203
395
|
return built;
|
|
204
396
|
}
|
|
397
|
+
function parseFilterConditions(filterContent) {
|
|
398
|
+
const conditions = [];
|
|
399
|
+
let logic = "AND";
|
|
400
|
+
const caseInsensitive = filterContent.startsWith("i");
|
|
401
|
+
const content = caseInsensitive ? filterContent.slice(1) : filterContent;
|
|
402
|
+
const hasAnd = content.includes("&&");
|
|
403
|
+
const hasOr = content.includes(" || ");
|
|
404
|
+
if (hasAnd && hasOr) {
|
|
405
|
+
throw new Error(
|
|
406
|
+
"Mixing && and || operators in the same filter is not supported. Use separate filter patterns instead."
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
const andGroups = content.split("&&").map((s) => s.trim());
|
|
410
|
+
for (const andGroup of andGroups) {
|
|
411
|
+
if (andGroup.includes(" || ")) {
|
|
412
|
+
logic = "OR";
|
|
413
|
+
const orConditions = andGroup.split(" || ").map((s) => s.trim());
|
|
414
|
+
for (const orCondition of orConditions) {
|
|
415
|
+
const parsed = parseSingleCondition(orCondition, caseInsensitive);
|
|
416
|
+
if (parsed) {
|
|
417
|
+
conditions.push(parsed);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
const parsed = parseSingleCondition(andGroup, caseInsensitive);
|
|
422
|
+
if (parsed) {
|
|
423
|
+
conditions.push(parsed);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (conditions.length === 0) {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
type: "INDEX_FILTER",
|
|
432
|
+
conditions,
|
|
433
|
+
logic
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function parseSingleCondition(condition, caseInsensitive = false) {
|
|
437
|
+
const cleanCondition = condition.startsWith("%") ? condition.slice(1) : condition;
|
|
438
|
+
let operator = null;
|
|
439
|
+
let operatorIndex = -1;
|
|
440
|
+
let operatorLength = 0;
|
|
441
|
+
const operators = [
|
|
442
|
+
["!*=", "!*="],
|
|
443
|
+
["!^=", "!^="],
|
|
444
|
+
["!$=", "!$="],
|
|
445
|
+
["!=", "!="],
|
|
446
|
+
["*=", "*="],
|
|
447
|
+
["^=", "^="],
|
|
448
|
+
["$=", "$="],
|
|
449
|
+
["=", "="]
|
|
450
|
+
];
|
|
451
|
+
for (const [op, opType] of operators) {
|
|
452
|
+
const index = cleanCondition.indexOf(op);
|
|
453
|
+
if (index !== -1) {
|
|
454
|
+
operator = opType;
|
|
455
|
+
operatorIndex = index;
|
|
456
|
+
operatorLength = op.length;
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (operator === null || operatorIndex === -1) {
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
const property = cleanCondition.slice(0, operatorIndex).trim();
|
|
464
|
+
const valueStr = cleanCondition.slice(operatorIndex + operatorLength).trim();
|
|
465
|
+
const values = [];
|
|
466
|
+
if (valueStr.includes(" | ")) {
|
|
467
|
+
const parts = valueStr.split(" | ");
|
|
468
|
+
for (const part of parts) {
|
|
469
|
+
const trimmed = part.trim();
|
|
470
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
471
|
+
values.push(value);
|
|
472
|
+
}
|
|
473
|
+
} else {
|
|
474
|
+
const trimmed = valueStr.trim();
|
|
475
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
476
|
+
values.push(value);
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
property,
|
|
480
|
+
operator,
|
|
481
|
+
values,
|
|
482
|
+
caseInsensitive
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
function separateFilterPatterns(patterns) {
|
|
486
|
+
const filterOnlyPatterns = [];
|
|
487
|
+
const combinedPatterns = [];
|
|
488
|
+
for (const pattern of patterns) {
|
|
489
|
+
const filterMatch = pattern.match(/^(.+\[[i%][^[\]]*\])\.(.+)$/);
|
|
490
|
+
if (filterMatch?.[1] && filterMatch[2]) {
|
|
491
|
+
const filterPart = filterMatch[1];
|
|
492
|
+
const fieldPart = filterMatch[2];
|
|
493
|
+
const baseArrayPath = filterPart.replace(/\[[i%][^[\]]*\]/, "[*]");
|
|
494
|
+
combinedPatterns.push({
|
|
495
|
+
filterPart,
|
|
496
|
+
fieldPart: `${baseArrayPath}.${fieldPart}`
|
|
497
|
+
});
|
|
498
|
+
} else {
|
|
499
|
+
filterOnlyPatterns.push(pattern);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return { filterOnlyPatterns, combinedPatterns };
|
|
503
|
+
}
|
|
205
504
|
function expandPatterns(pattern) {
|
|
206
505
|
function expandSingle(str) {
|
|
207
506
|
const start = str.indexOf("(");
|
|
@@ -245,7 +544,20 @@ function parsePattern(pattern) {
|
|
|
245
544
|
if (ch === "[") {
|
|
246
545
|
const end = pattern.indexOf("]", i + 1);
|
|
247
546
|
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
248
|
-
if (inside
|
|
547
|
+
if (inside.startsWith("%") || inside.startsWith("i%")) {
|
|
548
|
+
let filterContent;
|
|
549
|
+
if (inside.startsWith("i%")) {
|
|
550
|
+
filterContent = `i${inside.slice(2)}`;
|
|
551
|
+
} else if (inside.startsWith("%")) {
|
|
552
|
+
filterContent = inside.slice(1);
|
|
553
|
+
} else {
|
|
554
|
+
filterContent = inside;
|
|
555
|
+
}
|
|
556
|
+
const filterToken = parseFilterConditions(filterContent);
|
|
557
|
+
if (filterToken) {
|
|
558
|
+
tokens.push(filterToken);
|
|
559
|
+
}
|
|
560
|
+
} else if (inside === "*") {
|
|
249
561
|
tokens.push({ type: "INDEX_ANY" });
|
|
250
562
|
} else if (inside.includes("-")) {
|
|
251
563
|
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 };
|