@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.84
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/dist/index.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/package.json +5 -5
|
@@ -1 +1,282 @@
|
|
|
1
|
-
|
|
1
|
+
// src/lib/object/selector.ts
|
|
2
|
+
var parseSelector = (selector, options = {}) => {
|
|
3
|
+
const { type = "auto", throwOnError = false } = options;
|
|
4
|
+
if (!selector || typeof selector !== "string") {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const segments = [];
|
|
8
|
+
let current = "";
|
|
9
|
+
let inBrackets = false;
|
|
10
|
+
let inQuotes = false;
|
|
11
|
+
let quoteChar = "";
|
|
12
|
+
let bracketContent = "";
|
|
13
|
+
let bracketDepth = 0;
|
|
14
|
+
const detectType = (str) => {
|
|
15
|
+
if (str.includes("..")) return "descendant";
|
|
16
|
+
if (str.includes("[?(")) return "filter";
|
|
17
|
+
if (str.includes("[:") || str.includes("[0:")) return "slice";
|
|
18
|
+
if (str.includes("[,") || str.includes("[0,1]")) return "union";
|
|
19
|
+
if (str.includes("[*]") || str.includes(".*.")) return "wildcard";
|
|
20
|
+
if (str.includes("[]")) return "array-wildcard";
|
|
21
|
+
if (str.includes("[") && str.includes("]")) return "bracket";
|
|
22
|
+
return "dot";
|
|
23
|
+
};
|
|
24
|
+
const actualType = type === "auto" ? detectType(selector) : type;
|
|
25
|
+
const parseBracketContent = (content) => {
|
|
26
|
+
const trimmed = content.trim();
|
|
27
|
+
if (trimmed === "*" || trimmed === "") {
|
|
28
|
+
return {
|
|
29
|
+
type: "wildcard",
|
|
30
|
+
value: trimmed === "" ? "[]" : "*",
|
|
31
|
+
raw: trimmed === "" ? "[]" : content
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (trimmed === "[]") {
|
|
35
|
+
return { type: "wildcard", value: "[]", raw: content };
|
|
36
|
+
}
|
|
37
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
38
|
+
return { type: "index", value: parseInt(trimmed, 10), raw: content };
|
|
39
|
+
}
|
|
40
|
+
if (trimmed.includes(":")) {
|
|
41
|
+
const parts = trimmed.split(":").map((p) => p.trim());
|
|
42
|
+
const start = parts[0] === "" ? 0 : parseInt(parts[0], 10);
|
|
43
|
+
const end = parts[1] === "" ? Infinity : parseInt(parts[1], 10);
|
|
44
|
+
if (isNaN(start) || isNaN(end)) {
|
|
45
|
+
if (throwOnError) throw new Error(`Invalid slice notation: ${content}`);
|
|
46
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
47
|
+
}
|
|
48
|
+
return { type: "slice", value: [start, end], raw: content };
|
|
49
|
+
}
|
|
50
|
+
if (trimmed.includes(",")) {
|
|
51
|
+
const indices = trimmed.split(",").map((p) => {
|
|
52
|
+
const num = parseInt(p.trim(), 10);
|
|
53
|
+
return isNaN(num) ? -1 : num;
|
|
54
|
+
}).filter((n) => n >= 0);
|
|
55
|
+
if (indices.length === 0) {
|
|
56
|
+
if (throwOnError) throw new Error(`Invalid union notation: ${content}`);
|
|
57
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
58
|
+
}
|
|
59
|
+
return { type: "union", value: indices, raw: content };
|
|
60
|
+
}
|
|
61
|
+
if (trimmed.startsWith("?(") && trimmed.endsWith(")")) {
|
|
62
|
+
const expr = trimmed.slice(2, -1);
|
|
63
|
+
try {
|
|
64
|
+
const filterFn = createFilterFunction(expr);
|
|
65
|
+
return { type: "filter", value: filterFn, raw: content };
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (throwOnError) throw error;
|
|
68
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
72
|
+
return { type: "property", value: trimmed.slice(1, -1), raw: content };
|
|
73
|
+
}
|
|
74
|
+
return { type: "property", value: trimmed, raw: content };
|
|
75
|
+
};
|
|
76
|
+
for (let i = 0; i < selector.length; i++) {
|
|
77
|
+
const char = selector[i];
|
|
78
|
+
const nextChar = selector[i + 1];
|
|
79
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
80
|
+
inQuotes = true;
|
|
81
|
+
quoteChar = char;
|
|
82
|
+
continue;
|
|
83
|
+
} else if (char === quoteChar && inQuotes) {
|
|
84
|
+
inQuotes = false;
|
|
85
|
+
quoteChar = "";
|
|
86
|
+
if (!(char === '"' || char === "'")) {
|
|
87
|
+
if (inBrackets) {
|
|
88
|
+
bracketContent += char;
|
|
89
|
+
} else {
|
|
90
|
+
current += char;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (inQuotes) {
|
|
96
|
+
if (inBrackets) {
|
|
97
|
+
bracketContent += char;
|
|
98
|
+
} else {
|
|
99
|
+
current += char;
|
|
100
|
+
}
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (char === "[" && !inBrackets) {
|
|
104
|
+
if (current) {
|
|
105
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
106
|
+
current = "";
|
|
107
|
+
}
|
|
108
|
+
inBrackets = true;
|
|
109
|
+
bracketContent = "";
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (char === "]" && inBrackets && !inQuotes) {
|
|
113
|
+
inBrackets = false;
|
|
114
|
+
const segment = parseBracketContent(bracketContent);
|
|
115
|
+
segments.push(segment);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
let lastDot = segments.length > 0 ? segments[segments.length - 1] : null;
|
|
119
|
+
if (char === "." && !inBrackets && !inQuotes) {
|
|
120
|
+
if (nextChar === ".") {
|
|
121
|
+
const l = selector.split("..")[0];
|
|
122
|
+
segments.push({ type: "property", value: l, raw: l });
|
|
123
|
+
segments.push({ type: "descendant", value: "..", raw: ".." });
|
|
124
|
+
i++;
|
|
125
|
+
} else if (current) {
|
|
126
|
+
if (current === "*") {
|
|
127
|
+
segments.push({ type: "wildcard", value: current, raw: current });
|
|
128
|
+
} else {
|
|
129
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
130
|
+
}
|
|
131
|
+
current = "";
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (inBrackets) {
|
|
136
|
+
bracketContent += char;
|
|
137
|
+
} else {
|
|
138
|
+
if (lastDot && lastDot.type !== "descendant") {
|
|
139
|
+
current += char;
|
|
140
|
+
} else {
|
|
141
|
+
const l = selector.split(lastDot?.raw)[1];
|
|
142
|
+
if (l !== void 0) {
|
|
143
|
+
current = l;
|
|
144
|
+
} else {
|
|
145
|
+
current += char;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (current) {
|
|
151
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
152
|
+
}
|
|
153
|
+
return segments.map((segment) => {
|
|
154
|
+
if (segment.type === "property" && typeof segment.value === "string") {
|
|
155
|
+
if (/^-?\d+$/.test(segment.value)) {
|
|
156
|
+
const num = parseInt(segment.value, 10);
|
|
157
|
+
if (!isNaN(num)) {
|
|
158
|
+
return { ...segment, type: "index", value: num };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return segment;
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
var createFilterFunction = (expr) => {
|
|
166
|
+
const safeExpr = expr.replace(/@\.([a-zA-Z_$][\w$]*)/g, (_, prop) => `item.${prop}`).replace(/@/g, "item");
|
|
167
|
+
try {
|
|
168
|
+
return new Function("item", `return ${safeExpr}`);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
throw new Error(`Invalid filter expression: ${expr}`);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var stringifySelector = (segments) => {
|
|
174
|
+
const parts = [];
|
|
175
|
+
for (let i = 0; i < segments.length; i++) {
|
|
176
|
+
const segment = segments[i];
|
|
177
|
+
const prevSegment = i > 0 ? segments[i - 1] : null;
|
|
178
|
+
switch (segment.type) {
|
|
179
|
+
case "property":
|
|
180
|
+
const needsQuotes = /[\.\[\]\s]/.test(String(segment.value));
|
|
181
|
+
if (needsQuotes) {
|
|
182
|
+
parts.push(`["${segment.value}"]`);
|
|
183
|
+
} else {
|
|
184
|
+
if (prevSegment && prevSegment.type !== "descendant") {
|
|
185
|
+
parts.push(".");
|
|
186
|
+
}
|
|
187
|
+
parts.push(String(segment.value));
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
case "index":
|
|
191
|
+
parts.push(`[${segment.value}]`);
|
|
192
|
+
break;
|
|
193
|
+
case "wildcard":
|
|
194
|
+
if (segment.value === "[]") {
|
|
195
|
+
parts.push("[]");
|
|
196
|
+
} else {
|
|
197
|
+
parts.push("[*]");
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
case "slice":
|
|
201
|
+
const [start, end] = segment.value;
|
|
202
|
+
parts.push(`[${start}:${end === Infinity ? "" : end}]`);
|
|
203
|
+
break;
|
|
204
|
+
case "union":
|
|
205
|
+
const indices = segment.value;
|
|
206
|
+
parts.push(`[${indices.join(",")}]`);
|
|
207
|
+
break;
|
|
208
|
+
case "filter":
|
|
209
|
+
parts.push(`[?(${segment.raw?.slice(2, -1) || ""})]`);
|
|
210
|
+
break;
|
|
211
|
+
case "descendant":
|
|
212
|
+
parts.push("..");
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return parts.join("").replace(/\.\[/g, "[");
|
|
217
|
+
};
|
|
218
|
+
var validateSelector = (selector, options = {}) => {
|
|
219
|
+
if (Array.isArray(selector)) {
|
|
220
|
+
return selector.map(
|
|
221
|
+
(sel) => validateSelector(sel, options)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
if (!/^[a-zA-Z]/.test(selector)) {
|
|
225
|
+
return {
|
|
226
|
+
valid: false,
|
|
227
|
+
error: `Selector starts with '.'`,
|
|
228
|
+
type: detectSelectorType(selector)
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (!/[a-zA-Z]$/.test(selector)) {
|
|
232
|
+
return {
|
|
233
|
+
valid: false,
|
|
234
|
+
error: `Selector end with '.'`,
|
|
235
|
+
type: detectSelectorType(selector)
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const segments = parseSelector(selector, { throwOnError: true });
|
|
240
|
+
const type = detectSelectorType(selector);
|
|
241
|
+
if (options.allowedTypes) {
|
|
242
|
+
if (!options.allowedTypes.includes(type)) {
|
|
243
|
+
return {
|
|
244
|
+
valid: false,
|
|
245
|
+
error: `Selector type ${type} not allowed. Allowed: ${options.allowedTypes.join(
|
|
246
|
+
", "
|
|
247
|
+
)}`,
|
|
248
|
+
type
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return { valid: true, type: detectSelectorType(selector) };
|
|
253
|
+
} catch (error) {
|
|
254
|
+
return { valid: false, error: error.message };
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var detectSelectorType = (selector) => {
|
|
258
|
+
if (selector.includes("..")) return "descendant";
|
|
259
|
+
if (selector.includes("[?(")) return "filter";
|
|
260
|
+
if (selector.includes("[:") || selector.includes("[0:")) return "slice";
|
|
261
|
+
if (/\[[^\]]*,[^\]]*\]/.test(selector)) return "union";
|
|
262
|
+
if (selector.includes("[*]") || selector.includes(".*.")) return "wildcard";
|
|
263
|
+
if (selector.includes("[]")) return "array-wildcard";
|
|
264
|
+
if (selector.includes("[") && selector.includes("]")) return "bracket";
|
|
265
|
+
return "dot";
|
|
266
|
+
};
|
|
267
|
+
var createSelector = (template, replacements) => {
|
|
268
|
+
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
269
|
+
if (key in replacements) {
|
|
270
|
+
return String(replacements[key]);
|
|
271
|
+
}
|
|
272
|
+
return `{${key}}`;
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
export {
|
|
276
|
+
createFilterFunction,
|
|
277
|
+
createSelector,
|
|
278
|
+
detectSelectorType,
|
|
279
|
+
parseSelector,
|
|
280
|
+
stringifySelector,
|
|
281
|
+
validateSelector
|
|
282
|
+
};
|
package/dist/lib/object/sort.cjs
CHANGED
|
@@ -1 +1,209 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/object/sort.ts
|
|
21
|
+
var sort_exports = {};
|
|
22
|
+
__export(sort_exports, {
|
|
23
|
+
move: () => move,
|
|
24
|
+
reorder: () => reorder,
|
|
25
|
+
sort: () => sort,
|
|
26
|
+
sortBy: () => sortBy,
|
|
27
|
+
sortKeys: () => sortKeys,
|
|
28
|
+
swap: () => swap
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(sort_exports);
|
|
31
|
+
|
|
32
|
+
// src/lib/object/utils.ts
|
|
33
|
+
function isPlainObject(v) {
|
|
34
|
+
if (typeof v !== "object" || v === null) return false;
|
|
35
|
+
const proto = Object.getPrototypeOf(v);
|
|
36
|
+
return proto === Object.prototype || proto === null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/lib/object/sort.ts
|
|
40
|
+
function getAtPath(obj, path) {
|
|
41
|
+
if (obj == null) return void 0;
|
|
42
|
+
let cur = obj;
|
|
43
|
+
for (const segment of path.split(".")) {
|
|
44
|
+
if (cur == null) return void 0;
|
|
45
|
+
cur = cur[segment];
|
|
46
|
+
}
|
|
47
|
+
return cur;
|
|
48
|
+
}
|
|
49
|
+
function compareValues(a, b, locale, localeOptions) {
|
|
50
|
+
const aN = a == null;
|
|
51
|
+
const bN = b == null;
|
|
52
|
+
if (aN && bN) return 0;
|
|
53
|
+
if (aN) return 1;
|
|
54
|
+
if (bN) return -1;
|
|
55
|
+
const aNaN = typeof a === "number" && Number.isNaN(a);
|
|
56
|
+
const bNaN = typeof b === "number" && Number.isNaN(b);
|
|
57
|
+
if (aNaN && bNaN) return 0;
|
|
58
|
+
if (aNaN) return 1;
|
|
59
|
+
if (bNaN) return -1;
|
|
60
|
+
if (a instanceof Date && b instanceof Date) {
|
|
61
|
+
return a.getTime() - b.getTime();
|
|
62
|
+
}
|
|
63
|
+
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
64
|
+
return a === b ? 0 : a ? 1 : -1;
|
|
65
|
+
}
|
|
66
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
67
|
+
return a - b;
|
|
68
|
+
}
|
|
69
|
+
if (typeof a === "string" && typeof b === "string") {
|
|
70
|
+
return a.localeCompare(b, locale, localeOptions);
|
|
71
|
+
}
|
|
72
|
+
return String(a).localeCompare(String(b), locale, localeOptions);
|
|
73
|
+
}
|
|
74
|
+
function dirFor(order, i) {
|
|
75
|
+
const o = Array.isArray(order) ? order[i] ?? "asc" : order ?? "asc";
|
|
76
|
+
return o === "desc" ? -1 : 1;
|
|
77
|
+
}
|
|
78
|
+
function sortPrimitiveArray(arr, options) {
|
|
79
|
+
const { order, locale, localeOptions, nullsLast = true } = options;
|
|
80
|
+
const dir = dirFor(order, 0);
|
|
81
|
+
return arr.slice().sort((a, b) => {
|
|
82
|
+
const aN = a == null;
|
|
83
|
+
const bN = b == null;
|
|
84
|
+
if (aN && bN) return 0;
|
|
85
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
86
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
87
|
+
return compareValues(a, b, locale, localeOptions) * dir;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function sortObjectArray(arr, keys, options) {
|
|
91
|
+
const { order, locale, localeOptions, nullsLast = true, comparator } = options;
|
|
92
|
+
if (comparator) {
|
|
93
|
+
return arr.slice().sort(comparator);
|
|
94
|
+
}
|
|
95
|
+
return arr.slice().sort((a, b) => {
|
|
96
|
+
for (let i = 0; i < keys.length; i++) {
|
|
97
|
+
const aVal = getAtPath(a, keys[i]);
|
|
98
|
+
const bVal = getAtPath(b, keys[i]);
|
|
99
|
+
const aN = aVal == null;
|
|
100
|
+
const bN = bVal == null;
|
|
101
|
+
if (aN && bN) continue;
|
|
102
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
103
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
104
|
+
const cmp = compareValues(aVal, bVal, locale, localeOptions);
|
|
105
|
+
if (cmp !== 0) return cmp * dirFor(order, i);
|
|
106
|
+
}
|
|
107
|
+
return 0;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function sortRecord(record, options) {
|
|
111
|
+
const { order, priorityKeys = [], locale, localeOptions, deep } = options;
|
|
112
|
+
const dir = dirFor(order, 0);
|
|
113
|
+
const prioritySet = new Set(priorityKeys);
|
|
114
|
+
const rest = Object.keys(record).filter((k) => !prioritySet.has(k));
|
|
115
|
+
rest.sort(
|
|
116
|
+
(a, b) => a.localeCompare(b, locale, localeOptions) * dir
|
|
117
|
+
);
|
|
118
|
+
const ordered = [...priorityKeys.filter((k) => k in record), ...rest];
|
|
119
|
+
const result = {};
|
|
120
|
+
for (const key of ordered) {
|
|
121
|
+
result[key] = deep ? sortDeep(record[key], options) : record[key];
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
function sortDeep(value, options) {
|
|
126
|
+
if (Array.isArray(value)) {
|
|
127
|
+
const deepSorted = value.map((item) => sortDeep(item, options));
|
|
128
|
+
return sortArrayValue(deepSorted, options);
|
|
129
|
+
}
|
|
130
|
+
if (isPlainObject(value)) {
|
|
131
|
+
return sortRecord(value, options);
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
function applyIndices(arr, indices) {
|
|
136
|
+
const len = arr.length;
|
|
137
|
+
const used = /* @__PURE__ */ new Set();
|
|
138
|
+
const result = [];
|
|
139
|
+
for (const idx of indices) {
|
|
140
|
+
if (idx >= 0 && idx < len && !used.has(idx)) {
|
|
141
|
+
result.push(arr[idx]);
|
|
142
|
+
used.add(idx);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (let i = 0; i < len; i++) {
|
|
146
|
+
if (!used.has(i)) result.push(arr[i]);
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
function sortArrayValue(arr, options) {
|
|
151
|
+
const { by, comparator, indices, order } = options;
|
|
152
|
+
if (indices) {
|
|
153
|
+
const permuted = applyIndices(arr, indices);
|
|
154
|
+
return dirFor(order, 0) === -1 ? permuted.reverse() : permuted;
|
|
155
|
+
}
|
|
156
|
+
if (comparator) {
|
|
157
|
+
return arr.slice().sort(comparator);
|
|
158
|
+
}
|
|
159
|
+
if (by) {
|
|
160
|
+
const keys = Array.isArray(by) ? by : [by];
|
|
161
|
+
return sortObjectArray(arr, keys, options);
|
|
162
|
+
}
|
|
163
|
+
const firstReal = arr.find((x) => x != null);
|
|
164
|
+
if (firstReal != null && isPlainObject(firstReal)) {
|
|
165
|
+
return dirFor(order, 0) === -1 ? arr.slice().reverse() : arr.slice();
|
|
166
|
+
}
|
|
167
|
+
return sortPrimitiveArray(arr, options);
|
|
168
|
+
}
|
|
169
|
+
function sort(input, options = {}) {
|
|
170
|
+
const { deep } = options;
|
|
171
|
+
if (Array.isArray(input)) {
|
|
172
|
+
const items = deep ? input.map((item) => sortDeep(item, options)) : input;
|
|
173
|
+
return sortArrayValue(items, options);
|
|
174
|
+
}
|
|
175
|
+
if (isPlainObject(input)) {
|
|
176
|
+
return sortRecord(input, options);
|
|
177
|
+
}
|
|
178
|
+
return input;
|
|
179
|
+
}
|
|
180
|
+
function sortBy(arr, by, order = "asc", options = {}) {
|
|
181
|
+
return sort(arr, { ...options, by, order });
|
|
182
|
+
}
|
|
183
|
+
function sortKeys(record, options = {}) {
|
|
184
|
+
return sort(record, options);
|
|
185
|
+
}
|
|
186
|
+
function move(arr, from, to) {
|
|
187
|
+
const len = arr.length;
|
|
188
|
+
if (len === 0) return arr.slice();
|
|
189
|
+
const f = Math.max(0, Math.min(len - 1, from));
|
|
190
|
+
const t = Math.max(0, Math.min(len - 1, to));
|
|
191
|
+
if (f === t) return arr.slice();
|
|
192
|
+
const result = arr.slice();
|
|
193
|
+
const [item] = result.splice(f, 1);
|
|
194
|
+
result.splice(t, 0, item);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
function swap(arr, i, j) {
|
|
198
|
+
const len = arr.length;
|
|
199
|
+
if (i < 0 || i >= len) throw new RangeError(`swap: index ${i} out of range`);
|
|
200
|
+
if (j < 0 || j >= len) throw new RangeError(`swap: index ${j} out of range`);
|
|
201
|
+
if (i === j) return arr.slice();
|
|
202
|
+
const result = arr.slice();
|
|
203
|
+
result[i] = arr[j];
|
|
204
|
+
result[j] = arr[i];
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
function reorder(arr, indices, order = "asc") {
|
|
208
|
+
return sort(arr, { indices, order });
|
|
209
|
+
}
|