ph-utils 0.9.9 → 0.9.12
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.d.ts +25 -0
- package/lib/index.js +64 -0
- package/lib/server.js +28 -38
- package/lib/validator.d.ts +1 -0
- package/lib/validator.js +1 -0
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
@@ -126,4 +126,29 @@ export declare function round(num: number, precision?: number, roundType?: 0 | 1
|
|
126
126
|
* 反转字符串
|
127
127
|
*/
|
128
128
|
export declare function reverseStr(str: string): string;
|
129
|
+
/** 数据格式化配置 */
|
130
|
+
interface FormDataConfig<T> {
|
131
|
+
/** 配置需要转换为数字的字段 */
|
132
|
+
numberFields?: (keyof T)[];
|
133
|
+
/** 配置需要转换为字符串的字段 */
|
134
|
+
stringFields?: (keyof T)[];
|
135
|
+
/** 自定义的格式化 */
|
136
|
+
formatter?: {
|
137
|
+
[K in keyof T]?: "number" | "string" | ((value: any) => number | string);
|
138
|
+
};
|
139
|
+
}
|
140
|
+
/**
|
141
|
+
* 嵌套的 json 指定 key 数据
|
142
|
+
* @param data JSON格式数据
|
143
|
+
* @param keys 待获取的数据 key, 可以通过 [.] 获取嵌套数据, 例如: a.b.c
|
144
|
+
* @returns
|
145
|
+
*/
|
146
|
+
export declare function getJSONValue(data: Record<string, any>, keystr: string): Record<string, any> | null;
|
147
|
+
/**
|
148
|
+
* 数据格式化主要用于数据类型转换
|
149
|
+
* @param data 待转换数据类型的数据
|
150
|
+
* @param config 转换配置
|
151
|
+
* @returns
|
152
|
+
*/
|
153
|
+
export declare function formatData<T extends Record<string, any>>(data: T, config?: FormDataConfig<T>): T;
|
129
154
|
export {};
|
package/lib/index.js
CHANGED
@@ -173,3 +173,67 @@ export function round(num, precision = 2, roundType = 0) {
|
|
173
173
|
export function reverseStr(str) {
|
174
174
|
return str.split("").reverse().join("");
|
175
175
|
}
|
176
|
+
function getObjKeyValue(data, key) {
|
177
|
+
if (data == null)
|
178
|
+
return null;
|
179
|
+
return data[key];
|
180
|
+
}
|
181
|
+
/**
|
182
|
+
* 嵌套的 json 指定 key 数据
|
183
|
+
* @param data JSON格式数据
|
184
|
+
* @param keys 待获取的数据 key, 可以通过 [.] 获取嵌套数据, 例如: a.b.c
|
185
|
+
* @returns
|
186
|
+
*/
|
187
|
+
export function getJSONValue(data, keystr) {
|
188
|
+
if (data == null)
|
189
|
+
return null;
|
190
|
+
const keys = keystr.split(".");
|
191
|
+
let res = data;
|
192
|
+
for (const key of keys) {
|
193
|
+
res = getObjKeyValue(res, key);
|
194
|
+
if (res == null)
|
195
|
+
break;
|
196
|
+
}
|
197
|
+
return res;
|
198
|
+
}
|
199
|
+
/**
|
200
|
+
* 数据格式化主要用于数据类型转换
|
201
|
+
* @param data 待转换数据类型的数据
|
202
|
+
* @param config 转换配置
|
203
|
+
* @returns
|
204
|
+
*/
|
205
|
+
export function formatData(data, config) {
|
206
|
+
const cfg = {
|
207
|
+
numberFields: [],
|
208
|
+
stringFields: [],
|
209
|
+
formatter: {},
|
210
|
+
...config,
|
211
|
+
};
|
212
|
+
const res = {};
|
213
|
+
for (const key in data) {
|
214
|
+
let value = getJSONValue(data, key);
|
215
|
+
let formater;
|
216
|
+
if (key in cfg.formatter) {
|
217
|
+
formater = cfg.formatter[key];
|
218
|
+
}
|
219
|
+
if (cfg.numberFields.includes(key)) {
|
220
|
+
formater = "number";
|
221
|
+
}
|
222
|
+
if (cfg.stringFields.includes(key)) {
|
223
|
+
formater = "string";
|
224
|
+
}
|
225
|
+
if (formater != null) {
|
226
|
+
if (typeof formater == "function") {
|
227
|
+
value = formater(value);
|
228
|
+
}
|
229
|
+
else if (formater == "number") {
|
230
|
+
value = Number(value);
|
231
|
+
}
|
232
|
+
else if (formater == "string") {
|
233
|
+
value = String(value);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
res[key] = value;
|
237
|
+
}
|
238
|
+
return res;
|
239
|
+
}
|
package/lib/server.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { execFile } from "node:child_process";
|
2
2
|
import { parseEnv, parseArgs } from "node:util";
|
3
3
|
import { readFileSync } from "node:fs";
|
4
4
|
/**
|
@@ -7,47 +7,37 @@ import { readFileSync } from "node:fs";
|
|
7
7
|
* @returns
|
8
8
|
*/
|
9
9
|
export function exec(command, ...params) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
if (params[0]
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
opts = params[1];
|
23
|
-
}
|
24
|
-
}
|
25
|
-
else {
|
26
|
-
opts = params[0];
|
10
|
+
let argvs = [];
|
11
|
+
const commandItems = command.split(" ");
|
12
|
+
const cmd = commandItems.shift();
|
13
|
+
if (commandItems.length > 0) {
|
14
|
+
argvs = commandItems;
|
15
|
+
}
|
16
|
+
let opts = {};
|
17
|
+
if (params[0] != null) {
|
18
|
+
if (params[0] instanceof Array) {
|
19
|
+
argvs.push(...params[0]);
|
20
|
+
if (params[1] != null) {
|
21
|
+
opts = params[1];
|
27
22
|
}
|
28
23
|
}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
})
|
35
|
-
|
36
|
-
|
37
|
-
});
|
38
|
-
prs.on("error", (err) => {
|
39
|
-
error = err;
|
40
|
-
});
|
41
|
-
prs.on("close", (code, _signal) => {
|
42
|
-
if (code === 0) {
|
43
|
-
resolve(msg.join("\n"));
|
24
|
+
else {
|
25
|
+
opts = params[0];
|
26
|
+
}
|
27
|
+
}
|
28
|
+
return new Promise((resolve, reject) => {
|
29
|
+
execFile(cmd, argvs, (opts || {}), (err, stdout, stderr) => {
|
30
|
+
if (err) {
|
31
|
+
reject(err);
|
44
32
|
}
|
45
33
|
else {
|
46
|
-
|
47
|
-
|
34
|
+
let msg = stdout || stderr;
|
35
|
+
if (msg.includes("error") || msg.includes("Error")) {
|
36
|
+
const error = new Error(msg);
|
37
|
+
error.type = "StdErr";
|
38
|
+
reject(error);
|
48
39
|
}
|
49
|
-
|
50
|
-
reject(error);
|
40
|
+
resolve(msg);
|
51
41
|
}
|
52
42
|
});
|
53
43
|
});
|
@@ -87,7 +77,7 @@ export function parseEnvs() {
|
|
87
77
|
if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
|
88
78
|
nodeEnv = values.NODE_ENV;
|
89
79
|
}
|
90
|
-
|
80
|
+
if (nodeEnv == null) {
|
91
81
|
nodeEnv = "production";
|
92
82
|
}
|
93
83
|
process.env.NODE_ENV = nodeEnv;
|
package/lib/validator.d.ts
CHANGED
package/lib/validator.js
CHANGED
package/package.json
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
},
|
69
69
|
"./*": "./lib/*"
|
70
70
|
},
|
71
|
-
"version": "0.9.
|
71
|
+
"version": "0.9.12",
|
72
72
|
"type": "module",
|
73
73
|
"repository": {
|
74
74
|
"type": "git",
|
@@ -82,8 +82,8 @@
|
|
82
82
|
},
|
83
83
|
"homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
|
84
84
|
"devDependencies": {
|
85
|
-
"@types/node": "^
|
86
|
-
"typescript": "^5.
|
85
|
+
"@types/node": "^22.7.9",
|
86
|
+
"typescript": "^5.6.3"
|
87
87
|
},
|
88
88
|
"files": [
|
89
89
|
"lib"
|