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