@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
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
function filterObjectOrArrayKeys(objOrArray, options): Record<string, any> | Record<string, any>[];
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Defined in: [packages/utils/src/filterObjectOrArrayKeys.ts:
|
|
17
|
+
Defined in: [packages/utils/src/filterObjectOrArrayKeys.ts:58](https://github.com/lucasols/utils/blob/main/packages/utils/src/filterObjectOrArrayKeys.ts#L58)
|
|
18
18
|
|
|
19
19
|
Filters the keys of an object based on the provided patterns.
|
|
20
20
|
|
|
@@ -46,6 +46,19 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
|
|
|
46
46
|
- `'prop.test.(prop1|prop2|prop3)'` - Expands to `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3`
|
|
47
47
|
- `'components[*].(table_id|columns|filters[*].value)'` - Expands to `components[*].table_id`, `components[*].columns`, and `components[*].filters[*].value`
|
|
48
48
|
- `'(users|admins)[*].name'` - Expands to `users[*].name` and `admins[*].name`
|
|
49
|
+
- Array filtering by value:
|
|
50
|
+
- `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
51
|
+
- `'users[%name="John" | "Jane"]'` - Value-level OR using `|` for multiple values of same property
|
|
52
|
+
- `'users[%name="Alice" || %age=35]'` - Property-level OR using `||` for different properties
|
|
53
|
+
- `'users[%age=30 && %role="admin"]'` - Property-level AND using `&&` for different properties
|
|
54
|
+
- Note: Mixing `&&` and `||` in the same filter is not supported - use separate filter patterns instead
|
|
55
|
+
- `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
56
|
+
- `'users[%name*="oh"]'` - Contains operator (*=) - filters users where name contains "oh"
|
|
57
|
+
- `'users[%name^="Jo"]'` - Starts with operator (^=) - filters users where name starts with "Jo"
|
|
58
|
+
- `'users[%name$="hn"]'` - Ends with operator ($=) - filters users where name ends with "hn"
|
|
59
|
+
- `'users[%name!="John"]'` - Not equal operator (!=) - filters users where name is not "John"
|
|
60
|
+
- `'users[%name!*="admin"]'` - Not contains operator (!*=) - filters users where name doesn't contain "admin"
|
|
61
|
+
- `'users[i%name="john"]'` - Case-insensitive matching (i% prefix) - matches "John", "JOHN", "john", etc.
|
|
49
62
|
|
|
50
63
|
#### Parameters
|
|
51
64
|
|
|
@@ -77,6 +90,18 @@ Whether to reject empty objects in arrays (default: true).
|
|
|
77
90
|
|
|
78
91
|
The keys to reject.
|
|
79
92
|
|
|
93
|
+
###### sortKeys?
|
|
94
|
+
|
|
95
|
+
`false` \| `"asc"` \| `"simpleValuesFirst"` \| `"desc"` = `'simpleValuesFirst'`
|
|
96
|
+
|
|
97
|
+
Sort all keys by a specific order (optional, preserves original order when not specified).
|
|
98
|
+
|
|
99
|
+
###### sortPatterns?
|
|
100
|
+
|
|
101
|
+
`string`[]
|
|
102
|
+
|
|
103
|
+
Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
104
|
+
|
|
80
105
|
#### Returns
|
|
81
106
|
|
|
82
107
|
`Record`\<`string`, `any`\> \| `Record`\<`string`, `any`\>[]
|
package/docs/testUtils.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
function compactSnapshot(value, options): string;
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Defined in: [packages/utils/src/testUtils.ts:
|
|
17
|
+
Defined in: [packages/utils/src/testUtils.ts:363](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L363)
|
|
18
18
|
|
|
19
19
|
Produces a more compact and readable snapshot of a value using yaml.
|
|
20
20
|
By default booleans are shown as `✅` and `❌`, use `showBooleansAs` to disable/configure this.
|
|
@@ -45,6 +45,13 @@ Filtering patterns in `rejectKeys` and `filterKeys`:
|
|
|
45
45
|
- `'[4-*]'` - All items of the array from the fourth index to the end
|
|
46
46
|
- Expanding the patterns with parentheses:
|
|
47
47
|
- `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`, `prop.test.prop2`, and `prop.test.prop3.prop4`
|
|
48
|
+
- Array filtering by value:
|
|
49
|
+
- `'users[%name="John"]'` - Filters the `users` with the `name` property equal to `John`
|
|
50
|
+
- `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name` property equal to `John` or `Jane`
|
|
51
|
+
- `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by using `&&` and `||`, nesting logical operators is not supported yet
|
|
52
|
+
- `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
53
|
+
|
|
54
|
+
Check more supported patterns in [filterObjectOrArrayKeys](filterObjectOrArrayKeys.md#filterobjectorarraykeys) docs.
|
|
48
55
|
|
|
49
56
|
#### Parameters
|
|
50
57
|
|
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sortBy
|
|
3
|
+
} from "./chunk-SRVMMYSW.js";
|
|
4
|
+
import {
|
|
5
|
+
isPlainObject
|
|
6
|
+
} from "./chunk-JF2MDHOJ.js";
|
|
7
|
+
|
|
8
|
+
// src/filterObjectOrArrayKeys.ts
|
|
9
|
+
function filterObjectOrArrayKeys(objOrArray, {
|
|
10
|
+
filterKeys,
|
|
11
|
+
rejectKeys,
|
|
12
|
+
rejectEmptyObjectsInArray = true,
|
|
13
|
+
sortKeys = "simpleValuesFirst",
|
|
14
|
+
sortPatterns
|
|
15
|
+
}) {
|
|
16
|
+
function getNestedValue(obj, path) {
|
|
17
|
+
const parts = path.split(".");
|
|
18
|
+
let current = obj;
|
|
19
|
+
for (const part of parts) {
|
|
20
|
+
if (current == null || typeof current !== "object") {
|
|
21
|
+
return void 0;
|
|
22
|
+
}
|
|
23
|
+
current = current[part];
|
|
24
|
+
}
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
function evaluateCondition(item, condition) {
|
|
28
|
+
const value = getNestedValue(item, condition.property);
|
|
29
|
+
let valueStr = String(value);
|
|
30
|
+
if (condition.caseInsensitive) {
|
|
31
|
+
valueStr = valueStr.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
const processValue = (v) => condition.caseInsensitive ? v.toLowerCase() : v;
|
|
34
|
+
switch (condition.operator) {
|
|
35
|
+
case "=":
|
|
36
|
+
return condition.values.some((v) => valueStr === processValue(v));
|
|
37
|
+
case "!=":
|
|
38
|
+
return condition.values.every((v) => valueStr !== processValue(v));
|
|
39
|
+
case "*=":
|
|
40
|
+
return condition.values.some((v) => valueStr.includes(processValue(v)));
|
|
41
|
+
case "!*=":
|
|
42
|
+
return condition.values.every(
|
|
43
|
+
(v) => !valueStr.includes(processValue(v))
|
|
44
|
+
);
|
|
45
|
+
case "^=":
|
|
46
|
+
return condition.values.some(
|
|
47
|
+
(v) => valueStr.startsWith(processValue(v))
|
|
48
|
+
);
|
|
49
|
+
case "!^=":
|
|
50
|
+
return condition.values.every(
|
|
51
|
+
(v) => !valueStr.startsWith(processValue(v))
|
|
52
|
+
);
|
|
53
|
+
case "$=":
|
|
54
|
+
return condition.values.some((v) => valueStr.endsWith(processValue(v)));
|
|
55
|
+
case "!$=":
|
|
56
|
+
return condition.values.every(
|
|
57
|
+
(v) => !valueStr.endsWith(processValue(v))
|
|
58
|
+
);
|
|
59
|
+
default:
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
|
|
64
|
+
const filterPatternsRaw = toArray(filterKeys);
|
|
65
|
+
const rejectPatternsRaw = toArray(rejectKeys);
|
|
66
|
+
const hasFilters = filterPatternsRaw.length > 0;
|
|
67
|
+
const hasRejects = rejectPatternsRaw.length > 0;
|
|
68
|
+
const expandedFilterPatterns = filterPatternsRaw.flatMap(expandPatterns);
|
|
69
|
+
const expandedRejectPatterns = rejectPatternsRaw.flatMap(expandPatterns);
|
|
70
|
+
const { filterOnlyPatterns, combinedPatterns } = separateFilterPatterns(
|
|
71
|
+
expandedFilterPatterns
|
|
72
|
+
);
|
|
73
|
+
const filterPatterns = filterOnlyPatterns.map(parsePattern);
|
|
74
|
+
const rejectPatterns = expandedRejectPatterns.map(parsePattern);
|
|
75
|
+
const sortPatternsRaw = toArray(sortPatterns);
|
|
76
|
+
const expandedSortPatterns = sortPatternsRaw.flatMap(expandPatterns);
|
|
77
|
+
const sortPatternsParsed = expandedSortPatterns.map(parsePattern);
|
|
78
|
+
let dataToProcess = objOrArray;
|
|
79
|
+
if (combinedPatterns.length > 0) {
|
|
80
|
+
const groupedByFilter = /* @__PURE__ */ new Map();
|
|
81
|
+
for (const { filterPart, fieldPart } of combinedPatterns) {
|
|
82
|
+
if (!groupedByFilter.has(filterPart)) {
|
|
83
|
+
groupedByFilter.set(filterPart, []);
|
|
84
|
+
}
|
|
85
|
+
groupedByFilter.get(filterPart).push(fieldPart);
|
|
86
|
+
}
|
|
87
|
+
const combinedResult = Array.isArray(objOrArray) ? [] : {};
|
|
88
|
+
for (const [filterPart, fieldParts] of groupedByFilter) {
|
|
89
|
+
const filteredResult = filterObjectOrArrayKeys(objOrArray, {
|
|
90
|
+
filterKeys: [filterPart],
|
|
91
|
+
rejectKeys,
|
|
92
|
+
rejectEmptyObjectsInArray
|
|
93
|
+
});
|
|
94
|
+
const fieldSelectedResult = filterObjectOrArrayKeys(filteredResult, {
|
|
95
|
+
filterKeys: fieldParts,
|
|
96
|
+
rejectEmptyObjectsInArray
|
|
97
|
+
});
|
|
98
|
+
if (Array.isArray(combinedResult) && Array.isArray(fieldSelectedResult)) {
|
|
99
|
+
combinedResult.push(...fieldSelectedResult);
|
|
100
|
+
} else if (!Array.isArray(combinedResult) && !Array.isArray(fieldSelectedResult)) {
|
|
101
|
+
Object.assign(combinedResult, fieldSelectedResult);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (filterOnlyPatterns.length === 0) {
|
|
105
|
+
return combinedResult;
|
|
106
|
+
}
|
|
107
|
+
dataToProcess = combinedResult;
|
|
108
|
+
}
|
|
109
|
+
function matchPath(path, pattern, value) {
|
|
110
|
+
function rec(pi, pti) {
|
|
111
|
+
if (pti >= pattern.length) return pi === path.length;
|
|
112
|
+
const pt = pattern[pti];
|
|
113
|
+
if (pt.type === "WILDCARD_ANY") {
|
|
114
|
+
if (rec(pi, pti + 1)) return true;
|
|
115
|
+
if (pi < path.length) return rec(pi + 1, pti);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (pt.type === "WILDCARD_ONE") {
|
|
119
|
+
let j = pi;
|
|
120
|
+
let sawKey = false;
|
|
121
|
+
while (j < path.length) {
|
|
122
|
+
if (path[j].type === "KEY") sawKey = true;
|
|
123
|
+
if (sawKey && rec(j, pti + 1)) return true;
|
|
124
|
+
j += 1;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (pi >= path.length) return false;
|
|
129
|
+
const ct = path[pi];
|
|
130
|
+
switch (pt.type) {
|
|
131
|
+
case "KEY":
|
|
132
|
+
if (ct.type === "KEY" && ct.name === pt.name)
|
|
133
|
+
return rec(pi + 1, pti + 1);
|
|
134
|
+
if (ct.type === "INDEX") return rec(pi + 1, pti);
|
|
135
|
+
return false;
|
|
136
|
+
case "INDEX":
|
|
137
|
+
if (ct.type === "INDEX" && ct.index === pt.index)
|
|
138
|
+
return rec(pi + 1, pti + 1);
|
|
139
|
+
return false;
|
|
140
|
+
case "INDEX_ANY":
|
|
141
|
+
if (ct.type === "INDEX") return rec(pi + 1, pti + 1);
|
|
142
|
+
return false;
|
|
143
|
+
case "INDEX_RANGE":
|
|
144
|
+
if (ct.type === "INDEX") {
|
|
145
|
+
const okLower = ct.index >= pt.start;
|
|
146
|
+
const okUpper = pt.end === null ? true : ct.index <= pt.end;
|
|
147
|
+
if (okLower && okUpper) return rec(pi + 1, pti + 1);
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
case "INDEX_FILTER":
|
|
151
|
+
if (ct.type === "INDEX" && value !== void 0) {
|
|
152
|
+
const results = pt.conditions.map(
|
|
153
|
+
(cond) => evaluateCondition(value, cond)
|
|
154
|
+
);
|
|
155
|
+
const matches = pt.logic === "AND" ? results.every((r) => r) : results.some((r) => r);
|
|
156
|
+
if (matches) return rec(pi + 1, pti + 1);
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return rec(0, 0);
|
|
162
|
+
}
|
|
163
|
+
const matchesAnyFilter = (path, value) => filterPatterns.some((p) => matchPath(path, p, value));
|
|
164
|
+
const matchesAnyReject = (path, value) => rejectPatterns.some((p) => matchPath(path, p, value));
|
|
165
|
+
function getSortPriority(path) {
|
|
166
|
+
for (let i = 0; i < sortPatternsParsed.length; i++) {
|
|
167
|
+
if (matchPath(path, sortPatternsParsed[i])) {
|
|
168
|
+
return i;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return sortPatternsParsed.length;
|
|
172
|
+
}
|
|
173
|
+
function applySortKeys(keys, obj, sortOrder) {
|
|
174
|
+
if (sortOrder === "asc") {
|
|
175
|
+
return [...keys].sort();
|
|
176
|
+
}
|
|
177
|
+
if (sortOrder === "desc") {
|
|
178
|
+
return [...keys].sort().reverse();
|
|
179
|
+
}
|
|
180
|
+
return sortBy(
|
|
181
|
+
sortBy(keys, (k) => k),
|
|
182
|
+
(key) => {
|
|
183
|
+
const value = obj[key];
|
|
184
|
+
if (value !== void 0 && value !== null) {
|
|
185
|
+
if (Array.isArray(value) && value.length === 0) return 0;
|
|
186
|
+
if (isPlainObject(value)) {
|
|
187
|
+
const objLength = Object.keys(value).length;
|
|
188
|
+
return 1.99 + objLength * -1e-3;
|
|
189
|
+
}
|
|
190
|
+
if (Array.isArray(value)) {
|
|
191
|
+
const allItemsArePrimitives = value.every(
|
|
192
|
+
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0
|
|
193
|
+
);
|
|
194
|
+
if (allItemsArePrimitives) {
|
|
195
|
+
return 1.9 + value.length * -1e-3;
|
|
196
|
+
} else {
|
|
197
|
+
return 1.5 + value.length * -0.01;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (typeof value === "boolean") return 4;
|
|
201
|
+
if (typeof value === "number") return 3.5;
|
|
202
|
+
if (typeof value === "string" && value.length < 20) return 3;
|
|
203
|
+
return 2;
|
|
204
|
+
}
|
|
205
|
+
return 0;
|
|
206
|
+
},
|
|
207
|
+
"desc"
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
function sortKeysWithPatterns(keys_, obj, currentPath) {
|
|
211
|
+
if (!sortKeys && sortPatternsParsed.length === 0) {
|
|
212
|
+
return keys_;
|
|
213
|
+
}
|
|
214
|
+
let keysToSort = keys_;
|
|
215
|
+
if (sortKeys) {
|
|
216
|
+
keysToSort = applySortKeys(keysToSort, obj, sortKeys);
|
|
217
|
+
}
|
|
218
|
+
const sortedKeys = [...keysToSort].sort((a, b) => {
|
|
219
|
+
const pathA = currentPath.concat({ type: "KEY", name: a });
|
|
220
|
+
const pathB = currentPath.concat({ type: "KEY", name: b });
|
|
221
|
+
const priorityA = getSortPriority(pathA);
|
|
222
|
+
const priorityB = getSortPriority(pathB);
|
|
223
|
+
if (priorityA !== priorityB) {
|
|
224
|
+
return priorityA - priorityB;
|
|
225
|
+
}
|
|
226
|
+
if (sortKeys === "desc") {
|
|
227
|
+
return b.localeCompare(a);
|
|
228
|
+
}
|
|
229
|
+
if (sortKeys === "asc") {
|
|
230
|
+
return a.localeCompare(b);
|
|
231
|
+
}
|
|
232
|
+
return 0;
|
|
233
|
+
});
|
|
234
|
+
return sortedKeys;
|
|
235
|
+
}
|
|
236
|
+
const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
|
|
237
|
+
if (Array.isArray(value)) {
|
|
238
|
+
if (stack2.has(value)) {
|
|
239
|
+
throw new TypeError("Circular references are not supported");
|
|
240
|
+
}
|
|
241
|
+
stack2.add(value);
|
|
242
|
+
const out = [];
|
|
243
|
+
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
244
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
245
|
+
const childPath = path.concat({ type: "INDEX", index });
|
|
246
|
+
const child = value[index];
|
|
247
|
+
if (hasRejects && matchesAnyReject(childPath, child)) continue;
|
|
248
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath, child) : true;
|
|
249
|
+
const childAllowed = includeAllChildren || directInclude;
|
|
250
|
+
if (isPlainObject(child) || Array.isArray(child)) {
|
|
251
|
+
const builtChild = build(
|
|
252
|
+
child,
|
|
253
|
+
childPath,
|
|
254
|
+
childAllowed,
|
|
255
|
+
stack2,
|
|
256
|
+
false,
|
|
257
|
+
true
|
|
258
|
+
);
|
|
259
|
+
if (builtChild !== void 0) {
|
|
260
|
+
out.push(builtChild);
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
if (childAllowed) {
|
|
264
|
+
out.push(child);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
stack2.delete(value);
|
|
269
|
+
const filteredOut = rejectEmptyObjectsInArray ? out.filter(
|
|
270
|
+
(item) => !(isPlainObject(item) && Object.keys(item).length === 0)
|
|
271
|
+
) : out;
|
|
272
|
+
if (filteredOut.length === 0 && !allowedByFilter && !isRoot)
|
|
273
|
+
return void 0;
|
|
274
|
+
return filteredOut;
|
|
275
|
+
}
|
|
276
|
+
if (isPlainObject(value)) {
|
|
277
|
+
if (stack2.has(value)) {
|
|
278
|
+
throw new TypeError("Circular references are not supported");
|
|
279
|
+
}
|
|
280
|
+
stack2.add(value);
|
|
281
|
+
const result = {};
|
|
282
|
+
const includeAllChildren = allowedByFilter || !hasFilters;
|
|
283
|
+
const sortedKeys = sortKeysWithPatterns(Object.keys(value), value, path);
|
|
284
|
+
for (const key of sortedKeys) {
|
|
285
|
+
const childPath = path.concat({ type: "KEY", name: key });
|
|
286
|
+
if (hasRejects && matchesAnyReject(childPath)) continue;
|
|
287
|
+
const val = value[key];
|
|
288
|
+
const directInclude = hasFilters ? matchesAnyFilter(childPath) : true;
|
|
289
|
+
const childAllowed = includeAllChildren || directInclude;
|
|
290
|
+
if (isPlainObject(val) || Array.isArray(val)) {
|
|
291
|
+
const builtChild = build(
|
|
292
|
+
val,
|
|
293
|
+
childPath,
|
|
294
|
+
childAllowed,
|
|
295
|
+
stack2,
|
|
296
|
+
false,
|
|
297
|
+
false
|
|
298
|
+
);
|
|
299
|
+
if (builtChild === void 0) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (Array.isArray(builtChild) && builtChild.length === 0 && !childAllowed) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (isPlainObject(builtChild) && Object.keys(builtChild).length === 0 && !childAllowed) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
result[key] = builtChild;
|
|
309
|
+
} else {
|
|
310
|
+
if (childAllowed) {
|
|
311
|
+
result[key] = val;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
stack2.delete(value);
|
|
316
|
+
if (Object.keys(result).length === 0 && !allowedByFilter && !isRoot) {
|
|
317
|
+
if (parentIsArray && !rejectEmptyObjectsInArray) {
|
|
318
|
+
return {};
|
|
319
|
+
}
|
|
320
|
+
return void 0;
|
|
321
|
+
}
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
return allowedByFilter || !hasFilters ? value : void 0;
|
|
325
|
+
};
|
|
326
|
+
const startPath = [];
|
|
327
|
+
const initialAllowed = !hasFilters;
|
|
328
|
+
const stack = /* @__PURE__ */ new WeakSet();
|
|
329
|
+
const built = build(
|
|
330
|
+
dataToProcess,
|
|
331
|
+
startPath,
|
|
332
|
+
initialAllowed,
|
|
333
|
+
stack,
|
|
334
|
+
true,
|
|
335
|
+
false
|
|
336
|
+
);
|
|
337
|
+
if (built === void 0) return Array.isArray(dataToProcess) ? [] : {};
|
|
338
|
+
return built;
|
|
339
|
+
}
|
|
340
|
+
function parseFilterConditions(filterContent) {
|
|
341
|
+
const conditions = [];
|
|
342
|
+
let logic = "AND";
|
|
343
|
+
const caseInsensitive = filterContent.startsWith("i");
|
|
344
|
+
const content = caseInsensitive ? filterContent.slice(1) : filterContent;
|
|
345
|
+
const hasAnd = content.includes("&&");
|
|
346
|
+
const hasOr = content.includes(" || ");
|
|
347
|
+
if (hasAnd && hasOr) {
|
|
348
|
+
throw new Error(
|
|
349
|
+
"Mixing && and || operators in the same filter is not supported. Use separate filter patterns instead."
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
const andGroups = content.split("&&").map((s) => s.trim());
|
|
353
|
+
for (const andGroup of andGroups) {
|
|
354
|
+
if (andGroup.includes(" || ")) {
|
|
355
|
+
logic = "OR";
|
|
356
|
+
const orConditions = andGroup.split(" || ").map((s) => s.trim());
|
|
357
|
+
for (const orCondition of orConditions) {
|
|
358
|
+
const parsed = parseSingleCondition(orCondition, caseInsensitive);
|
|
359
|
+
if (parsed) {
|
|
360
|
+
conditions.push(parsed);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
} else {
|
|
364
|
+
const parsed = parseSingleCondition(andGroup, caseInsensitive);
|
|
365
|
+
if (parsed) {
|
|
366
|
+
conditions.push(parsed);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (conditions.length === 0) {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
type: "INDEX_FILTER",
|
|
375
|
+
conditions,
|
|
376
|
+
logic
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function parseSingleCondition(condition, caseInsensitive = false) {
|
|
380
|
+
const cleanCondition = condition.startsWith("%") ? condition.slice(1) : condition;
|
|
381
|
+
let operator = null;
|
|
382
|
+
let operatorIndex = -1;
|
|
383
|
+
let operatorLength = 0;
|
|
384
|
+
const operators = [
|
|
385
|
+
["!*=", "!*="],
|
|
386
|
+
["!^=", "!^="],
|
|
387
|
+
["!$=", "!$="],
|
|
388
|
+
["!=", "!="],
|
|
389
|
+
["*=", "*="],
|
|
390
|
+
["^=", "^="],
|
|
391
|
+
["$=", "$="],
|
|
392
|
+
["=", "="]
|
|
393
|
+
];
|
|
394
|
+
for (const [op, opType] of operators) {
|
|
395
|
+
const index = cleanCondition.indexOf(op);
|
|
396
|
+
if (index !== -1) {
|
|
397
|
+
operator = opType;
|
|
398
|
+
operatorIndex = index;
|
|
399
|
+
operatorLength = op.length;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (operator === null || operatorIndex === -1) {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
const property = cleanCondition.slice(0, operatorIndex).trim();
|
|
407
|
+
const valueStr = cleanCondition.slice(operatorIndex + operatorLength).trim();
|
|
408
|
+
const values = [];
|
|
409
|
+
if (valueStr.includes(" | ")) {
|
|
410
|
+
const parts = valueStr.split(" | ");
|
|
411
|
+
for (const part of parts) {
|
|
412
|
+
const trimmed = part.trim();
|
|
413
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
414
|
+
values.push(value);
|
|
415
|
+
}
|
|
416
|
+
} else {
|
|
417
|
+
const trimmed = valueStr.trim();
|
|
418
|
+
const value = trimmed.startsWith('"') && trimmed.endsWith('"') ? trimmed.slice(1, -1) : trimmed;
|
|
419
|
+
values.push(value);
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
property,
|
|
423
|
+
operator,
|
|
424
|
+
values,
|
|
425
|
+
caseInsensitive
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function separateFilterPatterns(patterns) {
|
|
429
|
+
const filterOnlyPatterns = [];
|
|
430
|
+
const combinedPatterns = [];
|
|
431
|
+
for (const pattern of patterns) {
|
|
432
|
+
const filterMatch = pattern.match(/^(.+\[[i%][^[\]]*\])\.(.+)$/);
|
|
433
|
+
if (filterMatch?.[1] && filterMatch[2]) {
|
|
434
|
+
const filterPart = filterMatch[1];
|
|
435
|
+
const fieldPart = filterMatch[2];
|
|
436
|
+
const baseArrayPath = filterPart.replace(/\[[i%][^[\]]*\]/, "[*]");
|
|
437
|
+
combinedPatterns.push({
|
|
438
|
+
filterPart,
|
|
439
|
+
fieldPart: `${baseArrayPath}.${fieldPart}`
|
|
440
|
+
});
|
|
441
|
+
} else {
|
|
442
|
+
filterOnlyPatterns.push(pattern);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return { filterOnlyPatterns, combinedPatterns };
|
|
446
|
+
}
|
|
447
|
+
function expandPatterns(pattern) {
|
|
448
|
+
function expandSingle(str) {
|
|
449
|
+
const start = str.indexOf("(");
|
|
450
|
+
if (start === -1) {
|
|
451
|
+
return [str];
|
|
452
|
+
}
|
|
453
|
+
const end = str.indexOf(")", start);
|
|
454
|
+
if (end === -1) {
|
|
455
|
+
return [str];
|
|
456
|
+
}
|
|
457
|
+
const before = str.slice(0, start);
|
|
458
|
+
const inside = str.slice(start + 1, end);
|
|
459
|
+
const after = str.slice(end + 1);
|
|
460
|
+
if (!inside.includes("|")) {
|
|
461
|
+
return expandSingle(before + inside + after);
|
|
462
|
+
}
|
|
463
|
+
const options = inside.split("|").filter((option) => option.trim().length > 0);
|
|
464
|
+
const results = [];
|
|
465
|
+
for (const option of options) {
|
|
466
|
+
const newStr = before + option + after;
|
|
467
|
+
results.push(...expandSingle(newStr));
|
|
468
|
+
}
|
|
469
|
+
return results;
|
|
470
|
+
}
|
|
471
|
+
return expandSingle(pattern);
|
|
472
|
+
}
|
|
473
|
+
function parsePattern(pattern) {
|
|
474
|
+
const tokens = [];
|
|
475
|
+
let i = 0;
|
|
476
|
+
const n = pattern.length;
|
|
477
|
+
const pushKey = (name) => {
|
|
478
|
+
if (name.length === 0) return;
|
|
479
|
+
tokens.push({ type: "KEY", name });
|
|
480
|
+
};
|
|
481
|
+
while (i < n) {
|
|
482
|
+
const ch = pattern[i];
|
|
483
|
+
if (ch === ".") {
|
|
484
|
+
i += 1;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (ch === "[") {
|
|
488
|
+
const end = pattern.indexOf("]", i + 1);
|
|
489
|
+
const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
|
|
490
|
+
if (inside.startsWith("%") || inside.startsWith("i%")) {
|
|
491
|
+
let filterContent;
|
|
492
|
+
if (inside.startsWith("i%")) {
|
|
493
|
+
filterContent = `i${inside.slice(2)}`;
|
|
494
|
+
} else if (inside.startsWith("%")) {
|
|
495
|
+
filterContent = inside.slice(1);
|
|
496
|
+
} else {
|
|
497
|
+
filterContent = inside;
|
|
498
|
+
}
|
|
499
|
+
const filterToken = parseFilterConditions(filterContent);
|
|
500
|
+
if (filterToken) {
|
|
501
|
+
tokens.push(filterToken);
|
|
502
|
+
}
|
|
503
|
+
} else if (inside === "*") {
|
|
504
|
+
tokens.push({ type: "INDEX_ANY" });
|
|
505
|
+
} else if (inside.includes("-")) {
|
|
506
|
+
const parts = inside.split("-");
|
|
507
|
+
const startStr = parts[0] ?? "";
|
|
508
|
+
const endStr = parts[1] ?? "";
|
|
509
|
+
const start = parseInt(startStr, 10);
|
|
510
|
+
const endNum = endStr === "*" ? null : parseInt(endStr, 10);
|
|
511
|
+
tokens.push({
|
|
512
|
+
type: "INDEX_RANGE",
|
|
513
|
+
start,
|
|
514
|
+
end: endNum === null || Number.isFinite(endNum) ? endNum : null
|
|
515
|
+
});
|
|
516
|
+
} else if (inside.length > 0) {
|
|
517
|
+
const idx = parseInt(inside, 10);
|
|
518
|
+
tokens.push({ type: "INDEX", index: idx });
|
|
519
|
+
}
|
|
520
|
+
i = end === -1 ? n : end + 1;
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
if (ch === "*") {
|
|
524
|
+
if (pattern[i + 1] === "*") {
|
|
525
|
+
tokens.push({ type: "WILDCARD_ANY" });
|
|
526
|
+
i += 2;
|
|
527
|
+
let j2 = i;
|
|
528
|
+
while (j2 < n) {
|
|
529
|
+
const c = pattern[j2];
|
|
530
|
+
if (c === "." || c === "[") break;
|
|
531
|
+
j2 += 1;
|
|
532
|
+
}
|
|
533
|
+
if (j2 > i) {
|
|
534
|
+
pushKey(pattern.slice(i, j2));
|
|
535
|
+
i = j2;
|
|
536
|
+
}
|
|
537
|
+
continue;
|
|
538
|
+
} else {
|
|
539
|
+
tokens.push({ type: "WILDCARD_ONE" });
|
|
540
|
+
i += 1;
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
let j = i;
|
|
545
|
+
while (j < n) {
|
|
546
|
+
const c = pattern[j];
|
|
547
|
+
if (c === "." || c === "[") break;
|
|
548
|
+
j += 1;
|
|
549
|
+
}
|
|
550
|
+
pushKey(pattern.slice(i, j));
|
|
551
|
+
i = j;
|
|
552
|
+
}
|
|
553
|
+
return tokens;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export {
|
|
557
|
+
filterObjectOrArrayKeys
|
|
558
|
+
};
|