@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.
@@ -1,257 +0,0 @@
1
- import {
2
- isPlainObject
3
- } from "./chunk-JF2MDHOJ.js";
4
-
5
- // src/filterObjectOrArrayKeys.ts
6
- function filterObjectOrArrayKeys(objOrArray, {
7
- filterKeys,
8
- rejectKeys,
9
- rejectEmptyObjectsInArray = true
10
- }) {
11
- const toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
12
- const filterPatternsRaw = toArray(filterKeys);
13
- const rejectPatternsRaw = toArray(rejectKeys);
14
- const hasFilters = filterPatternsRaw.length > 0;
15
- const hasRejects = rejectPatternsRaw.length > 0;
16
- const filterPatterns = filterPatternsRaw.map(parsePattern);
17
- const rejectPatterns = rejectPatternsRaw.map(parsePattern);
18
- function matchPath(path, pattern) {
19
- function rec(pi, pti) {
20
- if (pti >= pattern.length) return pi === path.length;
21
- const pt = pattern[pti];
22
- if (pt.type === "WILDCARD_ANY") {
23
- if (rec(pi, pti + 1)) return true;
24
- if (pi < path.length) return rec(pi + 1, pti);
25
- return false;
26
- }
27
- if (pt.type === "WILDCARD_ONE") {
28
- let j = pi;
29
- let sawKey = false;
30
- while (j < path.length) {
31
- if (path[j].type === "KEY") sawKey = true;
32
- if (sawKey && rec(j, pti + 1)) return true;
33
- j += 1;
34
- }
35
- return false;
36
- }
37
- if (pi >= path.length) return false;
38
- const ct = path[pi];
39
- switch (pt.type) {
40
- case "KEY":
41
- if (ct.type === "KEY" && ct.name === pt.name)
42
- return rec(pi + 1, pti + 1);
43
- if (ct.type === "INDEX") return rec(pi + 1, pti);
44
- return false;
45
- case "MULTI_KEY":
46
- if (ct.type === "KEY" && pt.names.includes(ct.name))
47
- return rec(pi + 1, pti + 1);
48
- if (ct.type === "INDEX") return rec(pi + 1, pti);
49
- return false;
50
- case "INDEX":
51
- if (ct.type === "INDEX" && ct.index === pt.index)
52
- return rec(pi + 1, pti + 1);
53
- return false;
54
- case "INDEX_ANY":
55
- if (ct.type === "INDEX") return rec(pi + 1, pti + 1);
56
- return false;
57
- case "INDEX_RANGE":
58
- if (ct.type === "INDEX") {
59
- const okLower = ct.index >= pt.start;
60
- const okUpper = pt.end === null ? true : ct.index <= pt.end;
61
- if (okLower && okUpper) return rec(pi + 1, pti + 1);
62
- }
63
- return false;
64
- }
65
- }
66
- return rec(0, 0);
67
- }
68
- const matchesAnyFilter = (path) => filterPatterns.some((p) => matchPath(path, p));
69
- const matchesAnyReject = (path) => rejectPatterns.some((p) => matchPath(path, p));
70
- const build = (value, path, allowedByFilter, stack2, isRoot, parentIsArray) => {
71
- if (Array.isArray(value)) {
72
- if (stack2.has(value)) {
73
- throw new TypeError("Circular references are not supported");
74
- }
75
- stack2.add(value);
76
- const out = [];
77
- const includeAllChildren = allowedByFilter || !hasFilters;
78
- for (let index = 0; index < value.length; index += 1) {
79
- const childPath = path.concat({ type: "INDEX", index });
80
- if (hasRejects && matchesAnyReject(childPath)) continue;
81
- const child = value[index];
82
- const directInclude = hasFilters ? matchesAnyFilter(childPath) : true;
83
- const childAllowed = includeAllChildren || directInclude;
84
- if (isPlainObject(child) || Array.isArray(child)) {
85
- const builtChild = build(
86
- child,
87
- childPath,
88
- childAllowed,
89
- stack2,
90
- false,
91
- true
92
- );
93
- if (builtChild !== void 0) {
94
- out.push(builtChild);
95
- }
96
- } else {
97
- if (childAllowed) {
98
- out.push(child);
99
- }
100
- }
101
- }
102
- stack2.delete(value);
103
- const filteredOut = rejectEmptyObjectsInArray ? out.filter(
104
- (item) => !(isPlainObject(item) && Object.keys(item).length === 0)
105
- ) : out;
106
- if (filteredOut.length === 0 && !allowedByFilter && !isRoot)
107
- return void 0;
108
- return filteredOut;
109
- }
110
- if (isPlainObject(value)) {
111
- if (stack2.has(value)) {
112
- throw new TypeError("Circular references are not supported");
113
- }
114
- stack2.add(value);
115
- const result = {};
116
- const includeAllChildren = allowedByFilter || !hasFilters;
117
- for (const key of Object.keys(value)) {
118
- const childPath = path.concat({ type: "KEY", name: key });
119
- if (hasRejects && matchesAnyReject(childPath)) continue;
120
- const val = value[key];
121
- const directInclude = hasFilters ? matchesAnyFilter(childPath) : true;
122
- const childAllowed = includeAllChildren || directInclude;
123
- if (isPlainObject(val) || Array.isArray(val)) {
124
- const builtChild = build(
125
- val,
126
- childPath,
127
- childAllowed,
128
- stack2,
129
- false,
130
- false
131
- );
132
- if (builtChild === void 0) {
133
- continue;
134
- }
135
- if (Array.isArray(builtChild) && builtChild.length === 0 && !childAllowed) {
136
- continue;
137
- }
138
- if (isPlainObject(builtChild) && Object.keys(builtChild).length === 0 && !childAllowed) {
139
- continue;
140
- }
141
- result[key] = builtChild;
142
- } else {
143
- if (childAllowed) {
144
- result[key] = val;
145
- }
146
- }
147
- }
148
- stack2.delete(value);
149
- if (Object.keys(result).length === 0 && !allowedByFilter && !isRoot) {
150
- if (parentIsArray && !rejectEmptyObjectsInArray) {
151
- return {};
152
- }
153
- return void 0;
154
- }
155
- return result;
156
- }
157
- return allowedByFilter || !hasFilters ? value : void 0;
158
- };
159
- const startPath = [];
160
- const initialAllowed = !hasFilters;
161
- const stack = /* @__PURE__ */ new WeakSet();
162
- const built = build(
163
- objOrArray,
164
- startPath,
165
- initialAllowed,
166
- stack,
167
- true,
168
- false
169
- );
170
- if (built === void 0) return Array.isArray(objOrArray) ? [] : {};
171
- return built;
172
- }
173
- function parsePattern(pattern) {
174
- const tokens = [];
175
- let i = 0;
176
- const n = pattern.length;
177
- const pushKey = (name) => {
178
- if (name.length === 0) return;
179
- tokens.push({ type: "KEY", name });
180
- };
181
- while (i < n) {
182
- const ch = pattern[i];
183
- if (ch === ".") {
184
- i += 1;
185
- continue;
186
- }
187
- if (ch === "[") {
188
- const end = pattern.indexOf("]", i + 1);
189
- const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
190
- if (inside === "*") {
191
- tokens.push({ type: "INDEX_ANY" });
192
- } else if (inside.includes("-")) {
193
- const parts = inside.split("-");
194
- const startStr = parts[0] ?? "";
195
- const endStr = parts[1] ?? "";
196
- const start = parseInt(startStr, 10);
197
- const endNum = endStr === "*" ? null : parseInt(endStr, 10);
198
- tokens.push({
199
- type: "INDEX_RANGE",
200
- start,
201
- end: endNum === null || Number.isFinite(endNum) ? endNum : null
202
- });
203
- } else if (inside.length > 0) {
204
- const idx = parseInt(inside, 10);
205
- tokens.push({ type: "INDEX", index: idx });
206
- }
207
- i = end === -1 ? n : end + 1;
208
- continue;
209
- }
210
- if (ch === "(") {
211
- const end = pattern.indexOf(")", i + 1);
212
- const inside = end === -1 ? pattern.slice(i + 1) : pattern.slice(i + 1, end);
213
- if (inside.includes("|") && inside.trim().length > 0) {
214
- const names = inside.split("|").filter((name) => name.length > 0);
215
- if (names.length > 0) {
216
- tokens.push({ type: "MULTI_KEY", names });
217
- }
218
- }
219
- i = end === -1 ? n : end + 1;
220
- continue;
221
- }
222
- if (ch === "*") {
223
- if (pattern[i + 1] === "*") {
224
- tokens.push({ type: "WILDCARD_ANY" });
225
- i += 2;
226
- let j2 = i;
227
- while (j2 < n) {
228
- const c = pattern[j2];
229
- if (c === "." || c === "[") break;
230
- j2 += 1;
231
- }
232
- if (j2 > i) {
233
- pushKey(pattern.slice(i, j2));
234
- i = j2;
235
- }
236
- continue;
237
- } else {
238
- tokens.push({ type: "WILDCARD_ONE" });
239
- i += 1;
240
- continue;
241
- }
242
- }
243
- let j = i;
244
- while (j < n) {
245
- const c = pattern[j];
246
- if (c === "." || c === "[") break;
247
- j += 1;
248
- }
249
- pushKey(pattern.slice(i, j));
250
- i = j;
251
- }
252
- return tokens;
253
- }
254
-
255
- export {
256
- filterObjectOrArrayKeys
257
- };