@zwa73/utils 1.0.162 → 1.0.164
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/UtilFileTools.d.ts +5 -2
- package/dist/UtilFileTools.js +3 -2
- package/dist/UtilFunctions.d.ts +16 -2
- package/dist/UtilFunctions.js +29 -4
- package/package.json +1 -1
- package/src/UtilFileTools.ts +5 -2
- package/src/UtilFunctions.ts +39 -4
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -36,6 +36,8 @@ type LoadJsonFileOpt<T> = Partial<{
|
|
|
36
36
|
type WriteJsonFileOpt = Partial<{
|
|
37
37
|
/**使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误 */
|
|
38
38
|
compress: boolean;
|
|
39
|
+
/**紧凑风格 压缩阈值 默认100 */
|
|
40
|
+
compressThreshold: number;
|
|
39
41
|
/**不自动修改扩展名为json */
|
|
40
42
|
forceExt: boolean;
|
|
41
43
|
}>;
|
|
@@ -118,8 +120,9 @@ export declare namespace UtilFT {
|
|
|
118
120
|
* @param filePath - 文件路径
|
|
119
121
|
* @param token - 所要写入的JToken
|
|
120
122
|
* @param opt - 可选参数
|
|
121
|
-
* @param opt.compress
|
|
122
|
-
* @param opt.forceExt
|
|
123
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
124
|
+
* @param opt.forceExt - 不自动修改扩展名为json
|
|
125
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
123
126
|
*/
|
|
124
127
|
function writeJSONFile(filePath: string, token: JToken, opt?: WriteJsonFileOpt): Promise<void>;
|
|
125
128
|
/**保证路径为某个风格
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -181,8 +181,9 @@ var UtilFT;
|
|
|
181
181
|
* @param filePath - 文件路径
|
|
182
182
|
* @param token - 所要写入的JToken
|
|
183
183
|
* @param opt - 可选参数
|
|
184
|
-
* @param opt.compress
|
|
185
|
-
* @param opt.forceExt
|
|
184
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
185
|
+
* @param opt.forceExt - 不自动修改扩展名为json
|
|
186
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
186
187
|
*/
|
|
187
188
|
async function writeJSONFile(filePath, token, opt) {
|
|
188
189
|
const str = UtilFunctions_1.UtilFunc.stringifyJToken(token, opt);
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ type ExecOpt = Partial<{
|
|
|
14
14
|
type StringifyOpt = Partial<{
|
|
15
15
|
/**使用紧凑风格 将会用/@@@.+@@@/作为特殊标记, 原始文本内若出现相同格式将会产生错误 */
|
|
16
16
|
compress: boolean;
|
|
17
|
+
/**紧凑风格 压缩阈值 默认100 */
|
|
18
|
+
compressThreshold: number;
|
|
17
19
|
/**插入的空格 数字为空格数量 默认为制表符\t */
|
|
18
20
|
space: string | number | null | undefined;
|
|
19
21
|
}>;
|
|
@@ -30,6 +32,12 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
30
32
|
}>;
|
|
31
33
|
type SuccessOut<T> = Outcome<Success, T>;
|
|
32
34
|
type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
|
|
35
|
+
/**遍历对象的回调函数
|
|
36
|
+
* @param key - 字段名
|
|
37
|
+
* @param value - 字段值
|
|
38
|
+
* @param parent - 父对象
|
|
39
|
+
*/
|
|
40
|
+
type EachFieldCallback = (key: string, value: JToken, parent: JObject) => void;
|
|
33
41
|
/**常用函数 */
|
|
34
42
|
export declare class UtilFunc {
|
|
35
43
|
/**获取当前时间戳
|
|
@@ -131,11 +139,17 @@ export declare class UtilFunc {
|
|
|
131
139
|
* @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
|
|
132
140
|
*/
|
|
133
141
|
static mapEntries<T extends AnyRecord>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
|
|
142
|
+
/** 遍历对象的所有字段
|
|
143
|
+
* @param obj - 对象
|
|
144
|
+
* @param callback - 回调函数
|
|
145
|
+
*/
|
|
146
|
+
static eachField(obj: JToken, callback: EachFieldCallback): void;
|
|
134
147
|
/**将JToken转换为字符串
|
|
135
148
|
* @param token - 待转换的Token
|
|
136
149
|
* @param opt - 可选参数
|
|
137
|
-
* @param opt.space
|
|
138
|
-
* @param opt.compress
|
|
150
|
+
* @param opt.space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
151
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
152
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
139
153
|
* @returns 转换完成的字符串
|
|
140
154
|
*/
|
|
141
155
|
static stringifyJToken(token: JToken | IJData, opt?: StringifyOpt): string;
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -378,18 +378,43 @@ class UtilFunc {
|
|
|
378
378
|
return result;
|
|
379
379
|
}, {});
|
|
380
380
|
}
|
|
381
|
+
/** 遍历对象的所有字段
|
|
382
|
+
* @param obj - 对象
|
|
383
|
+
* @param callback - 回调函数
|
|
384
|
+
*/
|
|
385
|
+
static eachField(obj, callback) {
|
|
386
|
+
if (obj == null)
|
|
387
|
+
return;
|
|
388
|
+
if (typeof obj === 'object' && 'toJSON' in obj && typeof obj.toJSON == 'function') {
|
|
389
|
+
UtilLogger_1.SLogger.warn('UtilFunc.eachField 错误 无法遍历IJData 已跳过');
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (Array.isArray(obj)) {
|
|
393
|
+
for (const item of obj)
|
|
394
|
+
UtilFunc.eachField(item, callback);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (typeof obj === 'object') {
|
|
398
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
399
|
+
UtilFunc.eachField(v, callback);
|
|
400
|
+
callback(k, v, obj);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
381
404
|
/**将JToken转换为字符串
|
|
382
405
|
* @param token - 待转换的Token
|
|
383
406
|
* @param opt - 可选参数
|
|
384
|
-
* @param opt.space
|
|
385
|
-
* @param opt.compress
|
|
407
|
+
* @param opt.space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
408
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
409
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
386
410
|
* @returns 转换完成的字符串
|
|
387
411
|
*/
|
|
388
412
|
static stringifyJToken(token, opt) {
|
|
389
413
|
opt ??= {};
|
|
390
|
-
let { compress, space } = opt;
|
|
414
|
+
let { compress, space, compressThreshold } = opt;
|
|
391
415
|
space ??= "\t";
|
|
392
416
|
space = space === null ? undefined : space;
|
|
417
|
+
compressThreshold ??= 100;
|
|
393
418
|
if (!compress)
|
|
394
419
|
return JSON.stringify(token, null, space);
|
|
395
420
|
const ec = '\uF121\uF122\uF123';
|
|
@@ -400,7 +425,7 @@ class UtilFunc {
|
|
|
400
425
|
const str = JSON.stringify(value);
|
|
401
426
|
if (typeof str != 'string')
|
|
402
427
|
return value;
|
|
403
|
-
if (str.length <=
|
|
428
|
+
if (str.length <= compressThreshold)
|
|
404
429
|
return `${ec}${str}${ec}`;
|
|
405
430
|
return value;
|
|
406
431
|
};
|
package/package.json
CHANGED
package/src/UtilFileTools.ts
CHANGED
|
@@ -48,6 +48,8 @@ type LoadJsonFileOpt<T> = Partial<{
|
|
|
48
48
|
type WriteJsonFileOpt = Partial<{
|
|
49
49
|
/**使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误 */
|
|
50
50
|
compress:boolean;
|
|
51
|
+
/**紧凑风格 压缩阈值 默认100 */
|
|
52
|
+
compressThreshold:number;
|
|
51
53
|
/**不自动修改扩展名为json */
|
|
52
54
|
forceExt:boolean;
|
|
53
55
|
}>
|
|
@@ -234,8 +236,9 @@ export async function loadJSONFile<T extends JToken>(filePath: string,opt?:LoadJ
|
|
|
234
236
|
* @param filePath - 文件路径
|
|
235
237
|
* @param token - 所要写入的JToken
|
|
236
238
|
* @param opt - 可选参数
|
|
237
|
-
* @param opt.compress
|
|
238
|
-
* @param opt.forceExt
|
|
239
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
240
|
+
* @param opt.forceExt - 不自动修改扩展名为json
|
|
241
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
239
242
|
*/
|
|
240
243
|
export async function writeJSONFile(
|
|
241
244
|
filePath: string,
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -24,6 +24,8 @@ type ExecOpt = Partial<{
|
|
|
24
24
|
type StringifyOpt = Partial<{
|
|
25
25
|
/**使用紧凑风格 将会用/@@@.+@@@/作为特殊标记, 原始文本内若出现相同格式将会产生错误 */
|
|
26
26
|
compress:boolean;
|
|
27
|
+
/**紧凑风格 压缩阈值 默认100 */
|
|
28
|
+
compressThreshold:number;
|
|
27
29
|
/**插入的空格 数字为空格数量 默认为制表符\t */
|
|
28
30
|
space:string|number|null|undefined;
|
|
29
31
|
}>
|
|
@@ -53,6 +55,14 @@ type PromiseResult<T> = {
|
|
|
53
55
|
/**请求下标/序号 */
|
|
54
56
|
index:number;
|
|
55
57
|
};
|
|
58
|
+
|
|
59
|
+
/**遍历对象的回调函数
|
|
60
|
+
* @param key - 字段名
|
|
61
|
+
* @param value - 字段值
|
|
62
|
+
* @param parent - 父对象
|
|
63
|
+
*/
|
|
64
|
+
type EachFieldCallback = (key: string, value: JToken,parent: JObject) => void;
|
|
65
|
+
|
|
56
66
|
/**常用函数 */
|
|
57
67
|
export class UtilFunc{
|
|
58
68
|
/**获取当前时间戳
|
|
@@ -444,18 +454,43 @@ static mapEntries<T extends AnyRecord>
|
|
|
444
454
|
}, {} as T);
|
|
445
455
|
}
|
|
446
456
|
|
|
457
|
+
/** 遍历对象的所有字段
|
|
458
|
+
* @param obj - 对象
|
|
459
|
+
* @param callback - 回调函数
|
|
460
|
+
*/
|
|
461
|
+
static eachField(obj: JToken, callback: EachFieldCallback): void {
|
|
462
|
+
if(obj==null) return;
|
|
463
|
+
if(typeof obj === 'object' && 'toJSON' in obj && typeof obj.toJSON == 'function'){
|
|
464
|
+
SLogger.warn('UtilFunc.eachField 错误 无法遍历IJData 已跳过');
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if(Array.isArray(obj)){
|
|
468
|
+
for (const item of obj)
|
|
469
|
+
UtilFunc.eachField(item, callback);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
if (typeof obj === 'object') {
|
|
473
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
474
|
+
UtilFunc.eachField(v, callback);
|
|
475
|
+
callback(k, v, obj as JObject);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
447
480
|
/**将JToken转换为字符串
|
|
448
481
|
* @param token - 待转换的Token
|
|
449
482
|
* @param opt - 可选参数
|
|
450
|
-
* @param opt.space
|
|
451
|
-
* @param opt.compress
|
|
483
|
+
* @param opt.space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
484
|
+
* @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
|
|
485
|
+
* @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
|
|
452
486
|
* @returns 转换完成的字符串
|
|
453
487
|
*/
|
|
454
488
|
static stringifyJToken(token:JToken|IJData,opt?:StringifyOpt){
|
|
455
489
|
opt ??= {};
|
|
456
|
-
let {compress,space} = opt;
|
|
490
|
+
let {compress,space,compressThreshold} = opt;
|
|
457
491
|
space ??="\t";
|
|
458
492
|
space = space===null ? undefined : space;
|
|
493
|
+
compressThreshold ??= 100;
|
|
459
494
|
|
|
460
495
|
if(!compress) return JSON.stringify(token,null,space);
|
|
461
496
|
|
|
@@ -468,7 +503,7 @@ static stringifyJToken(token:JToken|IJData,opt?:StringifyOpt){
|
|
|
468
503
|
return `${ec}${JSON.stringify(value)}${ec}`;
|
|
469
504
|
const str = JSON.stringify(value);
|
|
470
505
|
if(typeof str!='string') return value;
|
|
471
|
-
if(str.length<=
|
|
506
|
+
if(str.length<=compressThreshold) return `${ec}${str}${ec}`;
|
|
472
507
|
return value;
|
|
473
508
|
}
|
|
474
509
|
|