ziya-utils 1.1.1 → 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.
- package/README.md +35 -13
- package/README.zh-CN.md +42 -0
- package/dist/ziya-utils.js +1 -1
- package/es/async/index.d.ts +20 -0
- package/es/count/index.d.ts +25 -0
- package/es/count/index.js +30 -0
- package/es/date/index.d.ts +10 -2
- package/es/date/index.js +66 -19
- package/es/document/index.d.ts +7 -0
- package/es/file/index.d.ts +1 -1
- package/es/file/index.js +50 -58
- package/es/form/index.d.ts +15 -0
- package/es/form/index.js +51 -0
- package/es/index.d.ts +3 -0
- package/es/index.js +5 -2
- package/es/loger/index.d.ts +7 -0
- package/es/node_modules/tslib/tslib.es6.js +1 -43
- package/es/regexp/index.d.ts +1 -1
- package/es/regexp/index.js +20 -30
- package/es/security/index.d.ts +11 -3
- package/es/security/index.js +41 -4
- package/es/time/index.d.ts +1 -1
- package/es/time/index.js +4 -4
- package/es/xhr/index.d.ts +74 -0
- package/es/xhr/index.js +172 -0
- package/lib/async/index.d.ts +20 -0
- package/lib/count/index.d.ts +25 -0
- package/lib/count/index.js +32 -0
- package/lib/date/index.d.ts +10 -2
- package/lib/date/index.js +66 -18
- package/lib/document/index.d.ts +7 -0
- package/lib/file/index.d.ts +1 -1
- package/lib/file/index.js +49 -57
- package/lib/form/index.d.ts +15 -0
- package/lib/form/index.js +53 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +9 -0
- package/lib/loger/index.d.ts +7 -0
- package/lib/node_modules/tslib/tslib.es6.js +0 -44
- package/lib/regexp/index.d.ts +1 -1
- package/lib/regexp/index.js +20 -30
- package/lib/security/index.d.ts +11 -3
- package/lib/security/index.js +41 -3
- package/lib/time/index.d.ts +1 -1
- package/lib/time/index.js +4 -4
- package/lib/xhr/index.d.ts +74 -0
- package/lib/xhr/index.js +175 -0
- package/package.json +9 -3
|
@@ -14,20 +14,6 @@ PERFORMANCE OF THIS SOFTWARE.
|
|
|
14
14
|
***************************************************************************** */
|
|
15
15
|
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
16
16
|
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
17
|
|
|
32
18
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
19
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -39,37 +25,9 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
39
25
|
});
|
|
40
26
|
}
|
|
41
27
|
|
|
42
|
-
function __generator(thisArg, body) {
|
|
43
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
44
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
45
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
46
|
-
function step(op) {
|
|
47
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
48
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
49
|
-
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;
|
|
50
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
51
|
-
switch (op[0]) {
|
|
52
|
-
case 0: case 1: t = op; break;
|
|
53
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
54
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
55
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
56
|
-
default:
|
|
57
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
58
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
59
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
60
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
61
|
-
if (t[2]) _.ops.pop();
|
|
62
|
-
_.trys.pop(); continue;
|
|
63
|
-
}
|
|
64
|
-
op = body.call(thisArg, _);
|
|
65
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
66
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
28
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
71
29
|
var e = new Error(message);
|
|
72
30
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
73
31
|
};
|
|
74
32
|
|
|
75
|
-
export { __awaiter
|
|
33
|
+
export { __awaiter };
|
package/es/regexp/index.d.ts
CHANGED
package/es/regexp/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { __extends } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* @fileoverview 正则模块
|
|
5
|
-
* @
|
|
3
|
+
* @created 2025-04-02
|
|
6
4
|
* @author glk
|
|
7
5
|
*/
|
|
8
6
|
// 正则表达式常量
|
|
9
|
-
|
|
7
|
+
const RegexPatterns = {
|
|
10
8
|
/** 手机号码正则 */
|
|
11
9
|
PHONE: /^1[3-9]\d{9}$/,
|
|
12
10
|
/** 邮箱地址正则 */
|
|
@@ -23,39 +21,31 @@ var RegexPatterns = {
|
|
|
23
21
|
CHINESE: /^[\u4e00-\u9fa5]+$/
|
|
24
22
|
};
|
|
25
23
|
// 使用 `abstract class` 代替 `interface`,用于静态方法约束
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}());
|
|
31
|
-
var RegexValidator = /** @class */ (function (_super) {
|
|
32
|
-
__extends(RegexValidator, _super);
|
|
33
|
-
function RegexValidator() {
|
|
34
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
35
|
-
}
|
|
36
|
-
RegexValidator.isPhone = function (phone) {
|
|
24
|
+
class IRegexValidatorStatic {
|
|
25
|
+
}
|
|
26
|
+
class RegexValidator extends IRegexValidatorStatic {
|
|
27
|
+
static isPhone(phone) {
|
|
37
28
|
return RegexPatterns.PHONE.test(phone);
|
|
38
|
-
}
|
|
39
|
-
|
|
29
|
+
}
|
|
30
|
+
static isEmail(email) {
|
|
40
31
|
return RegexPatterns.EMAIL.test(email);
|
|
41
|
-
}
|
|
42
|
-
|
|
32
|
+
}
|
|
33
|
+
static isIdCard(idCard) {
|
|
43
34
|
return RegexPatterns.ID_CARD.test(idCard);
|
|
44
|
-
}
|
|
45
|
-
|
|
35
|
+
}
|
|
36
|
+
static isUrl(url) {
|
|
46
37
|
return RegexPatterns.URL.test(url);
|
|
47
|
-
}
|
|
48
|
-
|
|
38
|
+
}
|
|
39
|
+
static isIpv4(ip) {
|
|
49
40
|
return RegexPatterns.IPV4.test(ip);
|
|
50
|
-
}
|
|
51
|
-
|
|
41
|
+
}
|
|
42
|
+
static isStrongPassword(password) {
|
|
52
43
|
return RegexPatterns.STRONG_PASSWORD.test(password);
|
|
53
|
-
}
|
|
54
|
-
|
|
44
|
+
}
|
|
45
|
+
static isChinese(text) {
|
|
55
46
|
return RegexPatterns.CHINESE.test(text);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
}(IRegexValidatorStatic));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
59
49
|
// 默认导出
|
|
60
50
|
var IRegexUtils = {
|
|
61
51
|
patterns: RegexPatterns,
|
package/es/security/index.d.ts
CHANGED
|
@@ -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
|
|
9
|
-
|
|
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 };
|
package/es/security/index.js
CHANGED
|
@@ -1,15 +1,52 @@
|
|
|
1
|
+
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
4
|
+
* @fileoverview 安全工具模块
|
|
5
|
+
* @created 2025-04-2
|
|
6
|
+
* @author glk
|
|
3
7
|
*/
|
|
4
8
|
/**
|
|
5
9
|
* 生成通用唯一识别码(UUID)
|
|
6
10
|
* @returns
|
|
7
11
|
*/
|
|
8
|
-
|
|
12
|
+
function getUuid() {
|
|
9
13
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
10
14
|
var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
11
15
|
return v.toString(16);
|
|
12
16
|
});
|
|
13
|
-
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 复制内容到剪贴板
|
|
20
|
+
* @param data 要复制的数据
|
|
21
|
+
* @returns 返回是否成功
|
|
22
|
+
*/
|
|
23
|
+
function copyToClipboard(data) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
try {
|
|
26
|
+
// 转换为JSON字符串
|
|
27
|
+
var jsonString = JSON.stringify(data);
|
|
28
|
+
// 优先方案
|
|
29
|
+
if (navigator.clipboard && (window.isSecureContext || location.protocol === 'https:' || location.hostname === 'localhost')) {
|
|
30
|
+
yield navigator.clipboard.writeText(jsonString);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
// 降级方案
|
|
34
|
+
var textArea = document.createElement('textarea');
|
|
35
|
+
textArea.value = jsonString;
|
|
36
|
+
textArea.style.position = 'fixed';
|
|
37
|
+
textArea.style.left = '-9999px';
|
|
38
|
+
textArea.style.top = '-9999px';
|
|
39
|
+
document.body.appendChild(textArea);
|
|
40
|
+
textArea.select();
|
|
41
|
+
var successful = document.execCommand('copy');
|
|
42
|
+
document.body.removeChild(textArea);
|
|
43
|
+
return successful;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.error('复制失败:', error);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
14
51
|
|
|
15
|
-
export { getUuid };
|
|
52
|
+
export { copyToClipboard, getUuid };
|
package/es/time/index.d.ts
CHANGED
package/es/time/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview 时间模块
|
|
3
|
-
* @
|
|
3
|
+
* @created 2025-04-02
|
|
4
4
|
* @author glk
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
* @param time - 休眠时间(s)
|
|
9
9
|
* @returns
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
return new Promise(
|
|
13
|
-
setTimeout(
|
|
11
|
+
const sleep = (time) => {
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
setTimeout(() => {
|
|
14
14
|
resolve(true);
|
|
15
15
|
}, time * 1000);
|
|
16
16
|
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview XMLHttpRequest模块
|
|
3
|
+
* @created 2025-04-02
|
|
4
|
+
* @author glk
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 拦截规则
|
|
8
|
+
*/
|
|
9
|
+
interface InterceptRule {
|
|
10
|
+
url: string;
|
|
11
|
+
responseCallback: (data: any) => any | Promise<any>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 拦截器配置
|
|
15
|
+
*/
|
|
16
|
+
interface XHRInterceptorConfig {
|
|
17
|
+
enableLogging?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* XHR 拦截器类
|
|
21
|
+
*/
|
|
22
|
+
declare class XHRInterceptor {
|
|
23
|
+
private originalXMLHttpRequest;
|
|
24
|
+
private interceptRules;
|
|
25
|
+
private isActive;
|
|
26
|
+
private enableLogging;
|
|
27
|
+
constructor(config?: XHRInterceptorConfig);
|
|
28
|
+
/**
|
|
29
|
+
* 启动拦截器
|
|
30
|
+
*/
|
|
31
|
+
start(): void;
|
|
32
|
+
/**
|
|
33
|
+
* 停止拦截器并恢复原始XMLHttpRequest
|
|
34
|
+
*/
|
|
35
|
+
stop(): void;
|
|
36
|
+
/**
|
|
37
|
+
* 设置拦截规则(会覆盖现有规则)
|
|
38
|
+
*/
|
|
39
|
+
setRules(rules: InterceptRule[]): void;
|
|
40
|
+
/**
|
|
41
|
+
* 添加单个拦截规则
|
|
42
|
+
*/
|
|
43
|
+
addRule(url: string, responseCallback: InterceptRule["responseCallback"]): void;
|
|
44
|
+
/**
|
|
45
|
+
* 移除指定URL的拦截规则
|
|
46
|
+
*/
|
|
47
|
+
removeRule(url: string): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* 清除所有拦截规则
|
|
50
|
+
*/
|
|
51
|
+
clearRules(): void;
|
|
52
|
+
/**
|
|
53
|
+
* 获取当前拦截规则
|
|
54
|
+
*/
|
|
55
|
+
getRules(): InterceptRule[];
|
|
56
|
+
/**
|
|
57
|
+
* 获取拦截器状态
|
|
58
|
+
*/
|
|
59
|
+
getStatus(): {
|
|
60
|
+
isActive: boolean;
|
|
61
|
+
rulesCount: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* 检查URL是否匹配拦截规则
|
|
65
|
+
*/
|
|
66
|
+
private findMatchingRule;
|
|
67
|
+
/**
|
|
68
|
+
* 日志输出
|
|
69
|
+
*/
|
|
70
|
+
private log;
|
|
71
|
+
}
|
|
72
|
+
declare const xhrInterceptor: XHRInterceptor;
|
|
73
|
+
export type { InterceptRule, XHRInterceptorConfig };
|
|
74
|
+
export { XHRInterceptor, xhrInterceptor };
|
package/es/xhr/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview XMLHttpRequest模块
|
|
5
|
+
* @created 2025-04-02
|
|
6
|
+
* @author glk
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* XHR 拦截器类
|
|
10
|
+
*/
|
|
11
|
+
class XHRInterceptor {
|
|
12
|
+
constructor(config = {}) {
|
|
13
|
+
var _a;
|
|
14
|
+
this.interceptRules = [];
|
|
15
|
+
this.isActive = false;
|
|
16
|
+
this.originalXMLHttpRequest = window.XMLHttpRequest;
|
|
17
|
+
this.enableLogging = (_a = config.enableLogging) !== null && _a !== void 0 ? _a : true;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 启动拦截器
|
|
21
|
+
*/
|
|
22
|
+
start() {
|
|
23
|
+
if (this.isActive) {
|
|
24
|
+
this.log("拦截器已经启动");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const self = this;
|
|
28
|
+
// 重写 XMLHttpRequest
|
|
29
|
+
window.XMLHttpRequest = function () {
|
|
30
|
+
const xhr = new self.originalXMLHttpRequest();
|
|
31
|
+
let requestUrl = "";
|
|
32
|
+
// 保存原始的 open 方法
|
|
33
|
+
const originalOpen = xhr.open;
|
|
34
|
+
xhr.open = function (method, url, async = true, user, password) {
|
|
35
|
+
requestUrl = typeof url === "string" ? url : url.toString();
|
|
36
|
+
return originalOpen.call(this, method, url, async, user, password);
|
|
37
|
+
};
|
|
38
|
+
// 保存原始的 send 方法
|
|
39
|
+
const originalSend = xhr.send;
|
|
40
|
+
xhr.send = function (body) {
|
|
41
|
+
// 查找匹配的拦截规则
|
|
42
|
+
const matchingRule = self.findMatchingRule(requestUrl);
|
|
43
|
+
if (!matchingRule) {
|
|
44
|
+
// 没有匹配的规则,直接执行原始请求
|
|
45
|
+
return originalSend.call(this, body);
|
|
46
|
+
}
|
|
47
|
+
// 保存原始的 onreadystatechange
|
|
48
|
+
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
49
|
+
xhr.onreadystatechange = function (event) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
|
|
52
|
+
try {
|
|
53
|
+
// 解析原始响应
|
|
54
|
+
const originalData = JSON.parse(xhr.responseText);
|
|
55
|
+
// 使用回调函数处理响应数据
|
|
56
|
+
const modifiedData = yield matchingRule.responseCallback(originalData);
|
|
57
|
+
// 重写响应属性
|
|
58
|
+
Object.defineProperty(xhr, "responseText", {
|
|
59
|
+
writable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
value: JSON.stringify(modifiedData),
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(xhr, "response", {
|
|
64
|
+
writable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
value: JSON.stringify(modifiedData),
|
|
67
|
+
});
|
|
68
|
+
self.log(`已拦截并修改响应: ${requestUrl}`);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
self.log("拦截器处理失败:", error);
|
|
72
|
+
// 出错时保持原始响应
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// 调用原始的 onreadystatechange
|
|
76
|
+
if (originalOnReadyStateChange) {
|
|
77
|
+
originalOnReadyStateChange.call(this, event);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
return originalSend.call(this, body);
|
|
82
|
+
};
|
|
83
|
+
return xhr;
|
|
84
|
+
};
|
|
85
|
+
// 继承原始 XMLHttpRequest 的静态属性和原型
|
|
86
|
+
Object.setPrototypeOf(window.XMLHttpRequest, this.originalXMLHttpRequest);
|
|
87
|
+
window.XMLHttpRequest.prototype = this.originalXMLHttpRequest.prototype;
|
|
88
|
+
this.isActive = true;
|
|
89
|
+
this.log("XHR拦截器已启动");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 停止拦截器并恢复原始XMLHttpRequest
|
|
93
|
+
*/
|
|
94
|
+
stop() {
|
|
95
|
+
if (!this.isActive) {
|
|
96
|
+
this.log("拦截器未启动");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
window.XMLHttpRequest = this.originalXMLHttpRequest;
|
|
100
|
+
this.isActive = false;
|
|
101
|
+
this.log("XHR拦截器已停止,已恢复原始XMLHttpRequest");
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 设置拦截规则(会覆盖现有规则)
|
|
105
|
+
*/
|
|
106
|
+
setRules(rules) {
|
|
107
|
+
this.interceptRules = [...rules];
|
|
108
|
+
this.log("已设置拦截规则:", rules.map((r) => r.url));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 添加单个拦截规则
|
|
112
|
+
*/
|
|
113
|
+
addRule(url, responseCallback) {
|
|
114
|
+
this.interceptRules.push({ url, responseCallback });
|
|
115
|
+
this.log(`已添加拦截规则: ${url}`);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 移除指定URL的拦截规则
|
|
119
|
+
*/
|
|
120
|
+
removeRule(url) {
|
|
121
|
+
const initialLength = this.interceptRules.length;
|
|
122
|
+
this.interceptRules = this.interceptRules.filter((rule) => rule.url !== url);
|
|
123
|
+
const removed = this.interceptRules.length < initialLength;
|
|
124
|
+
if (removed) {
|
|
125
|
+
this.log(`已移除拦截规则: ${url}`);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.log(`未找到拦截规则: ${url}`);
|
|
129
|
+
}
|
|
130
|
+
return removed;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 清除所有拦截规则
|
|
134
|
+
*/
|
|
135
|
+
clearRules() {
|
|
136
|
+
this.interceptRules = [];
|
|
137
|
+
this.log("已清除所有拦截规则");
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 获取当前拦截规则
|
|
141
|
+
*/
|
|
142
|
+
getRules() {
|
|
143
|
+
return [...this.interceptRules];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 获取拦截器状态
|
|
147
|
+
*/
|
|
148
|
+
getStatus() {
|
|
149
|
+
return {
|
|
150
|
+
isActive: this.isActive,
|
|
151
|
+
rulesCount: this.interceptRules.length,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 检查URL是否匹配拦截规则
|
|
156
|
+
*/
|
|
157
|
+
findMatchingRule(url) {
|
|
158
|
+
return this.interceptRules.find((rule) => url.includes(rule.url));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 日志输出
|
|
162
|
+
*/
|
|
163
|
+
log(message, ...args) {
|
|
164
|
+
if (this.enableLogging) {
|
|
165
|
+
console.log(`[XHRInterceptor] ${message}`, ...args);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// 创建单例实例
|
|
170
|
+
const xhrInterceptor = new XHRInterceptor({ enableLogging: true });
|
|
171
|
+
|
|
172
|
+
export { XHRInterceptor, xhrInterceptor };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type CheckFunction<T = any> = () => T | null | undefined | false;
|
|
2
|
+
type InitFunction<T = any, R = any> = (checkResult: T) => R | Promise<R>;
|
|
3
|
+
interface AsyncTaskOptions {
|
|
4
|
+
/** 检查间隔时间(秒),默认 1 秒 */
|
|
5
|
+
duration?: number;
|
|
6
|
+
/** 最大等待时间(秒),超时后会 reject */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
/** 是否立即执行一次检查,默认 false */
|
|
9
|
+
immediate?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 创建异步任务,定时检查条件直到满足后执行初始化函数
|
|
13
|
+
* @param checkFun 检查函数,返回 true 值时表示条件满足
|
|
14
|
+
* @param initFun 初始化函数,在条件满足后执行
|
|
15
|
+
* @param options 配置选项
|
|
16
|
+
* @returns Promise,resolve 初始化函数的返回值
|
|
17
|
+
*/
|
|
18
|
+
declare function createAsyncTask<T, R>(checkFun?: CheckFunction<T>, initFun?: InitFunction<T, R>, options?: AsyncTaskOptions): Promise<R>;
|
|
19
|
+
export { createAsyncTask };
|
|
20
|
+
export type { CheckFunction, InitFunction, AsyncTaskOptions };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview 数值模块
|
|
3
|
+
* @created 2025-04-2
|
|
4
|
+
* @author glk
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 数值的精度控制
|
|
8
|
+
* @param num 源数值,可以为负
|
|
9
|
+
* @param count 保留几位有效数字
|
|
10
|
+
* @param round 是否四舍五入
|
|
11
|
+
* @param consistent 是否保持一致性
|
|
12
|
+
* @example
|
|
13
|
+
* formatDecimals(0.15, 1, false, false) // 0.1
|
|
14
|
+
* formatDecimals(0.15, 1, false, true) // 0.1
|
|
15
|
+
* formatDecimals(0.15, 1, true, false) // 0.2
|
|
16
|
+
* formatDecimals(0.15, 1, true, true) // 0.2
|
|
17
|
+
*
|
|
18
|
+
* formatDecimals(-0.15, 1, false, true) // -0.1
|
|
19
|
+
* formatDecimals(-0.15, 1, false, false) // -0.2
|
|
20
|
+
* formatDecimals(-0.15, 1, true, true) // -0.2
|
|
21
|
+
* formatDecimals(-0.15, 1, true, false) // -0.1
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
declare function precisionFormat(num: number, count: number, round?: boolean, consistent?: boolean): number;
|
|
25
|
+
export { precisionFormat };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview 数值模块
|
|
5
|
+
* @created 2025-04-2
|
|
6
|
+
* @author glk
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 数值的精度控制
|
|
10
|
+
* @param num 源数值,可以为负
|
|
11
|
+
* @param count 保留几位有效数字
|
|
12
|
+
* @param round 是否四舍五入
|
|
13
|
+
* @param consistent 是否保持一致性
|
|
14
|
+
* @example
|
|
15
|
+
* formatDecimals(0.15, 1, false, false) // 0.1
|
|
16
|
+
* formatDecimals(0.15, 1, false, true) // 0.1
|
|
17
|
+
* formatDecimals(0.15, 1, true, false) // 0.2
|
|
18
|
+
* formatDecimals(0.15, 1, true, true) // 0.2
|
|
19
|
+
*
|
|
20
|
+
* formatDecimals(-0.15, 1, false, true) // -0.1
|
|
21
|
+
* formatDecimals(-0.15, 1, false, false) // -0.2
|
|
22
|
+
* formatDecimals(-0.15, 1, true, true) // -0.2
|
|
23
|
+
* formatDecimals(-0.15, 1, true, false) // -0.1
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
function precisionFormat(num, count, round = true, consistent = true) {
|
|
27
|
+
var sign = num < 0 ? (consistent ? -1 : 1) : 1;
|
|
28
|
+
const val = Math.pow(10, count);
|
|
29
|
+
return round ? (Math.round(num * val * sign) / val) * sign : (Math.floor(num * val * sign) / val) * sign;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.precisionFormat = precisionFormat;
|
package/lib/date/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview 日期模块
|
|
3
|
-
* @
|
|
3
|
+
* @created 2025-04-02
|
|
4
4
|
* @author glk
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
@@ -31,4 +31,12 @@ declare const getElapsedTimeSince: (timestamp: number) => {
|
|
|
31
31
|
minutes: number;
|
|
32
32
|
seconds: number;
|
|
33
33
|
};
|
|
34
|
-
|
|
34
|
+
type DateFormatTemplate = 'YYYY-MM-DD' | 'YYYY/MM/DD' | 'DD/MM/YYYY' | 'MM/DD/YYYY' | 'YYYY-MM-DD HH:mm:ss' | 'HH:mm:ss' | 'HH:mm' | string;
|
|
35
|
+
/**
|
|
36
|
+
* 类型安全的日期格式化函数
|
|
37
|
+
* @param format 格式字符串或预定义模板
|
|
38
|
+
* @param date 可选的日期对象
|
|
39
|
+
* @returns 格式化后的日期字符串
|
|
40
|
+
*/
|
|
41
|
+
declare function formatDateTypeSafe(format: DateFormatTemplate, date?: Date): string;
|
|
42
|
+
export { isValidTimestamp, isSecondTimestamp, isMillisecondTimestamp, getElapsedTimeSince, formatDateTypeSafe, };
|