@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
|
@@ -37,20 +37,134 @@ 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);
|
|
49
124
|
const hasFilters = filterPatternsRaw.length > 0;
|
|
50
125
|
const hasRejects = rejectPatternsRaw.length > 0;
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
126
|
+
const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
|
|
127
|
+
const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
|
|
128
|
+
const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
|
|
129
|
+
expandedFilterPatterns
|
|
130
|
+
);
|
|
131
|
+
const filterPatterns = filterOnlyPatterns.map(parsePattern);
|
|
132
|
+
const rejectPatterns = expandedRejectPatterns.map(parsePattern);
|
|
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) {
|
|
54
168
|
function rec(pi, pti) {
|
|
55
169
|
if (pti >= pattern.length) return pi === path.length;
|
|
56
170
|
const pt = pattern[pti];
|
|
@@ -77,11 +191,6 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
77
191
|
return rec(pi + 1, pti + 1);
|
|
78
192
|
if (ct.type === "INDEX") return rec(pi + 1, pti);
|
|
79
193
|
return false;
|
|
80
|
-
case "MULTI_KEY":
|
|
81
|
-
if (ct.type === "KEY" && pt.names.includes(ct.name))
|
|
82
|
-
return rec(pi + 1, pti + 1);
|
|
83
|
-
if (ct.type === "INDEX") return rec(pi + 1, pti);
|
|
84
|
-
return false;
|
|
85
194
|
case "INDEX":
|
|
86
195
|
if (ct.type === "INDEX" && ct.index === pt.index)
|
|
87
196
|
return rec(pi + 1, pti + 1);
|
|
@@ -96,12 +205,91 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
96
205
|
if (okLower && okUpper) return rec(pi + 1, pti + 1);
|
|
97
206
|
}
|
|
98
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;
|
|
99
217
|
}
|
|
100
218
|
}
|
|
101
219
|
return rec(0, 0);
|
|
102
220
|
}
|
|
103
|
-
const matchesAnyFilter = (path) => filterPatterns.some((p) => matchPath(path, p));
|
|
104
|
-
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
|
+
}
|
|
105
293
|
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
106
294
|
if (Array.isArray(value)) {
|
|
107
295
|
if (stack2.has(value)) {
|
|
@@ -112,9 +300,9 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
112
300
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
113
301
|
for (let index = 0; index < value.length; index += 1) {
|
|
114
302
|
const childPath = path.concat({ type: "INDEX", index });
|
|
115
|
-
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
116
303
|
const child = value[index];
|
|
117
|
-
|
|
304
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
305
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
118
306
|
const childAllowed = includeAllChildren || directInclude;
|
|
119
307
|
if (isPlainObject(child) || Array.isArray(child)) {
|
|
120
308
|
const builtChild = build(
|
|
@@ -149,7 +337,8 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
149
337
|
stack2.add(value);
|
|
150
338
|
const result = {};
|
|
151
339
|
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
152
|
-
|
|
340
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
341
|
+
for (const key of sortedKeys) {
|
|
153
342
|
const childPath = path.concat({ type: "KEY", name: key });
|
|
154
343
|
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
155
344
|
const val = value[key];
|
|
@@ -195,16 +384,149 @@ function filterObjectOrArrayKeys(objOrArray, {
|
|
|
195
384
|
const initialAllowed = !hasFilters;
|
|
196
385
|
const stack = /* @__PURE__ */ new WeakSet();
|
|
197
386
|
const built = build(
|
|
198
|
-
|
|
387
|
+
dataToProcess,
|
|
199
388
|
startPath,
|
|
200
389
|
initialAllowed,
|
|
201
390
|
stack,
|
|
202
391
|
true,
|
|
203
392
|
false
|
|
204
393
|
);
|
|
205
|
-
if (built === void 0) return Array.isArray(
|
|
394
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
206
395
|
return built;
|
|
207
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
|
+
}
|
|
504
|
+
function expandPatterns(pattern) {
|
|
505
|
+
function expandSingle(str) {
|
|
506
|
+
const start = str.indexOf("(");
|
|
507
|
+
if (start === -1) {
|
|
508
|
+
return [str];
|
|
509
|
+
}
|
|
510
|
+
const end = str.indexOf(")", start);
|
|
511
|
+
if (end === -1) {
|
|
512
|
+
return [str];
|
|
513
|
+
}
|
|
514
|
+
const before = str.slice(0, start);
|
|
515
|
+
const inside = str.slice(start + 1, end);
|
|
516
|
+
const after = str.slice(end + 1);
|
|
517
|
+
if (!inside.includes("|")) {
|
|
518
|
+
return expandSingle(before + inside + after);
|
|
519
|
+
}
|
|
520
|
+
const options = inside.split("|").filter((option) => option.trim().length > 0);
|
|
521
|
+
const results = [];
|
|
522
|
+
for (const option of options) {
|
|
523
|
+
const newStr = before + option + after;
|
|
524
|
+
results.push(...expandSingle(newStr));
|
|
525
|
+
}
|
|
526
|
+
return results;
|
|
527
|
+
}
|
|
528
|
+
return expandSingle(pattern);
|
|
529
|
+
}
|
|
208
530
|
function parsePattern(pattern) {
|
|
209
531
|
const tokens = [];
|
|
210
532
|
let i = 0;
|
|
@@ -222,7 +544,20 @@ function parsePattern(pattern) {
|
|
|
222
544
|
if (ch === "[") {
|
|
223
545
|
const end = pattern.indexOf("]", i + 1);
|
|
224
546
|
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
225
|
-
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 === "*") {
|
|
226
561
|
tokens.push({ type: "INDEX_ANY" });
|
|
227
562
|
} else if (inside.includes("-")) {
|
|
228
563
|
const parts = inside.split("-");
|
|
@@ -242,18 +577,6 @@ function parsePattern(pattern) {
|
|
|
242
577
|
i = end === -1 ? n : end + 1;
|
|
243
578
|
continue;
|
|
244
579
|
}
|
|
245
|
-
if (ch === "(") {
|
|
246
|
-
const end = pattern.indexOf(")", i + 1);
|
|
247
|
-
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
248
|
-
if (inside.includes("|") && inside.trim().length > 0) {
|
|
249
|
-
const names = inside.split("|").filter((name) => name.length > 0);
|
|
250
|
-
if (names.length > 0) {
|
|
251
|
-
tokens.push({ type: "MULTI_KEY", names });
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
i = end === -1 ? n : end + 1;
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
580
|
if (ch === "*") {
|
|
258
581
|
if (pattern[i + 1] === "*") {
|
|
259
582
|
tokens.push({ type: "WILDCARD_ANY" });
|
|
@@ -25,20 +25,39 @@
|
|
|
25
25
|
* - `'[*]**nested'` - all `nested` props of all items of the array
|
|
26
26
|
* - `'[0-2]'` - The first three items of the array
|
|
27
27
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
28
|
-
* -
|
|
29
|
-
* - `'prop.test.(prop1|prop2|prop3)'` -
|
|
28
|
+
* - Pattern expansion with parentheses:
|
|
29
|
+
* - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
|
|
30
|
+
* - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
|
|
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.
|
|
30
45
|
*
|
|
31
46
|
* @param objOrArray - The object or array to filter.
|
|
32
47
|
* @param options - The options for the filter.
|
|
33
48
|
* @param options.filterKeys - The keys to filter.
|
|
34
49
|
* @param options.rejectKeys - The keys to reject.
|
|
35
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.
|
|
36
53
|
* @returns The filtered object or array.
|
|
37
54
|
*/
|
|
38
|
-
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, }: {
|
|
39
56
|
filterKeys?: string[] | string;
|
|
40
57
|
rejectKeys?: string[] | string;
|
|
41
58
|
rejectEmptyObjectsInArray?: boolean;
|
|
59
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
60
|
+
sortPatterns?: string[];
|
|
42
61
|
}): Record<string, any> | Record<string, any>[];
|
|
43
62
|
|
|
44
63
|
export { filterObjectOrArrayKeys };
|
|
@@ -25,20 +25,39 @@
|
|
|
25
25
|
* - `'[*]**nested'` - all `nested` props of all items of the array
|
|
26
26
|
* - `'[0-2]'` - The first three items of the array
|
|
27
27
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
28
|
-
* -
|
|
29
|
-
* - `'prop.test.(prop1|prop2|prop3)'` -
|
|
28
|
+
* - Pattern expansion with parentheses:
|
|
29
|
+
* - `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
|
|
30
|
+
* - `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
|
|
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.
|
|
30
45
|
*
|
|
31
46
|
* @param objOrArray - The object or array to filter.
|
|
32
47
|
* @param options - The options for the filter.
|
|
33
48
|
* @param options.filterKeys - The keys to filter.
|
|
34
49
|
* @param options.rejectKeys - The keys to reject.
|
|
35
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.
|
|
36
53
|
* @returns The filtered object or array.
|
|
37
54
|
*/
|
|
38
|
-
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, }: {
|
|
39
56
|
filterKeys?: string[] | string;
|
|
40
57
|
rejectKeys?: string[] | string;
|
|
41
58
|
rejectEmptyObjectsInArray?: boolean;
|
|
59
|
+
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
|
60
|
+
sortPatterns?: string[];
|
|
42
61
|
}): Record<string, any> | Record<string, any>[];
|
|
43
62
|
|
|
44
63
|
export { filterObjectOrArrayKeys };
|