@zwa73/utils 1.0.35 → 1.0.37
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/UtilFunctions.d.ts +51 -41
- package/dist/UtilFunctions.js +91 -72
- package/package.json +1 -1
- package/src/UtilFunctions.ts +19 -0
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -1,42 +1,52 @@
|
|
|
1
1
|
import { JToken } from "./UtilInterfaces";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
2
|
+
/**常用函数 */
|
|
3
|
+
export declare namespace UtilFunc {
|
|
4
|
+
/**获取当前时间戳
|
|
5
|
+
* number ()
|
|
6
|
+
* @returns {number} 时间戳
|
|
7
|
+
*/
|
|
8
|
+
function getTime(): number;
|
|
9
|
+
/**初始化对象的字段
|
|
10
|
+
* void (Object,string,any)
|
|
11
|
+
* @param {Record<string,T>} obj - 所要初始化的对象
|
|
12
|
+
* @param {string} field - 所要初始化的字段
|
|
13
|
+
* @param {T} defaultVal - 默认值
|
|
14
|
+
* @returns {T} - 最终值
|
|
15
|
+
*/
|
|
16
|
+
function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
|
|
17
|
+
/**生成一串uuid
|
|
18
|
+
* string ()
|
|
19
|
+
* @returns {string} uuid
|
|
20
|
+
*/
|
|
21
|
+
function genUUID(): string;
|
|
22
|
+
/**计算Hash
|
|
23
|
+
* string ()
|
|
24
|
+
* @param {string} str - 待计算的字符串
|
|
25
|
+
* @returns {string} hash
|
|
26
|
+
*/
|
|
27
|
+
function calcHash(str: string): string;
|
|
28
|
+
/**深克隆 序列化并反序列化
|
|
29
|
+
* @template {T} T - JToken类型的泛型
|
|
30
|
+
* @param {T} obj - 克隆目标
|
|
31
|
+
* @returns {T} 克隆结果
|
|
32
|
+
*/
|
|
33
|
+
function deepClone<T extends JToken>(obj: T): T;
|
|
34
|
+
/**是否为安全的数字
|
|
35
|
+
* @param {number} num - 所要检测的数字
|
|
36
|
+
* @returns {boolean} 是否安全
|
|
37
|
+
*/
|
|
38
|
+
function isSafeNumber(num: number): boolean;
|
|
39
|
+
/**等待 timeMs 毫秒
|
|
40
|
+
* @async
|
|
41
|
+
* @param {number} timeMs - 等待的毫秒数
|
|
42
|
+
* @returns {Promise<boolean>}
|
|
43
|
+
*/
|
|
44
|
+
function sleep(timeMs: number): Promise<boolean>;
|
|
45
|
+
/**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
|
|
46
|
+
* @param {string} command 指令文本
|
|
47
|
+
*/
|
|
48
|
+
function exec(command: string): Promise<{
|
|
49
|
+
stdout: string;
|
|
50
|
+
stderr: string;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -1,76 +1,95 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilFunc = void 0;
|
|
4
4
|
const crypto = require("crypto");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
5
|
+
const cp = require("child_process");
|
|
6
|
+
/**常用函数 */
|
|
7
|
+
var UtilFunc;
|
|
8
|
+
(function (UtilFunc) {
|
|
9
|
+
/**获取当前时间戳
|
|
10
|
+
* number ()
|
|
11
|
+
* @returns {number} 时间戳
|
|
12
|
+
*/
|
|
13
|
+
function getTime() {
|
|
14
|
+
return new Date().getTime();
|
|
15
|
+
}
|
|
16
|
+
UtilFunc.getTime = getTime;
|
|
17
|
+
/**初始化对象的字段
|
|
18
|
+
* void (Object,string,any)
|
|
19
|
+
* @param {Record<string,T>} obj - 所要初始化的对象
|
|
20
|
+
* @param {string} field - 所要初始化的字段
|
|
21
|
+
* @param {T} defaultVal - 默认值
|
|
22
|
+
* @returns {T} - 最终值
|
|
23
|
+
*/
|
|
24
|
+
function initField(obj, field, defaultVal) {
|
|
25
|
+
if (!(field in obj))
|
|
26
|
+
obj[field] = defaultVal;
|
|
27
|
+
return obj[field];
|
|
28
|
+
}
|
|
29
|
+
UtilFunc.initField = initField;
|
|
30
|
+
/**生成一串uuid
|
|
31
|
+
* string ()
|
|
32
|
+
* @returns {string} uuid
|
|
33
|
+
*/
|
|
34
|
+
function genUUID() {
|
|
35
|
+
return crypto.randomBytes(16).toString("hex");
|
|
36
|
+
}
|
|
37
|
+
UtilFunc.genUUID = genUUID;
|
|
38
|
+
/**计算Hash
|
|
39
|
+
* string ()
|
|
40
|
+
* @param {string} str - 待计算的字符串
|
|
41
|
+
* @returns {string} hash
|
|
42
|
+
*/
|
|
43
|
+
function calcHash(str) {
|
|
44
|
+
return crypto.createHash('md5').update(str).digest('hex');
|
|
45
|
+
}
|
|
46
|
+
UtilFunc.calcHash = calcHash;
|
|
47
|
+
/**深克隆 序列化并反序列化
|
|
48
|
+
* @template {T} T - JToken类型的泛型
|
|
49
|
+
* @param {T} obj - 克隆目标
|
|
50
|
+
* @returns {T} 克隆结果
|
|
51
|
+
*/
|
|
52
|
+
function deepClone(obj) {
|
|
53
|
+
return JSON.parse(JSON.stringify(obj));
|
|
54
|
+
}
|
|
55
|
+
UtilFunc.deepClone = deepClone;
|
|
56
|
+
/**是否为安全的数字
|
|
57
|
+
* @param {number} num - 所要检测的数字
|
|
58
|
+
* @returns {boolean} 是否安全
|
|
59
|
+
*/
|
|
60
|
+
function isSafeNumber(num) {
|
|
61
|
+
if (num === undefined || num == null || isNaN(num))
|
|
62
|
+
return false;
|
|
63
|
+
if (typeof num === 'number')
|
|
64
|
+
return true;
|
|
58
65
|
return false;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
66
|
+
}
|
|
67
|
+
UtilFunc.isSafeNumber = isSafeNumber;
|
|
68
|
+
/**等待 timeMs 毫秒
|
|
69
|
+
* @async
|
|
70
|
+
* @param {number} timeMs - 等待的毫秒数
|
|
71
|
+
* @returns {Promise<boolean>}
|
|
72
|
+
*/
|
|
73
|
+
async function sleep(timeMs) {
|
|
74
|
+
return new Promise(function (resolve, rejecte) {
|
|
75
|
+
let timer = setTimeout(function () {
|
|
76
|
+
resolve(true);
|
|
77
|
+
}, timeMs);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
UtilFunc.sleep = sleep;
|
|
81
|
+
/**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
|
|
82
|
+
* @param {string} command 指令文本
|
|
83
|
+
*/
|
|
84
|
+
function exec(command) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
cp.exec(command, (error, stdout, stderr) => {
|
|
87
|
+
if (error)
|
|
88
|
+
reject(error);
|
|
89
|
+
else
|
|
90
|
+
resolve({ stdout, stderr });
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
UtilFunc.exec = exec;
|
|
95
|
+
})(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
|
package/package.json
CHANGED
package/src/UtilFunctions.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import * as crypto from "crypto";
|
|
2
2
|
import { JToken } from "./UtilInterfaces";
|
|
3
|
+
import * as cp from "child_process";
|
|
3
4
|
|
|
5
|
+
/**常用函数 */
|
|
6
|
+
export namespace UtilFunc{
|
|
4
7
|
/**获取当前时间戳
|
|
5
8
|
* number ()
|
|
6
9
|
* @returns {number} 时间戳
|
|
@@ -73,4 +76,20 @@ export async function sleep(timeMs: number): Promise<boolean> {
|
|
|
73
76
|
});
|
|
74
77
|
}
|
|
75
78
|
|
|
79
|
+
/**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
|
|
80
|
+
* @param {string} command 指令文本
|
|
81
|
+
*/
|
|
82
|
+
export function exec(command: string) {
|
|
83
|
+
return new Promise<{ stdout:string, stderr:string }>
|
|
84
|
+
((resolve, reject) => {
|
|
85
|
+
cp.exec(command, (error, stdout, stderr) => {
|
|
86
|
+
if (error)
|
|
87
|
+
reject(error);
|
|
88
|
+
else
|
|
89
|
+
resolve({ stdout, stderr });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
}
|
|
76
95
|
|