@ybgnb/utils 0.1.8 → 0.1.9

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.
Files changed (63) hide show
  1. package/LICENSE +1 -1
  2. package/dist/core.cjs +504 -0
  3. package/dist/core.cjs.map +1 -0
  4. package/dist/core.d.cts +369 -0
  5. package/dist/core.d.ts +369 -2
  6. package/dist/core.js +433 -295
  7. package/dist/core.js.map +1 -1
  8. package/dist/dom.cjs +263 -0
  9. package/dist/dom.cjs.map +1 -0
  10. package/dist/dom.d.cts +105 -0
  11. package/dist/dom.d.ts +105 -2
  12. package/dist/dom.js +213 -122
  13. package/dist/dom.js.map +1 -1
  14. package/dist/node.cjs +410 -0
  15. package/dist/node.cjs.map +1 -0
  16. package/dist/node.d.cts +153 -0
  17. package/dist/node.d.ts +153 -2
  18. package/dist/node.js +348 -199
  19. package/dist/node.js.map +1 -1
  20. package/package.json +11 -11
  21. package/dist/core/error/common-error.d.ts +0 -8
  22. package/dist/core/index.d.ts +0 -17
  23. package/dist/core/result/biz-result.d.ts +0 -17
  24. package/dist/core/result/exec-biz.d.ts +0 -6
  25. package/dist/core/types/helpers.d.ts +0 -54
  26. package/dist/core/types/physical.d.ts +0 -16
  27. package/dist/core/utils/array.d.ts +0 -12
  28. package/dist/core/utils/error.d.ts +0 -22
  29. package/dist/core/utils/fetch.d.ts +0 -13
  30. package/dist/core/utils/function.d.ts +0 -28
  31. package/dist/core/utils/github.d.ts +0 -31
  32. package/dist/core/utils/number/format.d.ts +0 -19
  33. package/dist/core/utils/random.d.ts +0 -28
  34. package/dist/core/utils/serialize.d.ts +0 -23
  35. package/dist/core/utils/sleep.d.ts +0 -13
  36. package/dist/core/utils/time.d.ts +0 -19
  37. package/dist/core/utils/type.d.ts +0 -9
  38. package/dist/core/utils/url.d.ts +0 -36
  39. package/dist/core.umd.cjs +0 -2
  40. package/dist/core.umd.cjs.map +0 -1
  41. package/dist/dom/index.d.ts +0 -6
  42. package/dist/dom/utils/color.d.ts +0 -32
  43. package/dist/dom/utils/css.d.ts +0 -4
  44. package/dist/dom/utils/img.d.ts +0 -14
  45. package/dist/dom/utils/network.d.ts +0 -25
  46. package/dist/dom/utils/page.d.ts +0 -10
  47. package/dist/dom/utils/scroll.d.ts +0 -13
  48. package/dist/dom.umd.cjs +0 -2
  49. package/dist/dom.umd.cjs.map +0 -1
  50. package/dist/node/index.d.ts +0 -8
  51. package/dist/node/utils/env.d.ts +0 -6
  52. package/dist/node/utils/file/base.d.ts +0 -20
  53. package/dist/node/utils/file/delete.d.ts +0 -12
  54. package/dist/node/utils/file/download.d.ts +0 -4
  55. package/dist/node/utils/file/find.d.ts +0 -14
  56. package/dist/node/utils/file/json.d.ts +0 -20
  57. package/dist/node/utils/file/size.d.ts +0 -19
  58. package/dist/node/utils/win/cmd.d.ts +0 -21
  59. package/dist/node/utils/win/copy.d.ts +0 -4
  60. package/dist/node/utils/win/explorer.d.ts +0 -5
  61. package/dist/node/utils/win/regedit.d.ts +0 -17
  62. package/dist/node.umd.cjs +0 -2
  63. package/dist/node.umd.cjs.map +0 -1
package/dist/core.js CHANGED
@@ -1,299 +1,437 @@
1
- //#region ../src/core/error/common-error.ts
2
- var e = class extends Error {
3
- constructor(e, t) {
4
- super(e), t !== void 0 && (this.rawError = t instanceof Error ? t : Error(String(t))), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
5
- }
6
- toString() {
7
- return `${this.name}: ${this.message}${this.rawError ? ` (caused by ${this.rawError})` : ""}`;
8
- }
1
+ // src/core/error/common-error.ts
2
+ var CommonError = class extends Error {
3
+ constructor(message, rawError) {
4
+ super(message);
5
+ if (rawError !== void 0) {
6
+ this.rawError = rawError instanceof Error ? rawError : new Error(String(rawError));
7
+ }
8
+ this.name = new.target.name;
9
+ Object.setPrototypeOf(this, new.target.prototype);
10
+ }
11
+ toString() {
12
+ return `${this.name}: ${this.message}${this.rawError ? ` (caused by ${this.rawError})` : ""}`;
13
+ }
9
14
  };
10
- //#endregion
11
- //#region ../src/core/utils/error.ts
12
- function t(e) {
13
- return e instanceof Error ? e.name === "AbortError" || e.name === "CanceledError" || "code" in e && e.code === "ERR_CANCELED" : !1;
14
- }
15
- function n(e) {
16
- let t = Error(e ?? "操作已取消");
17
- return t.name = "AbortError", t;
18
- }
19
- function r(t) {
20
- return t instanceof e || t instanceof Error && t.name === "CommonError";
21
- }
22
- function i(e, t) {
23
- return t && (e.message = `${t} ${e.message}`), e;
24
- }
25
- function a(t, n) {
26
- return r(t) ? i(t, n) : new e(`${n} ${o(t)}`, t);
27
- }
28
- function o(e) {
29
- return typeof e == "string" ? e : e instanceof Error || e && typeof e == "object" && "message" in e && typeof e.message == "string" ? e.message : String(e) ?? "未知错误";
30
- }
31
- //#endregion
32
- //#region ../src/core/result/biz-result.ts
33
- var s = class t {
34
- constructor(e, t, n) {
35
- this.success = e, this.msg = t, this.data = n;
36
- }
37
- static createSuccess(e) {
38
- return new t(!0, "操作成功", e);
39
- }
40
- static createFail(e = "操作失败", n) {
41
- return new t(!1, e, n);
42
- }
43
- static createError(e) {
44
- return new t(!1, o(e));
45
- }
46
- async toPromise() {
47
- if (this.success) return this.data;
48
- throw new e(this.msg);
49
- }
50
- }, c = async (e) => {
51
- try {
52
- return s.createSuccess(await e());
53
- } catch (e) {
54
- return s.createError(e);
55
- }
15
+
16
+ // src/core/utils/error.ts
17
+ function isCanceledError(err) {
18
+ if (!(err instanceof Error)) return false;
19
+ return err.name === "AbortError" || err.name === "CanceledError" || "code" in err && err.code === "ERR_CANCELED";
20
+ }
21
+ function createAbortError(msg) {
22
+ const error = new Error(msg ?? "\u64CD\u4F5C\u5DF2\u53D6\u6D88");
23
+ error.name = "AbortError";
24
+ return error;
25
+ }
26
+ function isCommonError(error) {
27
+ return error instanceof CommonError || error instanceof Error && error.name === "CommonError";
28
+ }
29
+ function prefixError(error, prefix) {
30
+ if (!prefix) return error;
31
+ error.message = `${prefix} ${error.message}`;
32
+ return error;
33
+ }
34
+ function convertToCommonError(error, prefix) {
35
+ if (isCommonError(error)) {
36
+ return prefixError(error, prefix);
37
+ }
38
+ return new CommonError(`${prefix} ${getErrorMessage(error)}`, error);
39
+ }
40
+ function getErrorMessage(error) {
41
+ if (typeof error === "string") {
42
+ return error;
43
+ }
44
+ if (error instanceof Error) {
45
+ return error.message;
46
+ }
47
+ if (error && typeof error === "object" && "message" in error && typeof error.message === "string") {
48
+ return error.message;
49
+ }
50
+ return String(error) ?? "\u672A\u77E5\u9519\u8BEF";
51
+ }
52
+
53
+ // src/core/result/biz-result.ts
54
+ var BizResult = class _BizResult {
55
+ constructor(success, msg, data) {
56
+ this.success = success;
57
+ this.msg = msg;
58
+ this.data = data;
59
+ }
60
+ static createSuccess(data) {
61
+ return new _BizResult(true, "\u64CD\u4F5C\u6210\u529F", data);
62
+ }
63
+ static createFail(msg = "\u64CD\u4F5C\u5931\u8D25", data) {
64
+ return new _BizResult(false, msg, data);
65
+ }
66
+ static createError(e) {
67
+ return new _BizResult(false, getErrorMessage(e));
68
+ }
69
+ async toPromise() {
70
+ if (this.success) {
71
+ return this.data;
72
+ } else {
73
+ throw new CommonError(this.msg);
74
+ }
75
+ }
56
76
  };
57
- //#endregion
58
- //#region ../src/core/utils/array.ts
59
- function l(e) {
60
- return Array.isArray(e) && e.length === 0;
61
- }
62
- function u(e, t) {
63
- if (!Number.isInteger(t) || t < 1) throw Error("size 必须是正整数");
64
- let n = [];
65
- for (let r = 0; r < e.length; r += t) n.push(e.slice(r, r + t));
66
- return n;
67
- }
68
- //#endregion
69
- //#region ../src/core/utils/fetch.ts
70
- function d(e) {
71
- return /* @__PURE__ */ Error(`HTTP ${e.status}: ${e.statusText}`);
72
- }
73
- async function f(e, t, n) {
74
- let r = await fetch(e, n);
75
- if (!r.ok) throw d(r);
76
- switch (t) {
77
- case "json": return await r.json();
78
- case "text": return await r.text();
79
- case "blob": return await r.blob();
80
- case "arrayBuffer": return await r.arrayBuffer();
81
- case "formData": return await r.formData();
82
- default: return await r.text();
83
- }
84
- }
85
- //#endregion
86
- //#region ../src/core/utils/function.ts
87
- function p(e, t) {
88
- t ??= e.length;
89
- let n = Math.floor(Math.random() * t) + 1, r = [...e];
90
- for (let e = r.length - 1; e > 0; e--) {
91
- let t = Math.floor(Math.random() * (e + 1));
92
- [r[e], r[t]] = [r[t], r[e]];
93
- }
94
- let i = r.slice(0, n);
95
- for (let e of i) e();
96
- }
97
- async function m(e, t, n, r) {
98
- for (let i = e; i <= t; i += n) {
99
- let e = t - i + 1, a = Math.min(n, e);
100
- await r(Array.from({ length: a }, (e, t) => i + t));
101
- }
102
- }
103
- function h(e, t, ...n) {
104
- let r = t.split("."), i = e, a = 0;
105
- for (; a < r.length - 1; a++) if (i = i[r[a]], i == null) throw Error(`Path '${r.slice(0, a + 1).join(".")}' is null/undefined`);
106
- let o = i[r[a]];
107
- if (typeof o != "function") throw Error(`'${t}' is not a function`);
108
- return o.apply(i, n);
109
- }
110
- //#endregion
111
- //#region ../src/core/utils/github.ts
112
- function g(e) {
113
- e = e.replace(/^git\+/, "");
114
- let t = /^https:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\.git|\/)?$/, n = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/blob\/([^/]+)$/, r = e.match(t);
115
- if (r) return {
116
- owner: r[1],
117
- repo: r[2],
118
- branch: "main"
119
- };
120
- let i = e.match(n);
121
- if (i) return {
122
- owner: i[1],
123
- repo: i[2],
124
- branch: i[3]
125
- };
126
- throw Error("解析插件仓库URL失败");
127
- }
128
- function _(e) {
129
- let { owner: t, repo: n, branch: r, filePath: i } = e;
130
- return `https://raw.githubusercontent.com/${t}/${n}/${r}/${i}`;
131
- }
132
- async function v(e) {
133
- return JSON.parse(await f(_(e), "json"));
134
- }
135
- //#endregion
136
- //#region ../src/core/utils/random.ts
137
- function y(e) {
138
- let t = e.slice();
139
- for (let e = t.length - 1; e > 0; e--) {
140
- let n = Math.floor(Math.random() * (e + 1));
141
- [t[e], t[n]] = [t[n], t[e]];
142
- }
143
- return t;
144
- }
145
- function b(e, t) {
146
- return Math.floor(Math.random() * (t - e + 1)) + e;
147
- }
148
- function x(e, t) {
149
- return Math.random() * (t - e) + e;
150
- }
151
- function S(e, t) {
152
- return y(e).slice(0, t);
153
- }
154
- function C(e, t) {
155
- let n = [];
156
- for (let r = 0; r < t; r++) {
157
- let t = b(0, e.length - 1);
158
- n.push(e[t]);
159
- }
160
- return n;
161
- }
162
- function w(e) {
163
- return e[b(0, e.length - 1)];
164
- }
165
- function T(e) {
166
- return b(1, e);
167
- }
168
- //#endregion
169
- //#region ../src/core/utils/type.ts
170
- function E(e) {
171
- if (typeof e != "object" || !e) return !1;
172
- let t = Object.getPrototypeOf(e);
173
- return t === Object.prototype || t === null;
174
- }
175
- function D(e, t) {
176
- return typeof e != "object" || !e || Array.isArray(e) ? !1 : t === void 0 ? !0 : t in e;
177
- }
178
- //#endregion
179
- //#region ../src/core/utils/serialize.ts
180
- function O(e, t = /* @__PURE__ */ new WeakMap()) {
181
- if (typeof e == "function") return;
182
- if (typeof e != "object" || !e || e instanceof Date || e instanceof RegExp || e instanceof Map || e instanceof Set || ArrayBuffer.isView(e) || e instanceof ArrayBuffer) return e;
183
- if (t.has(e)) return t.get(e);
184
- if (Array.isArray(e)) {
185
- let n = [];
186
- t.set(e, n);
187
- for (let r of e) n.push(O(r, t));
188
- return n;
189
- }
190
- if (!E(e)) return e;
191
- let n = {};
192
- t.set(e, n);
193
- for (let [r, i] of Object.entries(e)) {
194
- if (typeof i == "function") continue;
195
- let e = O(i, t);
196
- e !== void 0 && (n[r] = e);
197
- }
198
- return n;
199
- }
200
- function k(e) {
201
- return e instanceof Error ? {
202
- name: e.name,
203
- message: e.message,
204
- stack: e.stack
205
- } : {
206
- name: "Error",
207
- message: String(e)
208
- };
209
- }
210
- //#endregion
211
- //#region ../src/core/utils/sleep.ts
212
- function A(e, t) {
213
- return new Promise((n, r) => {
214
- if (t?.aborted) return r(/* @__PURE__ */ Error("取消操作"));
215
- let i = setTimeout(() => {
216
- o(), n();
217
- }, e), a = () => {
218
- clearTimeout(i), o(), r(/* @__PURE__ */ Error("取消操作"));
219
- }, o = () => {
220
- t?.removeEventListener("abort", a);
221
- };
222
- t?.addEventListener("abort", a);
223
- });
224
- }
225
- function j(e, t, n) {
226
- return A(Math.random() * (t - e) + e, n);
227
- }
228
- //#endregion
229
- //#region ../src/core/utils/time.ts
230
- function M(e, t) {
231
- let n = isNaN(e.getTime()) ? /* @__PURE__ */ new Date() : e, r = n.getFullYear(), i = String(n.getMonth() + 1).padStart(2, "0"), a = String(n.getDate()).padStart(2, "0"), o = String(n.getHours()).padStart(2, "0"), s = String(n.getMinutes()).padStart(2, "0"), c = String(n.getSeconds()).padStart(2, "0");
232
- return t === "date" ? `${r}-${i}-${a}` : t === "time" ? `${o}:${s}:${c}` : `${r}-${i}-${a} ${o}:${s}:${c}`;
233
- }
234
- function N(e) {
235
- return M(e ?? /* @__PURE__ */ new Date(), "datetime");
236
- }
237
- function P(e) {
238
- return M(e ?? /* @__PURE__ */ new Date(), "date");
239
- }
240
- function F(e) {
241
- return M(e ?? /* @__PURE__ */ new Date(), "time");
242
- }
243
- //#endregion
244
- //#region ../src/core/utils/url.ts
245
- var I = /[!'()*]/g;
246
- function L(e) {
247
- try {
248
- let t = new URL(e);
249
- return t.protocol === "http:" || t.protocol === "https:";
250
- } catch {
251
- return !1;
252
- }
253
- }
254
- function R(e) {
255
- try {
256
- let { origin: t, pathname: n, searchParams: r } = new URL(e);
257
- return {
258
- baseUrl: t + n,
259
- searchParams: r
260
- };
261
- } catch {
262
- throw Error("Invalid URL");
263
- }
264
- }
265
- function z(e, t = !1) {
266
- return Object.keys(e).sort().filter((n) => t || e[n] != null).map((t) => {
267
- let n = e[t] ?? "";
268
- return `${encodeURIComponent(t)}=${encodeURIComponent(String(n))}`;
269
- }).join("&");
270
- }
271
- function B(e, t) {
272
- let n = {};
273
- return e.size > 0 && e.forEach((e, t) => {
274
- n[t] = e;
275
- }), t && (n = {
276
- ...n,
277
- ...t
278
- }), n;
279
- }
280
- //#endregion
281
- //#region ../src/core/utils/number/format.ts
282
- function V(e, t, n, r = "") {
283
- if (!Number.isFinite(e)) return {
284
- size: 0,
285
- unit: "",
286
- text: r
287
- };
288
- let i = e, a = 0;
289
- for (; i >= t && a < n.length - 1;) i /= t, a++;
290
- return {
291
- size: i,
292
- unit: n[a],
293
- text: `${i.toFixed(2).replace(/\.?0+$/, "")} ${n[a]}`
294
- };
295
- }
296
- //#endregion
297
- export { s as BizResult, e as CommonError, u as chunk, a as convertToCommonError, n as createAbortError, d as createResponseError, h as dynamicCall, z as encodeURLParams, c as execBiz, f as fetchWithFormat, M as formatDate, V as formatUnitSize, o as getErrorMessage, P as getFormattedDate, N as getFormattedDateTime, F as getFormattedTime, v as getGithubRawJson, I as invalidCharRegex, t as isCanceledError, r as isCommonError, l as isEmptyArr, L as isHttpUrl, D as isObject, E as isPlainObject, B as mergeQueryParams, _ as parseGithubRawUrl, g as parseGithubRepoUrl, R as parseUrl, m as processRangeInBatches, T as random1ToN, x as randomFloat, b as randomInt, p as runRandomFunctions, S as sample, w as sampleOne, C as sampleWithReplacement, k as serializeError, y as shuffle, A as sleep, j as sleepRandom, O as stripFunctions };
298
77
 
78
+ // src/core/result/exec-biz.ts
79
+ var execBiz = async (run) => {
80
+ try {
81
+ return BizResult.createSuccess(await run());
82
+ } catch (e) {
83
+ return BizResult.createError(e);
84
+ }
85
+ };
86
+
87
+ // src/core/utils/array.ts
88
+ function isEmptyArr(data) {
89
+ return Array.isArray(data) && data.length === 0;
90
+ }
91
+ function chunk(arr, size) {
92
+ if (!Number.isInteger(size) || size < 1) throw new Error("size \u5FC5\u987B\u662F\u6B63\u6574\u6570");
93
+ const result = [];
94
+ for (let i = 0; i < arr.length; i += size) {
95
+ result.push(arr.slice(i, i + size));
96
+ }
97
+ return result;
98
+ }
99
+
100
+ // src/core/utils/fetch.ts
101
+ function createResponseError(response) {
102
+ return new Error(`HTTP ${response.status}: ${response.statusText}`);
103
+ }
104
+ async function fetchWithFormat(url, format, init) {
105
+ const response = await fetch(url, init);
106
+ if (!response.ok) {
107
+ throw createResponseError(response);
108
+ }
109
+ switch (format) {
110
+ case "json":
111
+ return await response.json();
112
+ case "text":
113
+ return await response.text();
114
+ case "blob":
115
+ return await response.blob();
116
+ case "arrayBuffer":
117
+ return await response.arrayBuffer();
118
+ case "formData":
119
+ return await response.formData();
120
+ default:
121
+ return await response.text();
122
+ }
123
+ }
124
+
125
+ // src/core/utils/function.ts
126
+ function runRandomFunctions(fns, maxCount) {
127
+ maxCount = maxCount ?? fns.length;
128
+ const count = Math.floor(Math.random() * maxCount) + 1;
129
+ const shuffled = [...fns];
130
+ for (let i = shuffled.length - 1; i > 0; i--) {
131
+ const j = Math.floor(Math.random() * (i + 1));
132
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
133
+ }
134
+ const selected = shuffled.slice(0, count);
135
+ for (const fn of selected) {
136
+ fn();
137
+ }
138
+ }
139
+ async function processRangeInBatches(start, end, batchSize, processor) {
140
+ for (let current = start; current <= end; current += batchSize) {
141
+ const remaining = end - current + 1;
142
+ const actualSize = Math.min(batchSize, remaining);
143
+ const batch = Array.from({ length: actualSize }, (_, idx) => current + idx);
144
+ await processor(batch);
145
+ }
146
+ }
147
+ function dynamicCall(root, path, ...args) {
148
+ const segments = path.split(".");
149
+ let context = root;
150
+ let i = 0;
151
+ for (; i < segments.length - 1; i++) {
152
+ context = context[segments[i]];
153
+ if (context == null) {
154
+ throw new Error(`Path '${segments.slice(0, i + 1).join(".")}' is null/undefined`);
155
+ }
156
+ }
157
+ const fn = context[segments[i]];
158
+ if (typeof fn !== "function") {
159
+ throw new Error(`'${path}' is not a function`);
160
+ }
161
+ return fn.apply(context, args);
162
+ }
163
+
164
+ // src/core/utils/github.ts
165
+ function parseGithubRepoUrl(repositoryUrl) {
166
+ repositoryUrl = repositoryUrl.replace(/^git\+/, "");
167
+ const regexBase = /^https:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\.git|\/)?$/;
168
+ const regexFile = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/blob\/([^/]+)$/;
169
+ const m1 = repositoryUrl.match(regexBase);
170
+ if (m1) {
171
+ return {
172
+ owner: m1[1],
173
+ repo: m1[2],
174
+ branch: "main"
175
+ // 默认分支 main
176
+ };
177
+ }
178
+ const m2 = repositoryUrl.match(regexFile);
179
+ if (m2) {
180
+ return {
181
+ owner: m2[1],
182
+ repo: m2[2],
183
+ branch: m2[3]
184
+ };
185
+ }
186
+ throw new Error("\u89E3\u6790\u63D2\u4EF6\u4ED3\u5E93URL\u5931\u8D25");
187
+ }
188
+ function parseGithubRawUrl(gitHubRepoFile) {
189
+ const { owner, repo, branch, filePath } = gitHubRepoFile;
190
+ return `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${filePath}`;
191
+ }
192
+ async function getGithubRawJson(gitHubRepoFile) {
193
+ return JSON.parse(await fetchWithFormat(parseGithubRawUrl(gitHubRepoFile), "json"));
194
+ }
195
+
196
+ // src/core/utils/random.ts
197
+ function shuffle(arr) {
198
+ const clone = arr.slice();
199
+ for (let i = clone.length - 1; i > 0; i--) {
200
+ const j = Math.floor(Math.random() * (i + 1));
201
+ [clone[i], clone[j]] = [clone[j], clone[i]];
202
+ }
203
+ return clone;
204
+ }
205
+ function randomInt(min, max) {
206
+ return Math.floor(Math.random() * (max - min + 1)) + min;
207
+ }
208
+ function randomFloat(min, max) {
209
+ return Math.random() * (max - min) + min;
210
+ }
211
+ function sample(arr, count) {
212
+ return shuffle(arr).slice(0, count);
213
+ }
214
+ function sampleWithReplacement(arr, count) {
215
+ const result = [];
216
+ for (let i = 0; i < count; i++) {
217
+ const index = randomInt(0, arr.length - 1);
218
+ result.push(arr[index]);
219
+ }
220
+ return result;
221
+ }
222
+ function sampleOne(arr) {
223
+ return arr[randomInt(0, arr.length - 1)];
224
+ }
225
+ function random1ToN(n) {
226
+ return randomInt(1, n);
227
+ }
228
+
229
+ // src/core/utils/type.ts
230
+ function isPlainObject(value) {
231
+ if (value === null || typeof value !== "object") return false;
232
+ const proto = Object.getPrototypeOf(value);
233
+ return proto === Object.prototype || proto === null;
234
+ }
235
+ function isObject(value, key) {
236
+ if (value === null || typeof value !== "object") return false;
237
+ if (Array.isArray(value)) return false;
238
+ if (key !== void 0) {
239
+ return key in value;
240
+ }
241
+ return true;
242
+ }
243
+
244
+ // src/core/utils/serialize.ts
245
+ function stripFunctions(value, seen = /* @__PURE__ */ new WeakMap()) {
246
+ if (typeof value === "function") return void 0;
247
+ if (value === null || typeof value !== "object") return value;
248
+ if (value instanceof Date || value instanceof RegExp || value instanceof Map || value instanceof Set || ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
249
+ return value;
250
+ }
251
+ if (seen.has(value)) {
252
+ return seen.get(value);
253
+ }
254
+ if (Array.isArray(value)) {
255
+ const out2 = [];
256
+ seen.set(value, out2);
257
+ for (const item of value) {
258
+ out2.push(stripFunctions(item, seen));
259
+ }
260
+ return out2;
261
+ }
262
+ if (!isPlainObject(value)) {
263
+ return value;
264
+ }
265
+ const out = {};
266
+ seen.set(value, out);
267
+ for (const [key, item] of Object.entries(value)) {
268
+ if (typeof item === "function") continue;
269
+ const next = stripFunctions(item, seen);
270
+ if (next !== void 0) out[key] = next;
271
+ }
272
+ return out;
273
+ }
274
+ function serializeError(err) {
275
+ if (err instanceof Error) {
276
+ return {
277
+ name: err.name,
278
+ message: err.message,
279
+ stack: err.stack
280
+ };
281
+ }
282
+ return {
283
+ name: "Error",
284
+ message: String(err)
285
+ };
286
+ }
287
+
288
+ // src/core/utils/sleep.ts
289
+ function sleep(ms, signal) {
290
+ return new Promise((resolve, reject) => {
291
+ if (signal?.aborted) return reject(new Error("\u53D6\u6D88\u64CD\u4F5C"));
292
+ const timer = setTimeout(() => {
293
+ cleanup();
294
+ resolve();
295
+ }, ms);
296
+ const onAbort = () => {
297
+ clearTimeout(timer);
298
+ cleanup();
299
+ reject(new Error("\u53D6\u6D88\u64CD\u4F5C"));
300
+ };
301
+ const cleanup = () => {
302
+ signal?.removeEventListener("abort", onAbort);
303
+ };
304
+ signal?.addEventListener("abort", onAbort);
305
+ });
306
+ }
307
+ function sleepRandom(minMS, maxMS, signal) {
308
+ const ms = Math.random() * (maxMS - minMS) + minMS;
309
+ return sleep(ms, signal);
310
+ }
311
+
312
+ // src/core/utils/time.ts
313
+ function formatDate(date, pattern) {
314
+ const d = !isNaN(date.getTime()) ? date : /* @__PURE__ */ new Date();
315
+ const year = d.getFullYear();
316
+ const month = String(d.getMonth() + 1).padStart(2, "0");
317
+ const day = String(d.getDate()).padStart(2, "0");
318
+ const hours = String(d.getHours()).padStart(2, "0");
319
+ const minutes = String(d.getMinutes()).padStart(2, "0");
320
+ const seconds = String(d.getSeconds()).padStart(2, "0");
321
+ if (pattern === "date") return `${year}-${month}-${day}`;
322
+ if (pattern === "time") return `${hours}:${minutes}:${seconds}`;
323
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
324
+ }
325
+ function getFormattedDateTime(date) {
326
+ return formatDate(date ?? /* @__PURE__ */ new Date(), "datetime");
327
+ }
328
+ function getFormattedDate(date) {
329
+ return formatDate(date ?? /* @__PURE__ */ new Date(), "date");
330
+ }
331
+ function getFormattedTime(date) {
332
+ return formatDate(date ?? /* @__PURE__ */ new Date(), "time");
333
+ }
334
+
335
+ // src/core/utils/url.ts
336
+ var invalidCharRegex = /[!'()*]/g;
337
+ function isHttpUrl(path) {
338
+ try {
339
+ const url = new URL(path);
340
+ return url.protocol === "http:" || url.protocol === "https:";
341
+ } catch {
342
+ return false;
343
+ }
344
+ }
345
+ function parseUrl(url) {
346
+ try {
347
+ const { origin, pathname, searchParams } = new URL(url);
348
+ return {
349
+ baseUrl: origin + pathname,
350
+ searchParams
351
+ };
352
+ } catch {
353
+ throw new Error("Invalid URL");
354
+ }
355
+ }
356
+ function encodeURLParams(params, keepEmptyValues = false) {
357
+ return Object.keys(params).sort().filter((key) => {
358
+ return keepEmptyValues || params[key] != null;
359
+ }).map((key) => {
360
+ const raw = params[key] ?? "";
361
+ return `${encodeURIComponent(key)}=${encodeURIComponent(String(raw))}`;
362
+ }).join("&");
363
+ }
364
+ function mergeQueryParams(urlSearchParams, params) {
365
+ let mergedParams = {};
366
+ if (urlSearchParams.size > 0) {
367
+ urlSearchParams.forEach((value, key) => {
368
+ mergedParams[key] = value;
369
+ });
370
+ }
371
+ if (params) {
372
+ mergedParams = { ...mergedParams, ...params };
373
+ }
374
+ return mergedParams;
375
+ }
376
+
377
+ // src/core/utils/number/format.ts
378
+ function formatUnitSize(value, base, units, invalidText = "") {
379
+ if (!Number.isFinite(value)) {
380
+ return { size: 0, unit: "", text: invalidText };
381
+ }
382
+ let size = value;
383
+ let unitIndex = 0;
384
+ while (size >= base && unitIndex < units.length - 1) {
385
+ size /= base;
386
+ unitIndex++;
387
+ }
388
+ return {
389
+ size,
390
+ unit: units[unitIndex],
391
+ text: `${size.toFixed(2).replace(/\.?0+$/, "")} ${units[unitIndex]}`
392
+ };
393
+ }
394
+ export {
395
+ BizResult,
396
+ CommonError,
397
+ chunk,
398
+ convertToCommonError,
399
+ createAbortError,
400
+ createResponseError,
401
+ dynamicCall,
402
+ encodeURLParams,
403
+ execBiz,
404
+ fetchWithFormat,
405
+ formatDate,
406
+ formatUnitSize,
407
+ getErrorMessage,
408
+ getFormattedDate,
409
+ getFormattedDateTime,
410
+ getFormattedTime,
411
+ getGithubRawJson,
412
+ invalidCharRegex,
413
+ isCanceledError,
414
+ isCommonError,
415
+ isEmptyArr,
416
+ isHttpUrl,
417
+ isObject,
418
+ isPlainObject,
419
+ mergeQueryParams,
420
+ parseGithubRawUrl,
421
+ parseGithubRepoUrl,
422
+ parseUrl,
423
+ processRangeInBatches,
424
+ random1ToN,
425
+ randomFloat,
426
+ randomInt,
427
+ runRandomFunctions,
428
+ sample,
429
+ sampleOne,
430
+ sampleWithReplacement,
431
+ serializeError,
432
+ shuffle,
433
+ sleep,
434
+ sleepRandom,
435
+ stripFunctions
436
+ };
299
437
  //# sourceMappingURL=core.js.map