@vueboot/helper 0.0.0-stage → 0.4.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/index.js ADDED
@@ -0,0 +1,618 @@
1
+ // src/assert.ts
2
+ function assertString(val, funcName = "", valName = "") {
3
+ if (typeof val !== "string") {
4
+ throw new TypeError(
5
+ `[helper]${funcName ? `${funcName}: ` : ""}${valName ? `${valName}: ` : ""} \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`
6
+ );
7
+ }
8
+ }
9
+ function assertNumber(val, funcName = "", valName = "") {
10
+ if (typeof val !== "number") {
11
+ throw new TypeError(
12
+ `[helper]${funcName ? `${funcName}: ` : ""}${valName ? `${valName}: ` : ""} \u5FC5\u987B\u662F\u6570\u5B57`
13
+ );
14
+ }
15
+ }
16
+ function assertRangeNumber(min, max, funcName = "") {
17
+ if (Number.isNaN(min) || Number.isNaN(max) || min > max) {
18
+ throw new RangeError(
19
+ `[helper] ${funcName ? `${funcName}: ` : ""}min \u4E0D\u80FD\u5927\u4E8E max`
20
+ );
21
+ }
22
+ }
23
+ function assertPrecision(precision) {
24
+ if (!Number.isInteger(precision) || precision < 0 || precision > 100) {
25
+ throw new RangeError("[helper] precision \u5FC5\u987B\u662F 0 \u5230 100 \u4E4B\u95F4\u7684\u6574\u6570");
26
+ }
27
+ }
28
+ function assertArray(val, funcName = "", valName = "") {
29
+ if (!Array.isArray(val)) {
30
+ throw new TypeError(
31
+ `[helper]${funcName ? `${funcName}: ` : ""}${valName ? `${valName}: ` : ""} \u5FC5\u987B\u662F\u6570\u7EC4`
32
+ );
33
+ }
34
+ }
35
+
36
+ // src/validation.ts
37
+ var EMAIL_RE = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
38
+ var MOBILE_CN_RE = /^1[3-9]\d{9}$/;
39
+ var ID_CARD_FACTORS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
40
+ var ID_CARD_CODES = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
41
+ var CREDIT_CODE_CHARS = "0123456789ABCDEFGHJKLMNPQRTUWXY";
42
+ var CREDIT_CODE_WEIGHTS = [
43
+ 1,
44
+ 3,
45
+ 9,
46
+ 27,
47
+ 19,
48
+ 26,
49
+ 16,
50
+ 17,
51
+ 20,
52
+ 29,
53
+ 25,
54
+ 13,
55
+ 8,
56
+ 24,
57
+ 10,
58
+ 30,
59
+ 28
60
+ ];
61
+ function isEmpty(value) {
62
+ if (value === null || value === void 0) return true;
63
+ if (typeof value === "string") return value.length === 0;
64
+ if (Array.isArray(value)) return value.length === 0;
65
+ if (value instanceof Map || value instanceof Set) return value.size === 0;
66
+ if (typeof value === "object") {
67
+ const proto = Object.getPrototypeOf(value);
68
+ if (proto !== Object.prototype && proto !== null) return false;
69
+ return Object.keys(value).length === 0;
70
+ }
71
+ return false;
72
+ }
73
+ function isNumeric(value) {
74
+ if (typeof value === "number") return Number.isFinite(value);
75
+ if (typeof value === "string") {
76
+ const trimmed = value.trim();
77
+ return trimmed !== "" && Number.isFinite(Number(trimmed));
78
+ }
79
+ return false;
80
+ }
81
+ function isPlainObject(value) {
82
+ if (value === null || typeof value !== "object") return false;
83
+ const proto = Object.getPrototypeOf(value);
84
+ return proto === null || proto === Object.prototype;
85
+ }
86
+ function isHttpUrl(value) {
87
+ if (typeof value !== "string") return false;
88
+ try {
89
+ const url = new URL(value);
90
+ return url.protocol === "http:" || url.protocol === "https:";
91
+ } catch {
92
+ return false;
93
+ }
94
+ }
95
+ function isEmail(value) {
96
+ return typeof value === "string" && EMAIL_RE.test(value);
97
+ }
98
+ function isChinaMobile(value) {
99
+ return typeof value === "string" && MOBILE_CN_RE.test(value);
100
+ }
101
+ function isChinaIdCard(value) {
102
+ if (typeof value !== "string" || !/^\d{17}[\dXx]$/.test(value)) {
103
+ return false;
104
+ }
105
+ const year = Number(value.slice(6, 10));
106
+ const month = Number(value.slice(10, 12));
107
+ const day = Number(value.slice(12, 14));
108
+ const birth = new Date(year, month - 1, day);
109
+ if (birth.getFullYear() !== year || birth.getMonth() !== month - 1 || birth.getDate() !== day || year < 1900 || birth > /* @__PURE__ */ new Date()) {
110
+ return false;
111
+ }
112
+ let sum = 0;
113
+ for (let index = 0; index < 17; index += 1) {
114
+ sum += Number(value.charAt(index)) * ID_CARD_FACTORS[index];
115
+ }
116
+ return value.charAt(17).toUpperCase() === ID_CARD_CODES[sum % 11];
117
+ }
118
+ function isChinaUSCC(value) {
119
+ if (typeof value !== "string" || !/^[0-9A-HJ-NP-RT-UW-Y]{18}$/.test(value)) {
120
+ return false;
121
+ }
122
+ let sum = 0;
123
+ for (let index = 0; index < 17; index += 1) {
124
+ const charIndex = CREDIT_CODE_CHARS.indexOf(value.charAt(index));
125
+ if (charIndex === -1) return false;
126
+ sum += charIndex * CREDIT_CODE_WEIGHTS[index];
127
+ }
128
+ const remainder = sum % 31;
129
+ const expected = remainder === 0 ? "0" : CREDIT_CODE_CHARS.charAt(31 - remainder);
130
+ return value.charAt(17) === expected;
131
+ }
132
+
133
+ // src/string.ts
134
+ var WORD_BOUNDARY = /([a-z0-9])([A-Z])/g;
135
+ var ACRONYM_BOUNDARY = /([A-Z]+)([A-Z][a-z])/g;
136
+ var SEPARATORS = /[^A-Za-z0-9]+/;
137
+ function splitWords(value) {
138
+ return value.replace(WORD_BOUNDARY, "$1 $2").replace(ACRONYM_BOUNDARY, "$1 $2").split(SEPARATORS).filter((word) => word.length > 0).map((word) => word.toLowerCase());
139
+ }
140
+ function capitalize(value) {
141
+ if (value.length === 0) return value;
142
+ return value.charAt(0).toUpperCase() + value.slice(1);
143
+ }
144
+ function camelCase(value) {
145
+ let result = "";
146
+ splitWords(value).forEach((word, index) => {
147
+ result += index === 0 ? word : capitalize(word);
148
+ });
149
+ return result;
150
+ }
151
+ function pascalCase(value) {
152
+ return splitWords(value).map(capitalize).join("");
153
+ }
154
+ function kebabCase(value) {
155
+ return splitWords(value).join("-");
156
+ }
157
+ function snakeCase(value) {
158
+ return splitWords(value).join("_");
159
+ }
160
+ function cutString(value, maxLength, suffix = "...") {
161
+ const limit = Math.floor(maxLength);
162
+ if (limit <= 0) return "";
163
+ if (value.length <= limit) return value;
164
+ if (suffix.length >= limit) return value.slice(0, limit);
165
+ return value.slice(0, limit - suffix.length) + suffix;
166
+ }
167
+ function cleanString(value, trimWhitespace = true, removeNewlines = true) {
168
+ if (typeof value !== "string") return "";
169
+ let result = value;
170
+ if (trimWhitespace) result = result.trim();
171
+ if (removeNewlines) result = result.replace(/[\r\n]+/g, "");
172
+ return result;
173
+ }
174
+ function stripHtml(value) {
175
+ if (typeof value !== "string") return "";
176
+ return value.replace(/<!--[\s\S]*?-->/g, "").replace(/<script[\s\S]*?<\/script>/gi, "").replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<[^>]+>/g, "").trim();
177
+ }
178
+ var MASK_PHONE_RE = /^1[3-9]\d{9}$/;
179
+ var MASK_ID_CARD_RE = /^(\d{15}|\d{17}[\dXx])$/;
180
+ var MASK_LANDLINE_RE = /^\d{7,8}$/;
181
+ var MASK_LANDLINE_WITH_AREA_RE = /^0\d{9,11}$/;
182
+ var MASK_PLATE_RE = /^[一-龥][A-Za-z][A-Za-z0-9]{5,6}$/;
183
+ function maskSensitive(value) {
184
+ if (typeof value === "number") {
185
+ value = String(value);
186
+ } else if (typeof value !== "string" || value.trim() === "") {
187
+ return "";
188
+ }
189
+ const cleaned = value.replace(/[\s\-()]/g, "");
190
+ if (MASK_PHONE_RE.test(cleaned)) {
191
+ return `${cleaned.slice(0, 3)}****${cleaned.slice(7)}`;
192
+ }
193
+ const atIndex = cleaned.indexOf("@");
194
+ if (atIndex > 0) {
195
+ const local = cleaned.slice(0, atIndex);
196
+ const domain = cleaned.slice(atIndex);
197
+ if (domain.length >= 4) {
198
+ const prefix = local.length <= 3 ? `${local.charAt(0)}***` : `${local.slice(0, 3)}***`;
199
+ return prefix + domain;
200
+ }
201
+ }
202
+ if (MASK_ID_CARD_RE.test(cleaned)) {
203
+ const id = cleaned.toUpperCase();
204
+ return `${id.slice(0, 6)}${"*".repeat(id.length - 10)}${id.slice(-4)}`;
205
+ }
206
+ if (MASK_LANDLINE_RE.test(cleaned) || MASK_LANDLINE_WITH_AREA_RE.test(cleaned)) {
207
+ if (cleaned.length >= 8) {
208
+ return `${cleaned.slice(0, 3)}****${cleaned.slice(-4)}`;
209
+ }
210
+ if (cleaned.length === 7) {
211
+ return `${cleaned.slice(0, 3)}**${cleaned.slice(-2)}`;
212
+ }
213
+ return "*".repeat(cleaned.length);
214
+ }
215
+ if (MASK_PLATE_RE.test(cleaned)) {
216
+ const keepSuffix = cleaned.length >= 7 ? 2 : 1;
217
+ return cleaned.slice(0, 2) + "*".repeat(cleaned.length - 2 - keepSuffix) + cleaned.slice(-keepSuffix);
218
+ }
219
+ const name = value.trim();
220
+ const length = name.length;
221
+ if (length <= 1) return "*";
222
+ if (length === 2) return `${name.charAt(0)}*`;
223
+ if (length === 3) return `${name.charAt(0)}*${name.charAt(2)}`;
224
+ if (length === 4) return `${name.charAt(0)}**${name.charAt(3)}`;
225
+ if (length <= 6) {
226
+ return `${name.charAt(0)}${"*".repeat(length - 3)}${name.slice(-2)}`;
227
+ }
228
+ if (length <= 10) {
229
+ return `${name.slice(0, 2)}${"*".repeat(length - 4)}${name.slice(-2)}`;
230
+ }
231
+ return `${name.slice(0, 3)}${"*".repeat(length - 7)}${name.slice(-4)}`;
232
+ }
233
+
234
+ // src/number.ts
235
+ function rangeClamp(value, min, max) {
236
+ assertRangeNumber(min, max, "rangeClamp");
237
+ return Math.min(Math.max(value, min), max);
238
+ }
239
+ function toRound(value, precision = 0) {
240
+ assertPrecision(precision);
241
+ if (!isNumeric(value)) return 0;
242
+ const factor = 10 ** precision;
243
+ return Math.round(Number(value) * factor) / factor;
244
+ }
245
+ function toFloor(value, precision = 0) {
246
+ assertPrecision(precision);
247
+ if (!isNumeric(value)) return 0;
248
+ const factor = 10 ** precision;
249
+ return Math.floor(Number(value) * factor) / factor;
250
+ }
251
+ function toCeil(value, precision = 0) {
252
+ assertPrecision(precision);
253
+ if (!isNumeric(value)) return 0;
254
+ const factor = 10 ** precision;
255
+ return Math.ceil(Number(value) * factor) / factor;
256
+ }
257
+ function inRange(value, min, max) {
258
+ assertRangeNumber(min, max, "inRange");
259
+ return value >= min && value <= max;
260
+ }
261
+ function toFixed(value, precision = 2) {
262
+ assertPrecision(precision);
263
+ if (!isNumeric(value)) return String(value);
264
+ return Number(value).toFixed(precision);
265
+ }
266
+
267
+ // src/object.ts
268
+ function cloneValue(input, seen) {
269
+ if (input === null || typeof input !== "object") return input;
270
+ const cached = seen.get(input);
271
+ if (cached !== void 0) return cached;
272
+ const tag = Object.prototype.toString.call(input);
273
+ if (tag === "[object Boolean]" || tag === "[object Number]" || tag === "[object String]" || tag === "[object Symbol]" || tag === "[object BigInt]") {
274
+ return Object(input.valueOf());
275
+ }
276
+ if (input instanceof Date) return new Date(input.getTime());
277
+ if (input instanceof RegExp) {
278
+ const regExp = new RegExp(input.source, input.flags);
279
+ regExp.lastIndex = input.lastIndex;
280
+ return regExp;
281
+ }
282
+ if (input instanceof Map) {
283
+ const result2 = /* @__PURE__ */ new Map();
284
+ seen.set(input, result2);
285
+ input.forEach((value, key) => {
286
+ result2.set(cloneValue(key, seen), cloneValue(value, seen));
287
+ });
288
+ return result2;
289
+ }
290
+ if (input instanceof Set) {
291
+ const result2 = /* @__PURE__ */ new Set();
292
+ seen.set(input, result2);
293
+ input.forEach((value) => {
294
+ result2.add(cloneValue(value, seen));
295
+ });
296
+ return result2;
297
+ }
298
+ const isArray = Array.isArray(input);
299
+ const result = isArray ? [] : Object.create(Object.getPrototypeOf(input));
300
+ seen.set(input, result);
301
+ Reflect.ownKeys(input).forEach((key) => {
302
+ if (isArray && key === "length") return;
303
+ if (!Object.prototype.hasOwnProperty.call(input, key)) return;
304
+ result[key] = cloneValue(
305
+ input[key],
306
+ seen
307
+ );
308
+ });
309
+ return result;
310
+ }
311
+ function deepClone(value) {
312
+ return cloneValue(value, /* @__PURE__ */ new WeakMap());
313
+ }
314
+ function isMergeableObject(value) {
315
+ if (value === null || typeof value !== "object") return false;
316
+ const proto = Object.getPrototypeOf(value);
317
+ return proto === null || proto === Object.prototype;
318
+ }
319
+ function mergeInto(target, source) {
320
+ Reflect.ownKeys(source).forEach((key) => {
321
+ if (key === "__proto__") return;
322
+ if (!Object.prototype.hasOwnProperty.call(source, key)) return;
323
+ const sourceValue = source[key];
324
+ const targetValue = target[key];
325
+ if (isMergeableObject(targetValue) && isMergeableObject(sourceValue)) {
326
+ mergeInto(targetValue, sourceValue);
327
+ } else {
328
+ target[key] = deepClone(sourceValue);
329
+ }
330
+ });
331
+ }
332
+ function deepMerge(...sources) {
333
+ const result = {};
334
+ sources.forEach((source) => {
335
+ if (source === null || typeof source !== "object") return;
336
+ mergeInto(result, source);
337
+ });
338
+ return result;
339
+ }
340
+
341
+ // src/format.ts
342
+ function groupThousands(value) {
343
+ return value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
344
+ }
345
+ function formatMoney(value, prefix = "\uFFE5", suffix = "\u5143") {
346
+ if (!isNumeric(value)) return `${prefix}0.00${suffix}`;
347
+ const num = Number(value);
348
+ const fixed = Math.abs(num).toFixed(2);
349
+ const [integer = "0", decimal = "00"] = fixed.split(".");
350
+ const sign = num < 0 ? "-" : "";
351
+ return `${prefix}${sign}${groupThousands(integer)}.${decimal}${suffix}`;
352
+ }
353
+ var RMB_DIGITS = "\u96F6\u58F9\u8D30\u53C1\u8086\u4F0D\u9646\u67D2\u634C\u7396";
354
+ var RMB_UNITS = "\u4EDF\u4F70\u62FE\u4EBF\u4EDF\u4F70\u62FE\u4E07\u4EDF\u4F70\u62FE\u5143\u89D2\u5206";
355
+ function formatMoneyUpper(value) {
356
+ if (!isNumeric(value)) return "";
357
+ const num = Number(value);
358
+ const negative = num < 0;
359
+ const cents = Math.round(Math.abs(num) * 100);
360
+ if (cents === 0) return "\u96F6\u5143\u6574";
361
+ const digits = String(cents).padStart(3, "0");
362
+ if (digits.length > RMB_UNITS.length) return "";
363
+ const units = RMB_UNITS.slice(RMB_UNITS.length - digits.length);
364
+ let formatted = "";
365
+ for (let index = 0; index < digits.length; index += 1) {
366
+ const digit = Number(digits.charAt(index));
367
+ formatted += RMB_DIGITS.charAt(digit) + units.charAt(index);
368
+ }
369
+ formatted = formatted.replace(/零(仟|佰|拾|角)/g, "\u96F6").replace(/(零)+/g, "\u96F6").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "\u5143\u6574");
370
+ return (negative ? "\u8D1F" : "") + formatted;
371
+ }
372
+ var BYTE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB"];
373
+ function formatBytes(value, precision = 2) {
374
+ assertPrecision(precision);
375
+ if (!isNumeric(value)) return "0 B";
376
+ let size = Number(value);
377
+ if (size < 1024) return `${size} B`;
378
+ let unitIndex = 0;
379
+ while (size >= 1024 && unitIndex < BYTE_UNITS.length - 1) {
380
+ size /= 1024;
381
+ unitIndex += 1;
382
+ }
383
+ return `${toCeil(size, precision)} ${BYTE_UNITS[unitIndex]}`;
384
+ }
385
+ function formatKW(value, precision = 1) {
386
+ assertPrecision(precision);
387
+ if (!isNumeric(value)) return "-";
388
+ const num = Math.trunc(Number(value));
389
+ if (num < 1) return "0";
390
+ if (num < 1e3) return String(num);
391
+ if (num < 1e4) return `${toFloor(num / 1e3, precision)}K`;
392
+ return `${toFloor(num / 1e4, precision)}W`;
393
+ }
394
+
395
+ // src/random.ts
396
+ var UINT32_RANGE = 4294967296;
397
+ var MAX_RANDOM = 2 ** 53;
398
+ var DEFAULT_RANDOM_CHARS = "abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
399
+ function createRandomSource() {
400
+ const cryptoApi = globalThis.crypto;
401
+ if (typeof cryptoApi?.getRandomValues !== "function") {
402
+ return {
403
+ index: (size) => Math.floor(Math.random() * size),
404
+ fraction: () => Math.random()
405
+ };
406
+ }
407
+ const buffer = new Uint32Array(1);
408
+ const uint32 = () => {
409
+ cryptoApi.getRandomValues(buffer);
410
+ return buffer[0];
411
+ };
412
+ const uint53 = () => (uint32() & 2097151) * UINT32_RANGE + uint32();
413
+ return {
414
+ index(size) {
415
+ if (size <= UINT32_RANGE) {
416
+ const limit2 = Math.floor(UINT32_RANGE / size) * size;
417
+ let value2 = uint32();
418
+ while (value2 >= limit2) value2 = uint32();
419
+ return value2 % size;
420
+ }
421
+ const limit = Math.floor(MAX_RANDOM / size) * size;
422
+ let value = uint53();
423
+ while (value >= limit) value = uint53();
424
+ return value % size;
425
+ },
426
+ fraction() {
427
+ return uint53() / MAX_RANDOM;
428
+ }
429
+ };
430
+ }
431
+ function randomString(length = 16, chars = DEFAULT_RANDOM_CHARS) {
432
+ if (!Number.isInteger(length) || length < 1) {
433
+ throw new RangeError("[helper] randomString: length \u5FC5\u987B\u662F\u6B63\u6574\u6570");
434
+ }
435
+ if (typeof chars !== "string") {
436
+ throw new TypeError("[helper] randomString: chars \u5FC5\u987B\u662F\u5B57\u7B26\u4E32");
437
+ }
438
+ if (chars.length < 2) {
439
+ throw new RangeError("[helper] randomString: chars \u81F3\u5C11\u9700\u8981 2 \u4E2A\u5B57\u7B26");
440
+ }
441
+ const source = createRandomSource();
442
+ const firstChars = chars.replace(/[0-9]/g, "") || chars;
443
+ let result = firstChars[source.index(firstChars.length)];
444
+ for (let index = 1; index < length; index += 1) {
445
+ result += chars[source.index(chars.length)];
446
+ }
447
+ return result;
448
+ }
449
+ function randomInteger(length) {
450
+ if (!Number.isInteger(length) || length < 1 || length > 15) {
451
+ throw new RangeError("[helper] randomInteger: length \u5FC5\u987B\u662F 1 \u5230 15 \u7684\u6574\u6570");
452
+ }
453
+ const source = createRandomSource();
454
+ let result = 1 + source.index(9);
455
+ for (let index = 1; index < length; index += 1) {
456
+ result = result * 10 + source.index(10);
457
+ }
458
+ return result;
459
+ }
460
+ function randomInt(min, max) {
461
+ if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) {
462
+ throw new RangeError("[helper] randomInt: min \u4E0E max \u5FC5\u987B\u662F\u5B89\u5168\u6574\u6570");
463
+ }
464
+ if (min > max) {
465
+ throw new RangeError("[helper] randomInt: min \u4E0D\u80FD\u5927\u4E8E max");
466
+ }
467
+ const size = max - min + 1;
468
+ if (!Number.isSafeInteger(size) || size < 1) {
469
+ throw new RangeError("[helper] randomInt: \u968F\u673A\u533A\u95F4\u8FC7\u5927");
470
+ }
471
+ return min + createRandomSource().index(size);
472
+ }
473
+ function randomFloat(min, max, precision = 2) {
474
+ if (!Number.isFinite(min) || !Number.isFinite(max)) {
475
+ throw new RangeError("[helper] randomFloat: min \u4E0E max \u5FC5\u987B\u662F\u6709\u9650\u6570\u5B57");
476
+ }
477
+ if (min > max) {
478
+ throw new RangeError("[helper] randomFloat: min \u4E0D\u80FD\u5927\u4E8E max");
479
+ }
480
+ assertPrecision(precision);
481
+ if (min === max) return min;
482
+ const span = max - min;
483
+ if (!Number.isFinite(span)) {
484
+ throw new RangeError("[helper] randomFloat: \u968F\u673A\u533A\u95F4\u8FC7\u5927");
485
+ }
486
+ const value = min + createRandomSource().fraction() * span;
487
+ return Math.min(Math.max(toRound(value, precision), min), max);
488
+ }
489
+
490
+ // src/function.ts
491
+ function normalizeDelay(delay, fallback) {
492
+ if (!Number.isFinite(delay)) return fallback;
493
+ return delay < 0 ? 0 : delay;
494
+ }
495
+ function debounce(fn, delay = 150) {
496
+ const wait = normalizeDelay(delay, 150);
497
+ let timer;
498
+ return function(...args) {
499
+ if (timer !== void 0) clearTimeout(timer);
500
+ timer = setTimeout(() => {
501
+ timer = void 0;
502
+ fn.apply(this, args);
503
+ }, wait);
504
+ };
505
+ }
506
+ function throttle(fn, delay = 500) {
507
+ const wait = normalizeDelay(delay, 500);
508
+ let start = 0;
509
+ let timer;
510
+ return function(...args) {
511
+ const now = Date.now();
512
+ if (now - start < wait) {
513
+ if (timer !== void 0) clearTimeout(timer);
514
+ timer = setTimeout(() => {
515
+ start = Date.now();
516
+ timer = void 0;
517
+ fn.apply(this, args);
518
+ }, wait);
519
+ } else {
520
+ start = now;
521
+ fn.apply(this, args);
522
+ }
523
+ };
524
+ }
525
+
526
+ // src/convert.ts
527
+ var DEFAULT_TRUE_STRINGS = ["true", "1", "t", "on", "yes", "y"];
528
+ var DEFAULT_FALSE_STRINGS = ["false", "0", "f", "off", "no", "n"];
529
+ function toBool(value, trueStrings = DEFAULT_TRUE_STRINGS, falseStrings = DEFAULT_FALSE_STRINGS) {
530
+ if (typeof value === "boolean") return value;
531
+ if (!value) return false;
532
+ if (typeof value === "number") {
533
+ return value !== 0 && !Number.isNaN(value);
534
+ }
535
+ if (typeof value === "string") {
536
+ const normalized = value.trim().toLowerCase();
537
+ if (falseStrings.includes(normalized)) return false;
538
+ if (trueStrings.includes(normalized)) return true;
539
+ return true;
540
+ }
541
+ if (typeof value === "object" && value !== null) {
542
+ const valueOf = value.valueOf;
543
+ if (typeof valueOf === "function") {
544
+ const primitive = valueOf.call(value);
545
+ if (typeof primitive !== "object" && primitive !== value) {
546
+ return toBool(primitive, trueStrings, falseStrings);
547
+ }
548
+ }
549
+ return true;
550
+ }
551
+ return true;
552
+ }
553
+
554
+ // src/file.ts
555
+ var EXT_MIME_MAP = {
556
+ // 图片
557
+ jpg: "image/jpeg",
558
+ jpeg: "image/jpeg",
559
+ png: "image/png",
560
+ gif: "image/gif",
561
+ webp: "image/webp",
562
+ bmp: "image/bmp",
563
+ svg: "image/svg+xml",
564
+ ico: "image/x-icon",
565
+ avif: "image/avif",
566
+ tif: "image/tiff",
567
+ tiff: "image/tiff",
568
+ // 文本
569
+ txt: "text/plain",
570
+ csv: "text/csv",
571
+ html: "text/html",
572
+ htm: "text/html",
573
+ json: "application/json",
574
+ xml: "application/xml",
575
+ md: "text/markdown",
576
+ // 文档
577
+ pdf: "application/pdf",
578
+ doc: "application/msword",
579
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
580
+ xls: "application/vnd.ms-excel",
581
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
582
+ ppt: "application/vnd.ms-powerpoint",
583
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
584
+ // 音视频
585
+ mp3: "audio/mpeg",
586
+ wav: "audio/wav",
587
+ ogg: "audio/ogg",
588
+ m4a: "audio/mp4",
589
+ aac: "audio/aac",
590
+ mp4: "video/mp4",
591
+ webm: "video/webm",
592
+ mov: "video/quicktime",
593
+ avi: "video/x-msvideo",
594
+ mkv: "video/x-matroska",
595
+ flv: "video/x-flv",
596
+ // 压缩包
597
+ zip: "application/zip",
598
+ rar: "application/vnd.rar",
599
+ "7z": "application/x-7z-compressed",
600
+ gz: "application/gzip",
601
+ tar: "application/x-tar",
602
+ // 字体
603
+ ttf: "font/ttf",
604
+ otf: "font/otf",
605
+ woff: "font/woff",
606
+ woff2: "font/woff2",
607
+ // 其它
608
+ wasm: "application/wasm",
609
+ apk: "application/vnd.android.package-archive",
610
+ epub: "application/epub+zip"
611
+ };
612
+ function fileExtToMime(ext) {
613
+ if (typeof ext !== "string") return "";
614
+ const normalized = ext.trim().toLowerCase().replace(/^\./, "");
615
+ return EXT_MIME_MAP[normalized] ?? "";
616
+ }
617
+
618
+ export { assertArray, assertNumber, assertPrecision, assertRangeNumber, assertString, camelCase, capitalize, cleanString, cutString, debounce, deepClone, deepMerge, fileExtToMime, formatBytes, formatKW, formatMoney, formatMoneyUpper, inRange, isChinaIdCard, isChinaMobile, isChinaUSCC, isEmail, isEmpty, isHttpUrl, isNumeric, isPlainObject, kebabCase, maskSensitive, pascalCase, randomFloat, randomInt, randomInteger, randomString, rangeClamp, snakeCase, stripHtml, throttle, toBool, toCeil, toFixed, toFloor, toRound };
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@vueboot/helper",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.4.0",
4
+ "description": "零依赖工具包:验证 / 字符串 / 数字 / 对象 / 函数 / 转换 / 文件工具,支持 Tree-shaking",
5
+ "keywords": [
6
+ "typescript",
7
+ "helper",
8
+ "utils",
9
+ "validation",
10
+ "string",
11
+ "number",
12
+ "format",
13
+ "random",
14
+ "object",
15
+ "deep-clone",
16
+ "debounce",
17
+ "tree-shaking",
18
+ "zero-dependency"
19
+ ],
20
+ "homepage": "https://github.com/ygzhang-lab/vueboot/tree/main/packages/helper#readme",
21
+ "bugs": {
22
+ "url": "https://github.com/ygzhang-lab/vueboot/issues"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/ygzhang-lab/vueboot.git",
27
+ "directory": "packages/helper"
28
+ },
29
+ "author": "ygzhang-lab",
30
+ "license": "MIT",
31
+ "type": "module",
32
+ "sideEffects": false,
33
+ "types": "./dist/index.d.ts",
34
+ "main": "./dist/index.js",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js",
39
+ "default": "./dist/index.js"
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "files": [
44
+ "dist",
45
+ "README.md",
46
+ "API.md"
47
+ ],
48
+ "engines": {
49
+ "node": ">=18"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/"
54
+ },
55
+ "scripts": {
56
+ "build": "tsup",
57
+ "typecheck": "tsc --noEmit"
58
+ }
6
59
  }