@hz-9/a5-core 0.2.0-alpha.53 → 0.2.0-alpha.54

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 CHANGED
@@ -7,6 +7,7 @@ import { Logger } from '@nestjs/common';
7
7
  import { LoggerService } from '@nestjs/common/services/logger.service';
8
8
  import { LoggerService as LoggerService_2 } from '@nestjs/common';
9
9
  import { LogLevel } from '@nestjs/common/services/logger.service';
10
+ import ms from 'ms';
10
11
  import { NestFactoryStatic } from '@nestjs/core/nest-factory';
11
12
  import { NestFastifyApplication } from '@nestjs/platform-fastify';
12
13
  import { NestMiddleware } from '@nestjs/common';
@@ -796,6 +797,54 @@ export declare const MODULE_CONFIG_PATH_A5_CONFIG: <T extends string>(str: T) =>
796
797
  */
797
798
  export declare const MODULE_CONFIG_PATH_A5_LOG: <T extends string>(str: T) => `A5.log.${T}`;
798
799
 
800
+ /**
801
+ * @public
802
+ */
803
+ export declare type MSStringValue = ms.StringValue;
804
+
805
+ /**
806
+ * @public
807
+ */
808
+ export declare class MSUtil {
809
+ /**
810
+ * 将 ms 时间字符串转换为毫秒数
811
+ *
812
+ * @param value - ms 时间字符串
813
+ * @returns 毫秒数
814
+ *
815
+ * @example
816
+ * ```typescript
817
+ * MSUtil.ms('123') // 123
818
+ * MSUtil.ms('2h') // 7200000
819
+ * MSUtil.ms('30s') // 30000
820
+ * MSUtil.ms('123 ms') // 123
821
+ * ```
822
+ */
823
+ static ms(value: ms.StringValue): number;
824
+ /**
825
+ * 判断字符串是否是有效的 ms 时间字符串格式
826
+ * 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
827
+ *
828
+ * @param value - 要检查的字符串值
829
+ * @returns 如果是有效的 ms 字符串返回 true,否则返回 false
830
+ *
831
+ * @example
832
+ * ```typescript
833
+ * // 有效格式
834
+ * MSUtil.isMsStringValue('123') // true
835
+ * MSUtil.isMsStringValue('2h') // true
836
+ * MSUtil.isMsStringValue('30s') // true
837
+ * MSUtil.isMsStringValue('123 ms') // true
838
+ *
839
+ * // 无效格式
840
+ * MSUtil.isMsStringValue('abc') // false
841
+ * MSUtil.isMsStringValue('') // false
842
+ * MSUtil.isMsStringValue('123xx') // false
843
+ * ```
844
+ */
845
+ static isMsStringValue(value: string): value is ms.StringValue;
846
+ }
847
+
799
848
  /**
800
849
  * 类型断言工具 - 判断不是 any 类型
801
850
  *
@@ -2,5 +2,6 @@ export * from './a5.util';
2
2
  export * from './color.util';
3
3
  export * from './load-package.util';
4
4
  export * from './logo.util';
5
+ export * from './ms.util';
5
6
  export * from './random.util';
6
7
  export * from './run-env.util';
@@ -18,6 +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);
21
22
  __exportStar(require("./random.util"), exports);
22
23
  __exportStar(require("./run-env.util"), exports);
23
24
  //# sourceMappingURL=index.js.map
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hz-9/a5-core",
3
- "version": "0.2.0-alpha.53",
3
+ "version": "0.2.0-alpha.54",
4
4
  "description": "The core library for the `@hz-9/a5-*` series of repositories.",
5
5
  "keywords": [
6
6
  "nest",
@@ -53,7 +53,9 @@
53
53
  "type-fest": "~4.20.0",
54
54
  "upath": "~2.0.1",
55
55
  "ua-parser-js": "~1.0.38",
56
- "uuid": "^10.0.0"
56
+ "uuid": "^10.0.0",
57
+ "ms": "~2.1.3",
58
+ "@types/ms": "~2.1.0"
57
59
  },
58
60
  "devDependencies": {
59
61
  "@hz-9/eslint-config-airbnb-ts": "~0.6.0",