ziya-utils 1.1.2 → 1.1.3

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 (48) hide show
  1. package/README.md +35 -13
  2. package/README.zh-CN.md +42 -0
  3. package/dist/ziya-utils.js +1 -1
  4. package/es/async/index.d.ts +20 -0
  5. package/es/count/index.d.ts +25 -0
  6. package/es/count/index.js +30 -0
  7. package/es/date/index.d.ts +10 -2
  8. package/es/date/index.js +66 -19
  9. package/es/document/index.d.ts +7 -0
  10. package/es/file/index.d.ts +1 -1
  11. package/es/file/index.js +50 -58
  12. package/es/form/index.d.ts +15 -0
  13. package/es/form/index.js +51 -0
  14. package/es/index.d.ts +3 -0
  15. package/es/index.js +5 -2
  16. package/es/loger/index.d.ts +7 -0
  17. package/es/node_modules/tslib/tslib.es6.js +1 -43
  18. package/es/regexp/index.d.ts +1 -1
  19. package/es/regexp/index.js +20 -30
  20. package/es/security/index.d.ts +11 -3
  21. package/es/security/index.js +41 -4
  22. package/es/time/index.d.ts +1 -1
  23. package/es/time/index.js +4 -4
  24. package/es/xhr/index.d.ts +74 -0
  25. package/es/xhr/index.js +172 -0
  26. package/lib/async/index.d.ts +20 -0
  27. package/lib/count/index.d.ts +25 -0
  28. package/lib/count/index.js +32 -0
  29. package/lib/date/index.d.ts +10 -2
  30. package/lib/date/index.js +66 -18
  31. package/lib/document/index.d.ts +7 -0
  32. package/lib/file/index.d.ts +1 -1
  33. package/lib/file/index.js +49 -57
  34. package/lib/form/index.d.ts +15 -0
  35. package/lib/form/index.js +53 -0
  36. package/lib/index.d.ts +3 -0
  37. package/lib/index.js +9 -0
  38. package/lib/loger/index.d.ts +7 -0
  39. package/lib/node_modules/tslib/tslib.es6.js +0 -44
  40. package/lib/regexp/index.d.ts +1 -1
  41. package/lib/regexp/index.js +20 -30
  42. package/lib/security/index.d.ts +11 -3
  43. package/lib/security/index.js +41 -3
  44. package/lib/time/index.d.ts +1 -1
  45. package/lib/time/index.js +4 -4
  46. package/lib/xhr/index.d.ts +74 -0
  47. package/lib/xhr/index.js +175 -0
  48. package/package.json +17 -3
package/lib/date/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @fileoverview 日期模块
5
- * @date 2025-04-02
5
+ * @created 2025-04-02
6
6
  * @author glk
7
7
  */
8
8
  /**
@@ -25,9 +25,9 @@ function isValidTimestamp(timestamp) {
25
25
  }
26
26
  // 检查时间戳范围是否合理
27
27
  // 时间戳下限:1970年1月1日
28
- var minTimestamp = 0;
28
+ const minTimestamp = 0;
29
29
  // 时间戳上限:设置为一个合理的未来日期,例如2100年1月1日
30
- var maxTimestamp = 4102444800000; // 2100-01-01的毫秒时间戳
30
+ const maxTimestamp = 4102444800000; // 2100-01-01的毫秒时间戳
31
31
  // 检查时间戳是否为13位(毫秒级)或10位(秒级)
32
32
  if (timestamp.toString().length === 13) {
33
33
  // 毫秒级时间戳
@@ -45,41 +45,89 @@ function isValidTimestamp(timestamp) {
45
45
  * @param timestamp
46
46
  * @returns
47
47
  */
48
- var isSecondTimestamp = function (timestamp) {
49
- return isValidTimestamp(timestamp) && timestamp.toString().length === 10;
50
- };
48
+ const isSecondTimestamp = (timestamp) => isValidTimestamp(timestamp) && timestamp.toString().length === 10;
51
49
  /**
52
50
  * 是不是毫秒级时间戳
53
51
  * @param timestamp
54
52
  * @returns
55
53
  */
56
- var isMillisecondTimestamp = function (timestamp) {
57
- return isValidTimestamp(timestamp) && timestamp.toString().length === 13;
58
- };
54
+ const isMillisecondTimestamp = (timestamp) => isValidTimestamp(timestamp) && timestamp.toString().length === 13;
59
55
  /**
60
56
  * 计算传入时间戳和当前时间的时间差
61
57
  * @param timestamp 时间戳
62
58
  * @returns
63
59
  */
64
- var getElapsedTimeSince = function (timestamp) {
65
- var now = Date.now();
60
+ const getElapsedTimeSince = (timestamp) => {
61
+ const now = Date.now();
66
62
  if (!isValidTimestamp(timestamp)) {
67
63
  timestamp = now;
68
64
  }
69
- var diff = Math.max(0, now - (isSecondTimestamp(timestamp) ? timestamp * 1000 : timestamp));
65
+ const diff = Math.max(0, now - (isSecondTimestamp(timestamp) ? timestamp * 1000 : timestamp));
70
66
  // 计算小时
71
- var hours = Math.floor(diff / (1000 * 60 * 60));
67
+ const hours = Math.floor(diff / (1000 * 60 * 60));
72
68
  // 计算分钟
73
- var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
69
+ const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
74
70
  // 计算秒
75
- var seconds = Math.floor((diff % (1000 * 60)) / 1000);
71
+ const seconds = Math.floor((diff % (1000 * 60)) / 1000);
76
72
  return {
77
- hours: hours,
78
- minutes: minutes,
79
- seconds: seconds,
73
+ hours,
74
+ minutes,
75
+ seconds,
80
76
  };
81
77
  };
78
+ /**
79
+ * 日期格式化
80
+ * @param format 格式字符串
81
+ * @param date 可选的日期对象,默认为当前时间
82
+ * @returns 格式化后的日期字符串
83
+ */
84
+ function formatDateAdvanced(format, date) {
85
+ const targetDate = date || new Date();
86
+ // 类型安全的数字格式化
87
+ const pad = (num, length = 2) => num.toString().padStart(length, "0");
88
+ // 获取各个时间组件
89
+ const components = {
90
+ year: targetDate.getFullYear(),
91
+ month: targetDate.getMonth() + 1,
92
+ day: targetDate.getDate(),
93
+ hours: targetDate.getHours(),
94
+ minutes: targetDate.getMinutes(),
95
+ seconds: targetDate.getSeconds(),
96
+ milliseconds: targetDate.getMilliseconds()
97
+ };
98
+ // 格式化映射表
99
+ const formatMap = {
100
+ 'YYYY': components.year.toString(),
101
+ 'YY': components.year.toString().slice(-2),
102
+ 'MM': pad(components.month),
103
+ 'M': components.month.toString(),
104
+ 'DD': pad(components.day),
105
+ 'D': components.day.toString(),
106
+ 'HH': pad(components.hours),
107
+ 'H': components.hours.toString(),
108
+ 'mm': pad(components.minutes),
109
+ 'm': components.minutes.toString(),
110
+ 'ss': pad(components.seconds),
111
+ 's': components.seconds.toString(),
112
+ 'SSS': pad(components.milliseconds, 3)
113
+ };
114
+ // 按照键的长度排序,避免短键替换长键的问题
115
+ const sortedKeys = Object.keys(formatMap).sort((a, b) => b.length - a.length);
116
+ return sortedKeys.reduce((result, key) => {
117
+ return result.replace(new RegExp(key, 'g'), formatMap[key]);
118
+ }, format);
119
+ }
120
+ /**
121
+ * 类型安全的日期格式化函数
122
+ * @param format 格式字符串或预定义模板
123
+ * @param date 可选的日期对象
124
+ * @returns 格式化后的日期字符串
125
+ */
126
+ function formatDateTypeSafe(format, date) {
127
+ return formatDateAdvanced(format, date);
128
+ }
82
129
 
130
+ exports.formatDateTypeSafe = formatDateTypeSafe;
83
131
  exports.getElapsedTimeSince = getElapsedTimeSince;
84
132
  exports.isMillisecondTimestamp = isMillisecondTimestamp;
85
133
  exports.isSecondTimestamp = isSecondTimestamp;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @fileoverview Document模块
3
+ * @created 2025-04-02
4
+ * @author glk
5
+ */
6
+ declare function addStyleStr(styStr?: string): HTMLStyleElement;
7
+ export { addStyleStr };
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @fileoverview 文件模块
3
- * @date 2025-04-2
3
+ * @created 2025-04-2
4
4
  * @author glk
5
5
  */
6
6
  /**
package/lib/file/index.js CHANGED
@@ -5,7 +5,7 @@ var index = require('../regexp/index.js');
5
5
 
6
6
  /**
7
7
  * @fileoverview 文件模块
8
- * @date 2025-04-2
8
+ * @created 2025-04-2
9
9
  * @author glk
10
10
  */
11
11
  /**
@@ -13,12 +13,12 @@ var index = require('../regexp/index.js');
13
13
  * @param filePath 文件路径字符串
14
14
  * @returns 提取的文件名,如果路径为空则返回空字符串,如果路径中没有分隔符则返回原字符串
15
15
  */
16
- var extractFileNameFromPath = function (filePath) {
16
+ const extractFileNameFromPath = (filePath) => {
17
17
  if (!filePath) {
18
18
  return "";
19
19
  }
20
20
  // 同时支持 Unix/Linux/Mac (/) 和 Windows (\) 风格的路径
21
- var lastSlashIndex = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
21
+ const lastSlashIndex = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
22
22
  // 如果找不到分隔符,则整个字符串就是文件名
23
23
  if (lastSlashIndex === -1) {
24
24
  return filePath;
@@ -32,61 +32,53 @@ var extractFileNameFromPath = function (filePath) {
32
32
  * @returns Promise<void>
33
33
  * @throws Error 当URL无效或网络请求失败时抛出错误
34
34
  */
35
- var downloadSingleFile = function (url, name) { return tslib_es6.__awaiter(void 0, void 0, void 0, function () {
36
- var response, fileName, queryIndex, blob, blobUrl_1, link_1, error_1;
37
- return tslib_es6.__generator(this, function (_a) {
38
- switch (_a.label) {
39
- case 0:
40
- // 验证URL
41
- if (!url || !index.default.validator.isUrl(url)) {
42
- throw new Error('无效的URL');
43
- }
44
- _a.label = 1;
45
- case 1:
46
- _a.trys.push([1, 4, , 5]);
47
- return [4 /*yield*/, fetch(url)];
48
- case 2:
49
- response = _a.sent();
50
- // 检查响应状态
51
- if (!response.ok) {
52
- throw new Error("\u4E0B\u8F7D\u5931\u8D25: ".concat(response.status, " ").concat(response.statusText));
53
- }
54
- fileName = name;
55
- if (!fileName) {
56
- fileName = extractFileNameFromPath(url);
57
- queryIndex = fileName.indexOf('?');
58
- if (queryIndex !== -1) {
59
- fileName = fileName.substring(0, queryIndex);
60
- }
61
- // 如果仍然没有文件名,使用默认名称
62
- if (!fileName) {
63
- fileName = 'downloaded_file';
64
- }
65
- }
66
- return [4 /*yield*/, response.blob()];
67
- case 3:
68
- blob = _a.sent();
69
- blobUrl_1 = URL.createObjectURL(blob);
70
- link_1 = document.createElement('a');
71
- link_1.href = blobUrl_1;
72
- link_1.download = fileName;
73
- // 执行下载
74
- document.body.appendChild(link_1);
75
- link_1.click();
76
- // 清理
77
- setTimeout(function () {
78
- URL.revokeObjectURL(blobUrl_1);
79
- link_1.remove();
80
- }, 100); // 添加小延迟以确保下载开始
81
- return [3 /*break*/, 5];
82
- case 4:
83
- error_1 = _a.sent();
84
- console.error('文件下载失败:', error_1);
85
- throw error_1; // 重新抛出错误以便调用者可以处理
86
- case 5: return [2 /*return*/];
35
+ const downloadSingleFile = (url, name) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
36
+ // 验证URL
37
+ if (!url || !index.default.validator.isUrl(url)) {
38
+ throw new Error('无效的URL');
39
+ }
40
+ try {
41
+ // 获取响应
42
+ const response = yield fetch(url);
43
+ // 检查响应状态
44
+ if (!response.ok) {
45
+ throw new Error(`下载失败: ${response.status} ${response.statusText}`);
46
+ }
47
+ // 获取文件名 - 优先使用提供的名称,否则从URL中提取
48
+ let fileName = name;
49
+ if (!fileName) {
50
+ fileName = extractFileNameFromPath(url);
51
+ // 处理查询参数
52
+ const queryIndex = fileName.indexOf('?');
53
+ if (queryIndex !== -1) {
54
+ fileName = fileName.substring(0, queryIndex);
55
+ }
56
+ // 如果仍然没有文件名,使用默认名称
57
+ if (!fileName) {
58
+ fileName = 'downloaded_file';
59
+ }
87
60
  }
88
- });
89
- }); };
61
+ // 获取blob和内容类型
62
+ const blob = yield response.blob();
63
+ // 创建下载链接
64
+ const blobUrl = URL.createObjectURL(blob); // 注意:直接使用原始blob保留MIME类型
65
+ const link = document.createElement('a');
66
+ link.href = blobUrl;
67
+ link.download = fileName;
68
+ // 执行下载
69
+ document.body.appendChild(link);
70
+ link.click();
71
+ // 清理
72
+ setTimeout(() => {
73
+ URL.revokeObjectURL(blobUrl);
74
+ link.remove();
75
+ }, 100); // 添加小延迟以确保下载开始
76
+ }
77
+ catch (error) {
78
+ console.error('文件下载失败:', error);
79
+ throw error; // 重新抛出错误以便调用者可以处理
80
+ }
81
+ });
90
82
 
91
83
  exports.downloadSingleFile = downloadSingleFile;
92
84
  exports.extractFileNameFromPath = extractFileNameFromPath;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @fileoverview 表单模块
3
+ * @created 2025-04-02
4
+ * @author glk
5
+ */
6
+ type FormDataValue = string | number | boolean | File | Blob | null | undefined;
7
+ type FormDataInput = string | Record<string, FormDataValue | FormDataValue[] | Record<string, any>>;
8
+ /**
9
+ * 将数据转换为 FormData 对象
10
+ * @param data 要转换的数据,可以是查询字符串或对象
11
+ * @returns FormData 对象
12
+ */
13
+ declare function createFormData(data: FormDataInput): FormData;
14
+ export { createFormData };
15
+ export type { FormDataInput, FormDataValue };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview 表单模块
5
+ * @created 2025-04-02
6
+ * @author glk
7
+ */
8
+ /**
9
+ * 将数据转换为 FormData 对象
10
+ * @param data 要转换的数据,可以是查询字符串或对象
11
+ * @returns FormData 对象
12
+ */
13
+ function createFormData(data) {
14
+ const formData = new FormData();
15
+ if (typeof data === "string") {
16
+ // 处理查询字符串
17
+ const params = new URLSearchParams(data);
18
+ for (const [key, value] of params) {
19
+ formData.append(key, value);
20
+ }
21
+ }
22
+ else if (typeof data === "object" && data !== null) {
23
+ // 处理对象
24
+ Object.keys(data).forEach((key) => {
25
+ const value = data[key];
26
+ if (value !== null && value !== undefined) {
27
+ if (Array.isArray(value)) {
28
+ // 数组:每个元素单独添加
29
+ value.forEach((item) => formData.append(key, String(item)));
30
+ }
31
+ else if (value instanceof File || value instanceof Blob) {
32
+ // 文件或Blob直接添加
33
+ formData.append(key, value);
34
+ }
35
+ else if (typeof value === "object") {
36
+ // 对象直接转字符串
37
+ formData.append(key, JSON.stringify(value));
38
+ }
39
+ else {
40
+ // 其他类型转为字符串
41
+ formData.append(key, String(value));
42
+ }
43
+ }
44
+ else {
45
+ // null 或 undefined 转为空字符串
46
+ formData.append(key, "");
47
+ }
48
+ });
49
+ }
50
+ return formData;
51
+ }
52
+
53
+ exports.createFormData = createFormData;
package/lib/index.d.ts CHANGED
@@ -3,3 +3,6 @@ export * from "./time";
3
3
  export * from "./date";
4
4
  export * from "./security";
5
5
  export * from "./regexp";
6
+ export * from "./form";
7
+ export * from "./count";
8
+ export * from "./xhr";
package/lib/index.js CHANGED
@@ -5,16 +5,25 @@ var index$1 = require('./time/index.js');
5
5
  var index$2 = require('./date/index.js');
6
6
  var index$3 = require('./security/index.js');
7
7
  var index$4 = require('./regexp/index.js');
8
+ var index$5 = require('./form/index.js');
9
+ var index$6 = require('./count/index.js');
10
+ var index$7 = require('./xhr/index.js');
8
11
 
9
12
 
10
13
 
11
14
  exports.downloadSingleFile = index.downloadSingleFile;
12
15
  exports.extractFileNameFromPath = index.extractFileNameFromPath;
13
16
  exports.sleep = index$1.sleep;
17
+ exports.formatDateTypeSafe = index$2.formatDateTypeSafe;
14
18
  exports.getElapsedTimeSince = index$2.getElapsedTimeSince;
15
19
  exports.isMillisecondTimestamp = index$2.isMillisecondTimestamp;
16
20
  exports.isSecondTimestamp = index$2.isSecondTimestamp;
17
21
  exports.isValidTimestamp = index$2.isValidTimestamp;
22
+ exports.copyToClipboard = index$3.copyToClipboard;
18
23
  exports.getUuid = index$3.getUuid;
19
24
  exports.RegexPatterns = index$4.RegexPatterns;
20
25
  exports.RegexValidator = index$4.RegexValidator;
26
+ exports.createFormData = index$5.createFormData;
27
+ exports.precisionFormat = index$6.precisionFormat;
28
+ exports.XHRInterceptor = index$7.XHRInterceptor;
29
+ exports.xhrInterceptor = index$7.xhrInterceptor;
@@ -0,0 +1,7 @@
1
+ declare const loger: {
2
+ info: (...args: any[]) => void;
3
+ error: (...args: any[]) => void;
4
+ warn: (...args: any[]) => void;
5
+ debug: (...args: any[]) => void;
6
+ };
7
+ export { loger };
@@ -16,20 +16,6 @@ PERFORMANCE OF THIS SOFTWARE.
16
16
  ***************************************************************************** */
17
17
  /* global Reflect, Promise, SuppressedError, Symbol */
18
18
 
19
- var extendStatics = function(d, b) {
20
- extendStatics = Object.setPrototypeOf ||
21
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
23
- return extendStatics(d, b);
24
- };
25
-
26
- function __extends(d, b) {
27
- if (typeof b !== "function" && b !== null)
28
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
29
- extendStatics(d, b);
30
- function __() { this.constructor = d; }
31
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
32
- }
33
19
 
34
20
  function __awaiter(thisArg, _arguments, P, generator) {
35
21
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -41,39 +27,9 @@ function __awaiter(thisArg, _arguments, P, generator) {
41
27
  });
42
28
  }
43
29
 
44
- function __generator(thisArg, body) {
45
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
46
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
- function verb(n) { return function (v) { return step([n, v]); }; }
48
- function step(op) {
49
- if (f) throw new TypeError("Generator is already executing.");
50
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
- if (y = 0, t) op = [op[0] & 2, t.value];
53
- switch (op[0]) {
54
- case 0: case 1: t = op; break;
55
- case 4: _.label++; return { value: op[1], done: false };
56
- case 5: _.label++; y = op[1]; op = [0]; continue;
57
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
- default:
59
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
- if (t[2]) _.ops.pop();
64
- _.trys.pop(); continue;
65
- }
66
- op = body.call(thisArg, _);
67
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
- }
70
- }
71
-
72
30
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
73
31
  var e = new Error(message);
74
32
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
75
33
  };
76
34
 
77
35
  exports.__awaiter = __awaiter;
78
- exports.__extends = __extends;
79
- exports.__generator = __generator;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @fileoverview 正则模块
3
- * @date 2025-04-02
3
+ * @created 2025-04-02
4
4
  * @author glk
5
5
  */
6
6
  /**
@@ -2,15 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
6
-
7
5
  /**
8
6
  * @fileoverview 正则模块
9
- * @date 2025-04-02
7
+ * @created 2025-04-02
10
8
  * @author glk
11
9
  */
12
10
  // 正则表达式常量
13
- var RegexPatterns = {
11
+ const RegexPatterns = {
14
12
  /** 手机号码正则 */
15
13
  PHONE: /^1[3-9]\d{9}$/,
16
14
  /** 邮箱地址正则 */
@@ -27,39 +25,31 @@ var RegexPatterns = {
27
25
  CHINESE: /^[\u4e00-\u9fa5]+$/
28
26
  };
29
27
  // 使用 `abstract class` 代替 `interface`,用于静态方法约束
30
- var IRegexValidatorStatic = /** @class */ (function () {
31
- function IRegexValidatorStatic() {
32
- }
33
- return IRegexValidatorStatic;
34
- }());
35
- var RegexValidator = /** @class */ (function (_super) {
36
- tslib_es6.__extends(RegexValidator, _super);
37
- function RegexValidator() {
38
- return _super !== null && _super.apply(this, arguments) || this;
39
- }
40
- RegexValidator.isPhone = function (phone) {
28
+ class IRegexValidatorStatic {
29
+ }
30
+ class RegexValidator extends IRegexValidatorStatic {
31
+ static isPhone(phone) {
41
32
  return RegexPatterns.PHONE.test(phone);
42
- };
43
- RegexValidator.isEmail = function (email) {
33
+ }
34
+ static isEmail(email) {
44
35
  return RegexPatterns.EMAIL.test(email);
45
- };
46
- RegexValidator.isIdCard = function (idCard) {
36
+ }
37
+ static isIdCard(idCard) {
47
38
  return RegexPatterns.ID_CARD.test(idCard);
48
- };
49
- RegexValidator.isUrl = function (url) {
39
+ }
40
+ static isUrl(url) {
50
41
  return RegexPatterns.URL.test(url);
51
- };
52
- RegexValidator.isIpv4 = function (ip) {
42
+ }
43
+ static isIpv4(ip) {
53
44
  return RegexPatterns.IPV4.test(ip);
54
- };
55
- RegexValidator.isStrongPassword = function (password) {
45
+ }
46
+ static isStrongPassword(password) {
56
47
  return RegexPatterns.STRONG_PASSWORD.test(password);
57
- };
58
- RegexValidator.isChinese = function (text) {
48
+ }
49
+ static isChinese(text) {
59
50
  return RegexPatterns.CHINESE.test(text);
60
- };
61
- return RegexValidator;
62
- }(IRegexValidatorStatic));
51
+ }
52
+ }
63
53
  // 默认导出
64
54
  var IRegexUtils = {
65
55
  patterns: RegexPatterns,
@@ -1,9 +1,17 @@
1
1
  /**
2
- * 通用安全工具
2
+ * @fileoverview 安全工具模块
3
+ * @created 2025-04-2
4
+ * @author glk
3
5
  */
4
6
  /**
5
7
  * 生成通用唯一识别码(UUID)
6
8
  * @returns
7
9
  */
8
- declare const getUuid: () => string;
9
- export { getUuid, };
10
+ declare function getUuid(): string;
11
+ /**
12
+ * 复制内容到剪贴板
13
+ * @param data 要复制的数据
14
+ * @returns 返回是否成功
15
+ */
16
+ declare function copyToClipboard(data: unknown): Promise<boolean>;
17
+ export { getUuid, copyToClipboard };
@@ -1,17 +1,55 @@
1
1
  'use strict';
2
2
 
3
+ var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
4
+
3
5
  /**
4
- * 通用安全工具
6
+ * @fileoverview 安全工具模块
7
+ * @created 2025-04-2
8
+ * @author glk
5
9
  */
6
10
  /**
7
11
  * 生成通用唯一识别码(UUID)
8
12
  * @returns
9
13
  */
10
- var getUuid = function () {
14
+ function getUuid() {
11
15
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
12
16
  var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
13
17
  return v.toString(16);
14
18
  });
15
- };
19
+ }
20
+ /**
21
+ * 复制内容到剪贴板
22
+ * @param data 要复制的数据
23
+ * @returns 返回是否成功
24
+ */
25
+ function copyToClipboard(data) {
26
+ return tslib_es6.__awaiter(this, void 0, void 0, function* () {
27
+ try {
28
+ // 转换为JSON字符串
29
+ var jsonString = JSON.stringify(data);
30
+ // 优先方案
31
+ if (navigator.clipboard && (window.isSecureContext || location.protocol === 'https:' || location.hostname === 'localhost')) {
32
+ yield navigator.clipboard.writeText(jsonString);
33
+ return true;
34
+ }
35
+ // 降级方案
36
+ var textArea = document.createElement('textarea');
37
+ textArea.value = jsonString;
38
+ textArea.style.position = 'fixed';
39
+ textArea.style.left = '-9999px';
40
+ textArea.style.top = '-9999px';
41
+ document.body.appendChild(textArea);
42
+ textArea.select();
43
+ var successful = document.execCommand('copy');
44
+ document.body.removeChild(textArea);
45
+ return successful;
46
+ }
47
+ catch (error) {
48
+ console.error('复制失败:', error);
49
+ return false;
50
+ }
51
+ });
52
+ }
16
53
 
54
+ exports.copyToClipboard = copyToClipboard;
17
55
  exports.getUuid = getUuid;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @fileoverview 时间模块
3
- * @date 2025-04-02
3
+ * @created 2025-04-02
4
4
  * @author glk
5
5
  */
6
6
  /**
package/lib/time/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @fileoverview 时间模块
5
- * @date 2025-04-02
5
+ * @created 2025-04-02
6
6
  * @author glk
7
7
  */
8
8
  /**
@@ -10,9 +10,9 @@
10
10
  * @param time - 休眠时间(s)
11
11
  * @returns
12
12
  */
13
- var sleep = function (time) {
14
- return new Promise(function (resolve) {
15
- setTimeout(function () {
13
+ const sleep = (time) => {
14
+ return new Promise((resolve) => {
15
+ setTimeout(() => {
16
16
  resolve(true);
17
17
  }, time * 1000);
18
18
  });