@hz-9/a5-core 0.2.0-alpha.6 → 0.2.0-alpha.60
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/all.d.ts +270 -5
- package/dist/core/a5-application.d.ts +20 -1
- package/dist/core/a5-application.js +8 -3
- package/dist/core/a5-console-logger.js +5 -10
- package/dist/core/a5-factory.js +4 -1
- package/dist/errors/a5-load-package.error.d.ts +20 -0
- package/dist/errors/a5-load-package.error.js +23 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +18 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +2 -0
- package/dist/interface/json.d.ts +27 -0
- package/dist/interface/json.js +3 -0
- package/dist/interface/type-challenges.utils.d.ts +94 -0
- package/dist/interface/type-challenges.utils.js +4 -0
- package/dist/plugins/axios.d.ts +2 -0
- package/dist/plugins/axios.js +7 -0
- package/dist/plugins/dayjs.d.ts +2 -0
- package/dist/plugins/dayjs.js +7 -0
- package/dist/plugins/fs-extra.d.ts +2 -0
- package/dist/plugins/fs-extra.js +7 -0
- package/dist/plugins/ms.d.ts +2 -0
- package/dist/plugins/ms.js +7 -0
- package/dist/plugins/nanoid.d.ts +2 -1
- package/dist/plugins/nanoid.js +2 -4
- package/dist/plugins/pug.d.ts +2 -0
- package/dist/plugins/pug.js +7 -0
- package/dist/plugins/type-fest.d.ts +1 -0
- package/dist/plugins/{index.js → type-fest.js} +2 -2
- package/dist/plugins/ua-parser-js.d.ts +2 -0
- package/dist/plugins/ua-parser-js.js +7 -0
- package/dist/plugins/upath.d.ts +2 -0
- package/dist/plugins/upath.js +7 -0
- package/dist/plugins/uuid.d.ts +2 -0
- package/dist/plugins/uuid.js +27 -0
- package/dist/test/integration/core/a5-factory.integration.spec.js +2 -2
- package/dist/util/a5.util.d.ts +9 -0
- package/dist/util/a5.util.js +13 -0
- package/dist/util/index.d.ts +2 -0
- package/dist/util/index.js +2 -0
- package/dist/util/load-package.util.d.ts +2 -2
- package/dist/util/load-package.util.js +11 -7
- package/dist/util/ms.util.d.ts +47 -0
- package/dist/util/ms.util.js +66 -0
- package/dist/util/random.util.d.ts +27 -0
- package/dist/util/random.util.js +46 -0
- package/package.json +47 -8
- package/dist/plugins/index.d.ts +0 -1
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 类型断言工具 - 期望类型为 true
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type Expect<T extends true> = T;
|
|
7
|
+
/**
|
|
8
|
+
* 类型断言工具 - 期望值为 true
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export type ExpectTrue<T extends true> = T;
|
|
13
|
+
/**
|
|
14
|
+
* 类型断言工具 - 期望值为 false
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export type ExpectFalse<T extends false> = T;
|
|
19
|
+
/**
|
|
20
|
+
* 类型断言工具 - 判断是否为 true
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export type IsTrue<T extends true> = T;
|
|
25
|
+
/**
|
|
26
|
+
* 类型断言工具 - 判断是否为 false
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export type IsFalse<T extends false> = T;
|
|
31
|
+
/**
|
|
32
|
+
* 类型断言工具 - 判断两个类型不相等
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
|
|
37
|
+
/**
|
|
38
|
+
* 类型断言工具 - 判断两个类型相等
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
43
|
+
/**
|
|
44
|
+
* 类型断言工具 - 判断是否为 any 类型
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
49
|
+
/**
|
|
50
|
+
* 类型断言工具 - 判断不是 any 类型
|
|
51
|
+
*
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export type NotAny<T> = true extends IsAny<T> ? false : true;
|
|
55
|
+
/**
|
|
56
|
+
* 类型调试工具 - 展开类型定义
|
|
57
|
+
*
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export type Debug<T> = {
|
|
61
|
+
[K in keyof T]: T[K];
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* 类型合并工具 - 合并嵌套类型
|
|
65
|
+
*
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export type MergeInsertions<T> = T extends object ? {
|
|
69
|
+
[K in keyof T]: MergeInsertions<T[K]>;
|
|
70
|
+
} : T;
|
|
71
|
+
/**
|
|
72
|
+
* 类型断言工具 - 判断两个类型结构相似
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
export type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
|
|
77
|
+
/**
|
|
78
|
+
* 类型断言工具 - 期望类型扩展自目标类型
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
export type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false;
|
|
83
|
+
/**
|
|
84
|
+
* 类型断言工具 - 验证函数参数类型
|
|
85
|
+
*
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
export type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC> ? true : false;
|
|
89
|
+
/**
|
|
90
|
+
* 类型转换工具 - 联合类型转交叉类型
|
|
91
|
+
*
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
94
|
+
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const axios_1 = __importDefault(require("axios"));
|
|
6
|
+
module.exports = axios_1.default;
|
|
7
|
+
//# sourceMappingURL=axios.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
6
|
+
module.exports = dayjs_1.default;
|
|
7
|
+
//# sourceMappingURL=dayjs.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
6
|
+
module.exports = fs_extra_1.default;
|
|
7
|
+
//# sourceMappingURL=fs-extra.js.map
|
package/dist/plugins/nanoid.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { nanoid } from 'nanoid';
|
|
2
|
+
export = nanoid;
|
package/dist/plugins/nanoid.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
exports
|
|
4
|
-
var nanoid_1 = require("nanoid");
|
|
5
|
-
Object.defineProperty(exports, "nanoid", { enumerable: true, get: function () { return nanoid_1.nanoid; } });
|
|
2
|
+
const nanoid_1 = require("nanoid");
|
|
3
|
+
module.exports = nanoid_1.nanoid;
|
|
6
4
|
//# sourceMappingURL=nanoid.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const pug_1 = __importDefault(require("pug"));
|
|
6
|
+
module.exports = pug_1.default;
|
|
7
|
+
//# sourceMappingURL=pug.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'type-fest';
|
|
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("
|
|
18
|
-
//# sourceMappingURL=
|
|
17
|
+
__exportStar(require("type-fest"), exports);
|
|
18
|
+
//# sourceMappingURL=type-fest.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const ua_parser_js_1 = __importDefault(require("ua-parser-js"));
|
|
6
|
+
module.exports = ua_parser_js_1.default;
|
|
7
|
+
//# sourceMappingURL=ua-parser-js.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const upath_1 = __importDefault(require("upath"));
|
|
6
|
+
module.exports = upath_1.default;
|
|
7
|
+
//# sourceMappingURL=upath.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
const uuid = __importStar(require("uuid"));
|
|
26
|
+
module.exports = uuid;
|
|
27
|
+
//# sourceMappingURL=uuid.js.map
|
|
@@ -10,10 +10,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
/* eslint-disable @typescript-eslint/consistent-type-definitions */
|
|
13
|
-
const core_1 = require("../../../core");
|
|
14
|
-
const util_1 = require("../../../util");
|
|
15
13
|
const node_console_1 = __importDefault(require("node:console"));
|
|
16
14
|
const common_1 = require("@nestjs/common");
|
|
15
|
+
const core_1 = require("../../../core");
|
|
16
|
+
const util_1 = require("../../../util");
|
|
17
17
|
describe('A5Factory', () => {
|
|
18
18
|
describe('create', () => {
|
|
19
19
|
describe('printLogo', () => {
|
package/dist/util/a5.util.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingHttpHeaders } from 'node:http';
|
|
3
|
+
import { FastifyRequest } from '../interface/http';
|
|
1
4
|
/**
|
|
2
5
|
*
|
|
3
6
|
* @public
|
|
@@ -24,4 +27,10 @@ export declare class A5Util {
|
|
|
24
27
|
* @returns 资源路径的绝对路径。
|
|
25
28
|
*/
|
|
26
29
|
static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
|
|
30
|
+
static getReqIdFromRequest(req: FastifyRequest): string;
|
|
31
|
+
static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
|
|
32
|
+
static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
|
|
33
|
+
reqId: string;
|
|
34
|
+
reqHeaders: IncomingHttpHeaders;
|
|
35
|
+
};
|
|
27
36
|
}
|
package/dist/util/a5.util.js
CHANGED
|
@@ -36,6 +36,19 @@ class A5Util {
|
|
|
36
36
|
? (0, upath_1.normalize)(sourcePath)
|
|
37
37
|
: (0, upath_1.resolve)((0, upath_1.normalize)(basePath), (0, upath_1.normalize)(sourcePath));
|
|
38
38
|
}
|
|
39
|
+
static getReqIdFromRequest(req) {
|
|
40
|
+
return req.id;
|
|
41
|
+
}
|
|
42
|
+
static getReqHeadersFromRequest(req) {
|
|
43
|
+
// TODO 支持 HTTP2
|
|
44
|
+
return req.headers;
|
|
45
|
+
}
|
|
46
|
+
static getReqIdAndHeadersFromRequest(req) {
|
|
47
|
+
return {
|
|
48
|
+
reqId: req.id,
|
|
49
|
+
reqHeaders: req.headers,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
39
52
|
}
|
|
40
53
|
exports.A5Util = A5Util;
|
|
41
54
|
//# sourceMappingURL=a5.util.js.map
|
package/dist/util/index.d.ts
CHANGED
package/dist/util/index.js
CHANGED
|
@@ -18,5 +18,7 @@ __exportStar(require("./a5.util"), exports);
|
|
|
18
18
|
__exportStar(require("./color.util"), exports);
|
|
19
19
|
__exportStar(require("./load-package.util"), exports);
|
|
20
20
|
__exportStar(require("./logo.util"), exports);
|
|
21
|
+
__exportStar(require("./ms.util"), exports);
|
|
22
|
+
__exportStar(require("./random.util"), exports);
|
|
21
23
|
__exportStar(require("./run-env.util"), exports);
|
|
22
24
|
//# sourceMappingURL=index.js.map
|
|
@@ -10,13 +10,13 @@ export declare class LoadPackageUtil {
|
|
|
10
10
|
* @returns 加载的包模块
|
|
11
11
|
* @throws 当包加载失败时抛出错误
|
|
12
12
|
*/
|
|
13
|
-
static loadPackage<T = unknown>(packageName: string):
|
|
13
|
+
static loadPackage<T = unknown>(packageName: string): T;
|
|
14
14
|
/**
|
|
15
15
|
* 安全地动态加载包
|
|
16
16
|
* @param packageName - 包名
|
|
17
17
|
* @returns 加载的包模块,如果失败则返回 null
|
|
18
18
|
*/
|
|
19
|
-
static loadPackageSafe<T = unknown>(packageName: string):
|
|
19
|
+
static loadPackageSafe<T = unknown>(packageName: string): T | null;
|
|
20
20
|
/**
|
|
21
21
|
* 带重试的动态加载包
|
|
22
22
|
* @param packageName - 包名
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LoadPackageUtil = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
4
5
|
const a5_util_1 = require("./a5.util");
|
|
5
6
|
// TODO 当前文件,并未编写单元测试
|
|
6
7
|
/**
|
|
@@ -15,7 +16,7 @@ class LoadPackageUtil {
|
|
|
15
16
|
* @returns 加载的包模块
|
|
16
17
|
* @throws 当包加载失败时抛出错误
|
|
17
18
|
*/
|
|
18
|
-
static
|
|
19
|
+
static loadPackage(packageName) {
|
|
19
20
|
try {
|
|
20
21
|
// 使用 Dynimic_import 引入模块,仍会显示 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG 报错,
|
|
21
22
|
// 使用 require 进行动态引入;
|
|
@@ -25,8 +26,10 @@ class LoadPackageUtil {
|
|
|
25
26
|
return module;
|
|
26
27
|
}
|
|
27
28
|
catch (error) {
|
|
28
|
-
//
|
|
29
|
-
|
|
29
|
+
// 抛出 A5LoadPackageError 错误
|
|
30
|
+
const cause = error instanceof Error ? error : undefined;
|
|
31
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
32
|
+
throw new errors_1.A5LoadPackageError(packageName, `Failed to load package "${packageName}": ${message}`, cause);
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
35
|
/**
|
|
@@ -34,9 +37,9 @@ class LoadPackageUtil {
|
|
|
34
37
|
* @param packageName - 包名
|
|
35
38
|
* @returns 加载的包模块,如果失败则返回 null
|
|
36
39
|
*/
|
|
37
|
-
static
|
|
40
|
+
static loadPackageSafe(packageName) {
|
|
38
41
|
try {
|
|
39
|
-
return
|
|
42
|
+
return LoadPackageUtil.loadPackage(packageName);
|
|
40
43
|
}
|
|
41
44
|
catch {
|
|
42
45
|
return null;
|
|
@@ -54,7 +57,7 @@ class LoadPackageUtil {
|
|
|
54
57
|
let lastError = null;
|
|
55
58
|
for (let i = 0; i <= retries; i += 1) {
|
|
56
59
|
try {
|
|
57
|
-
return
|
|
60
|
+
return LoadPackageUtil.loadPackage(packageName);
|
|
58
61
|
}
|
|
59
62
|
catch (error) {
|
|
60
63
|
lastError = error instanceof Error ? error : new Error(String(error));
|
|
@@ -64,7 +67,8 @@ class LoadPackageUtil {
|
|
|
64
67
|
}
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
|
-
throw lastError ||
|
|
70
|
+
throw (lastError ||
|
|
71
|
+
new errors_1.A5LoadPackageError(packageName, `Failed to load package "${packageName}" after ${retries + 1} attempts`));
|
|
68
72
|
}
|
|
69
73
|
}
|
|
70
74
|
exports.LoadPackageUtil = LoadPackageUtil;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import ms from 'ms';
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export type MSStringValue = ms.StringValue;
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare class MSUtil {
|
|
10
|
+
/**
|
|
11
|
+
* 将 ms 时间字符串转换为毫秒数
|
|
12
|
+
*
|
|
13
|
+
* @param value - ms 时间字符串
|
|
14
|
+
* @returns 毫秒数
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* MSUtil.ms('123') // 123
|
|
19
|
+
* MSUtil.ms('2h') // 7200000
|
|
20
|
+
* MSUtil.ms('30s') // 30000
|
|
21
|
+
* MSUtil.ms('123 ms') // 123
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
static ms(value: ms.StringValue): number;
|
|
25
|
+
/**
|
|
26
|
+
* 判断字符串是否是有效的 ms 时间字符串格式
|
|
27
|
+
* 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
|
|
28
|
+
*
|
|
29
|
+
* @param value - 要检查的字符串值
|
|
30
|
+
* @returns 如果是有效的 ms 字符串返回 true,否则返回 false
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // 有效格式
|
|
35
|
+
* MSUtil.isMsStringValue('123') // true
|
|
36
|
+
* MSUtil.isMsStringValue('2h') // true
|
|
37
|
+
* MSUtil.isMsStringValue('30s') // true
|
|
38
|
+
* MSUtil.isMsStringValue('123 ms') // true
|
|
39
|
+
*
|
|
40
|
+
* // 无效格式
|
|
41
|
+
* MSUtil.isMsStringValue('abc') // false
|
|
42
|
+
* MSUtil.isMsStringValue('') // false
|
|
43
|
+
* MSUtil.isMsStringValue('123xx') // false
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
static isMsStringValue(value: string): value is ms.StringValue;
|
|
47
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MSUtil = void 0;
|
|
7
|
+
const ms_1 = __importDefault(require("ms"));
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
class MSUtil {
|
|
12
|
+
/**
|
|
13
|
+
* 将 ms 时间字符串转换为毫秒数
|
|
14
|
+
*
|
|
15
|
+
* @param value - ms 时间字符串
|
|
16
|
+
* @returns 毫秒数
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* MSUtil.ms('123') // 123
|
|
21
|
+
* MSUtil.ms('2h') // 7200000
|
|
22
|
+
* MSUtil.ms('30s') // 30000
|
|
23
|
+
* MSUtil.ms('123 ms') // 123
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
static ms(value) {
|
|
27
|
+
return (0, ms_1.default)(value);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 判断字符串是否是有效的 ms 时间字符串格式
|
|
31
|
+
* 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
|
|
32
|
+
*
|
|
33
|
+
* @param value - 要检查的字符串值
|
|
34
|
+
* @returns 如果是有效的 ms 字符串返回 true,否则返回 false
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // 有效格式
|
|
39
|
+
* MSUtil.isMsStringValue('123') // true
|
|
40
|
+
* MSUtil.isMsStringValue('2h') // true
|
|
41
|
+
* MSUtil.isMsStringValue('30s') // true
|
|
42
|
+
* MSUtil.isMsStringValue('123 ms') // true
|
|
43
|
+
*
|
|
44
|
+
* // 无效格式
|
|
45
|
+
* MSUtil.isMsStringValue('abc') // false
|
|
46
|
+
* MSUtil.isMsStringValue('') // false
|
|
47
|
+
* MSUtil.isMsStringValue('123xx') // false
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
static isMsStringValue(value) {
|
|
51
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
// 尝试转换,如果 ms() 函数能成功解析则说明格式正确
|
|
56
|
+
// 使用类型断言因为我们正在验证这个值
|
|
57
|
+
const result = (0, ms_1.default)(value);
|
|
58
|
+
return typeof result === 'number' && Number.isNaN(result) === false;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.MSUtil = MSUtil;
|
|
66
|
+
//# sourceMappingURL=ms.util.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { V4Options, V7Options } from 'uuid';
|
|
2
|
+
/**
|
|
3
|
+
* A5 随机工具类
|
|
4
|
+
*
|
|
5
|
+
* 提供生成随机值和 UUID 的静态方法
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare class A5RandomUtil {
|
|
10
|
+
static nanoid(size?: number): string;
|
|
11
|
+
static uuidV4(options?: V4Options): string;
|
|
12
|
+
static uuidV7(options?: V7Options): string;
|
|
13
|
+
/**
|
|
14
|
+
* 生成指定位数的随机数字
|
|
15
|
+
*
|
|
16
|
+
* @param digits - 位数,默认 6
|
|
17
|
+
* @returns 随机数字字符串
|
|
18
|
+
*/
|
|
19
|
+
static randomDigits(digits?: number): string;
|
|
20
|
+
/**
|
|
21
|
+
* 生成指定长度的随机字符串(a-zA-Z0-9)
|
|
22
|
+
*
|
|
23
|
+
* @param length - 长度,默认 8
|
|
24
|
+
* @returns 随机字符串
|
|
25
|
+
*/
|
|
26
|
+
static randomString(length?: number, chars?: string): string;
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.A5RandomUtil = void 0;
|
|
4
|
+
const nanoid_1 = require("nanoid");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
/**
|
|
7
|
+
* A5 随机工具类
|
|
8
|
+
*
|
|
9
|
+
* 提供生成随机值和 UUID 的静态方法
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
class A5RandomUtil {
|
|
14
|
+
static nanoid(size) {
|
|
15
|
+
return (0, nanoid_1.nanoid)(size);
|
|
16
|
+
}
|
|
17
|
+
static uuidV4(options) {
|
|
18
|
+
return (0, uuid_1.v4)(options);
|
|
19
|
+
}
|
|
20
|
+
static uuidV7(options) {
|
|
21
|
+
return (0, uuid_1.v7)(options);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 生成指定位数的随机数字
|
|
25
|
+
*
|
|
26
|
+
* @param digits - 位数,默认 6
|
|
27
|
+
* @returns 随机数字字符串
|
|
28
|
+
*/
|
|
29
|
+
static randomDigits(digits = 6) {
|
|
30
|
+
const min = 10 ** (digits - 1);
|
|
31
|
+
const max = 10 ** digits - 1;
|
|
32
|
+
const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
33
|
+
return randomNum.toString();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 生成指定长度的随机字符串(a-zA-Z0-9)
|
|
37
|
+
*
|
|
38
|
+
* @param length - 长度,默认 8
|
|
39
|
+
* @returns 随机字符串
|
|
40
|
+
*/
|
|
41
|
+
static randomString(length = 8, chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
|
|
42
|
+
return (0, nanoid_1.customAlphabet)(chars)(length);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.A5RandomUtil = A5RandomUtil;
|
|
46
|
+
//# sourceMappingURL=random.util.js.map
|