@zwa73/utils 1.0.47 → 1.0.49
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/UtilClass.d.ts +36 -0
- package/dist/UtilClass.js +53 -1
- package/dist/UtilCom.js +6 -6
- package/dist/UtilDecorators.d.ts +8 -12
- package/dist/UtilDecorators.js +75 -13
- package/dist/UtilFileTools.d.ts +12 -2
- package/dist/UtilFileTools.js +23 -9
- package/dist/UtilFunctions.d.ts +74 -14
- package/dist/UtilFunctions.js +107 -3
- package/dist/UtilInterfaces.d.ts +22 -0
- package/dist/test/test.d.ts +1 -0
- package/dist/test/test.js +187 -0
- package/dist/test/test2.d.ts +1 -0
- package/dist/test/test2.js +35 -0
- package/package.json +4 -2
- package/postinstall.js +39 -0
- package/publish.bat +1 -1
- package/src/UtilClass.ts +68 -0
- package/src/UtilCom.ts +6 -6
- package/src/UtilDecorators.ts +66 -12
- package/src/UtilFileTools.ts +21 -9
- package/src/UtilFunctions.ts +150 -4
- package/src/UtilInterfaces.ts +56 -1
- package/src/test/test.ts +223 -0
- package/src/test/test2.ts +40 -0
- package/tsconfig.json +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const UtilFunctions_1 = require("../UtilFunctions");
|
|
4
|
+
const compKeys = ["getData", "getA"];
|
|
5
|
+
class A {
|
|
6
|
+
abc = 223;
|
|
7
|
+
/**A的getData */
|
|
8
|
+
getData() {
|
|
9
|
+
return this.abc;
|
|
10
|
+
}
|
|
11
|
+
/**获取A */
|
|
12
|
+
getA() {
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class C {
|
|
17
|
+
abc = 3333;
|
|
18
|
+
/**A的getData */
|
|
19
|
+
resetData() {
|
|
20
|
+
return this.abc;
|
|
21
|
+
}
|
|
22
|
+
/**获取C */
|
|
23
|
+
getC() {
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/**获取C */
|
|
27
|
+
getC1() {
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
class B {
|
|
32
|
+
_bc;
|
|
33
|
+
constructor() {
|
|
34
|
+
const tb = this;
|
|
35
|
+
this._bc = UtilFunctions_1.UtilFunc.composePartClass(tb, new A(), ...compKeys);
|
|
36
|
+
}
|
|
37
|
+
static init() {
|
|
38
|
+
return new B()._bc;
|
|
39
|
+
}
|
|
40
|
+
/**B的getData */
|
|
41
|
+
getData1() { return "BGetdata"; }
|
|
42
|
+
getTest() { return this._bc.getData(); }
|
|
43
|
+
}
|
|
44
|
+
const BComp = {
|
|
45
|
+
init() {
|
|
46
|
+
return B.init();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
let c = BComp.init();
|
|
50
|
+
console.log(1);
|
|
51
|
+
let d = c.getData(); //?
|
|
52
|
+
c.getA;
|
|
53
|
+
c.getData1(); //?
|
|
54
|
+
c.getTest(); //?
|
|
55
|
+
let r3 = null;
|
|
56
|
+
class Monster {
|
|
57
|
+
constructor(a) { }
|
|
58
|
+
monfunc() {
|
|
59
|
+
return "mon";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class Monster1 {
|
|
63
|
+
constructor(a) { }
|
|
64
|
+
m1num = 123;
|
|
65
|
+
monfunc1() {
|
|
66
|
+
return "mon1";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const OtherMonster = {
|
|
70
|
+
init() {
|
|
71
|
+
let mon = new Monster(123);
|
|
72
|
+
let mon1 = new Monster1(223);
|
|
73
|
+
let om = {
|
|
74
|
+
om: "123", mon
|
|
75
|
+
};
|
|
76
|
+
return UtilFunctions_1.UtilFunc.composeClass(om, mon, mon1);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
let om = OtherMonster.init(); //?
|
|
80
|
+
om.monfunc(); //?
|
|
81
|
+
om.monfunc1(); //?
|
|
82
|
+
om.m1num; //?
|
|
83
|
+
const BASE_MAX = { 攻击: 24, 生命: 54, 防御: 16, 暴击: 5, 暴击伤害: 5, 攻击加成: 3.8, 生命加成: 3.8, 防御加成: 3.8 };
|
|
84
|
+
const 精调前 = [
|
|
85
|
+
{ 攻击: 17, 生命: 0, 防御: 0, 暴击: 0, 暴击伤害: 3.5, 攻击加成: 3.0, 生命加成: 0, 防御加成: 3.4 },
|
|
86
|
+
{ 攻击: 19, 生命: 0, 防御: 0, 暴击: 4.5, 暴击伤害: 4.5, 攻击加成: 3.4, 生命加成: 0, 防御加成: 0 },
|
|
87
|
+
{ 攻击: 21, 生命: 0, 防御: 10, 暴击: 0, 暴击伤害: 0, 攻击加成: 3.8, 生命加成: 0, 防御加成: 3.0 },
|
|
88
|
+
{ 攻击: 17, 生命: 48, 防御: 14, 暴击: 0, 暴击伤害: 0, 攻击加成: 2.6, 生命加成: 0, 防御加成: 0 },
|
|
89
|
+
{ 攻击: 23, 生命: 0, 防御: 0, 暴击: 5, 暴击伤害: 0, 攻击加成: 3.4, 生命加成: 0, 防御加成: 0 },
|
|
90
|
+
{ 攻击: 17, 生命: 53, 防御: 0, 暴击: 0, 暴击伤害: 0, 攻击加成: 3.4, 生命加成: 0, 防御加成: 0 },
|
|
91
|
+
{ 攻击: 21, 生命: 0, 防御: 0, 暴击: 3.5, 暴击伤害: 0, 攻击加成: 3.0, 生命加成: 0, 防御加成: 0 },
|
|
92
|
+
{ 攻击: 17, 生命: 0, 防御: 3.8, 暴击: 0, 暴击伤害: 0, 攻击加成: 2.6, 生命加成: 0, 防御加成: 0 },
|
|
93
|
+
{ 攻击: 19, 生命: 38, 防御: 0, 暴击: 0, 暴击伤害: 0, 攻击加成: 3.4, 生命加成: 0, 防御加成: 0 }, // 机枪 双重对策 折叠脚架
|
|
94
|
+
];
|
|
95
|
+
const 精调后 = [
|
|
96
|
+
{ 攻击: 26, 生命: 0, 防御: 0, 暴击: 0, 暴击伤害: 8.1, 攻击加成: 7.5, 生命加成: 0, 防御加成: 5.8 },
|
|
97
|
+
{ 攻击: 31, 生命: 0, 防御: 0, 暴击: 10.4, 暴击伤害: 10.4, 攻击加成: 7.5, 生命加成: 0, 防御加成: 0 },
|
|
98
|
+
{ 攻击: 49, 生命: 0, 防御: 17, 暴击: 0, 暴击伤害: 0, 攻击加成: 7.6, 生命加成: 0, 防御加成: 6.9 },
|
|
99
|
+
{ 攻击: 46, 生命: 116, 防御: 27, 暴击: 0, 暴击伤害: 0, 攻击加成: 5.0, 生命加成: 0, 防御加成: 0 },
|
|
100
|
+
{ 攻击: 42, 生命: 0, 防御: 0, 暴击: 11.5, 暴击伤害: 0, 攻击加成: 6.8, 生命加成: 0, 防御加成: 0 },
|
|
101
|
+
{ 攻击: 34, 生命: 101, 防御: 0, 暴击: 0, 暴击伤害: 0, 攻击加成: 6.5, 生命加成: 0, 防御加成: 0 },
|
|
102
|
+
{ 攻击: 38, 生命: 0, 防御: 0, 暴击: 8.4, 暴击伤害: 0, 攻击加成: 4.8, 生命加成: 0, 防御加成: 0 },
|
|
103
|
+
{ 攻击: 43, 生命: 0, 防御: 8.0, 暴击: 0, 暴击伤害: 0, 攻击加成: 7.3, 生命加成: 0, 防御加成: 0 },
|
|
104
|
+
{ 攻击: 42, 生命: 54, 防御: 0, 暴击: 0, 暴击伤害: 0, 攻击加成: 6.2, 生命加成: 0, 防御加成: 0 }, // 机枪 双重对策 折叠脚架
|
|
105
|
+
];
|
|
106
|
+
const Pointify = (ed) => UtilFunctions_1.UtilFunc.mapObject(ed, (k, v) => v ? v / BASE_MAX[k] : 0);
|
|
107
|
+
const Sump = (ed) => Object.values(ed).reduce((r, v) => r + v);
|
|
108
|
+
const pipeFunc = UtilFunctions_1.UtilFunc.pipeline(Pointify, Sump);
|
|
109
|
+
const Display = (list) => list.reduce((r, v) => `${r}\t${v.toFixed(2)}`, "").trim();
|
|
110
|
+
const pre = 精调前.map((v) => pipeFunc(v));
|
|
111
|
+
const aft = 精调后.map((v) => pipeFunc(v));
|
|
112
|
+
const div = pre.map((dv, i) => aft[i] / dv);
|
|
113
|
+
Display(pre); //?
|
|
114
|
+
Display(aft); //?
|
|
115
|
+
Display(div); //?
|
|
116
|
+
// 定义一个函数,它将作为构造函数
|
|
117
|
+
function MyObject() {
|
|
118
|
+
// @ts-ignore
|
|
119
|
+
this.a = 123;
|
|
120
|
+
}
|
|
121
|
+
let asloop = [
|
|
122
|
+
{
|
|
123
|
+
type: "effect_of_condition",
|
|
124
|
+
id: "loop",
|
|
125
|
+
effect: [
|
|
126
|
+
{ if: { math: ["i", ">", "0"] },
|
|
127
|
+
then: [
|
|
128
|
+
{ u_message: "hello world" },
|
|
129
|
+
{ math: ["i", "-=", "1"] },
|
|
130
|
+
{ run_eocs: "loop" },
|
|
131
|
+
] }
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
type: "effect_of_condition",
|
|
136
|
+
id: "main",
|
|
137
|
+
effect: [
|
|
138
|
+
{ math: ["i", "=", "10"] },
|
|
139
|
+
{ run_eocs: "loop" }
|
|
140
|
+
]
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
let asaloop = [
|
|
144
|
+
{
|
|
145
|
+
type: "effect_of_condition",
|
|
146
|
+
id: "loop",
|
|
147
|
+
effect: [
|
|
148
|
+
{ u_message: "hello world" }
|
|
149
|
+
]
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
type: "effect_of_condition",
|
|
153
|
+
id: "main",
|
|
154
|
+
effect: [
|
|
155
|
+
{
|
|
156
|
+
run_eoc_with: "efl",
|
|
157
|
+
variables: {
|
|
158
|
+
i: "0",
|
|
159
|
+
length: "10",
|
|
160
|
+
eoc: "loop"
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
}
|
|
165
|
+
];
|
|
166
|
+
const testa = {
|
|
167
|
+
_a: 123
|
|
168
|
+
};
|
|
169
|
+
Object.defineProperty(testa, 'a', {
|
|
170
|
+
get: () => 1,
|
|
171
|
+
set: function (value) {
|
|
172
|
+
console.log('Set:', value);
|
|
173
|
+
},
|
|
174
|
+
enumerable: true
|
|
175
|
+
});
|
|
176
|
+
Object.defineProperty(testa, 'c', {
|
|
177
|
+
value: 123,
|
|
178
|
+
writable: true,
|
|
179
|
+
enumerable: true,
|
|
180
|
+
configurable: true
|
|
181
|
+
});
|
|
182
|
+
testa.constructor.prototype; //?
|
|
183
|
+
testa; //?
|
|
184
|
+
let testkeys = [];
|
|
185
|
+
for (let k in testa)
|
|
186
|
+
testkeys.push(k);
|
|
187
|
+
testkeys; //?
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const UtilFileTools_1 = require("../UtilFileTools");
|
|
5
|
+
// 将字符串转换为十六进制
|
|
6
|
+
function stringToHex(str) {
|
|
7
|
+
const buf = Buffer.from(str, 'utf8');
|
|
8
|
+
return buf.toString('hex');
|
|
9
|
+
}
|
|
10
|
+
// 将十六进制转换为字符串
|
|
11
|
+
function hexToString(hex) {
|
|
12
|
+
const buf = Buffer.from(hex, 'hex');
|
|
13
|
+
return buf.toString('utf8');
|
|
14
|
+
}
|
|
15
|
+
// 示例
|
|
16
|
+
let str = "Hello World";
|
|
17
|
+
let hex = "0B0000002D";
|
|
18
|
+
console.log(stringToHex(str)); // 输出字符串对应的十六进制
|
|
19
|
+
console.log(hexToString(hex)); // 输出十六进制对应的字符串
|
|
20
|
+
hexToString("556E6974794653"); //?
|
|
21
|
+
/**
|
|
22
|
+
首先我有一个文件夹./in
|
|
23
|
+
我在里面存放了一些整合加密的二进制文件,文件名符合正则/.*\.bk/
|
|
24
|
+
我需要你帮我写解密程序,将其解密并分离为单独的文件,接下来我告诉你解密方式:
|
|
25
|
+
读取文件后,我们先用split(hexToString(hex))对文本进行分割成多块,然后在每一块找到第二个"UnityFS",
|
|
26
|
+
再从第二个UnityFS的开头截取到每一块的末尾
|
|
27
|
+
我举一个例子,假设我们有一段分割完成的文本块 "112233 UnityFS sasasd UnityFS我需要的内容"
|
|
28
|
+
那么它截取完成之后应该是"UnityFS我需要的内容"
|
|
29
|
+
最后,我们在将所有截取完成的文本块作为文件输出到./out/{filename}
|
|
30
|
+
文件名就是 {原文件名+块编号.bs}
|
|
31
|
+
开始写吧
|
|
32
|
+
**/
|
|
33
|
+
path.parse("123.bs").name; //?
|
|
34
|
+
let filepath = "F:\\Sosarciel\\SosarcielCore\\NodeJs\\utils\\src";
|
|
35
|
+
UtilFileTools_1.UtilFT.fileSearchGlob(`${filepath}/**/*.ts`, "**/test/**/*.ts"); //?
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.49",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test"
|
|
7
|
+
"test": "node test",
|
|
8
|
+
"postinstall": "node postinstall.js"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [
|
|
10
11
|
"zwa73"
|
|
@@ -16,6 +17,7 @@
|
|
|
16
17
|
"esm": "^3.2.25",
|
|
17
18
|
"esm-resolve": "^1.0.8",
|
|
18
19
|
"fluent-ffmpeg": "^2.1.2",
|
|
20
|
+
"glob": "^10.3.10",
|
|
19
21
|
"html-entities": "^2.3.3",
|
|
20
22
|
"http-proxy-agent": "^5.0.0",
|
|
21
23
|
"https-proxy-agent": "^5.0.1",
|
package/postinstall.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const currentVersion = require('./package.json').version;
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { UtilFT } = require("./index");
|
|
4
|
+
const dataPath = path.join(__dirname,"..","version.json");
|
|
5
|
+
UtilFT.ensurePathExists(dataPath);
|
|
6
|
+
let dataTable = UtilFT.loadJSONFileSync(dataPath,{});
|
|
7
|
+
dataTable.utils = dataTable.utils??{};
|
|
8
|
+
let prevVersion = dataTable.utils.version;
|
|
9
|
+
dataTable.utils.version = currentVersion;
|
|
10
|
+
|
|
11
|
+
console.log(`${currentVersion} 版本已安装`);
|
|
12
|
+
|
|
13
|
+
// 将版本号转换为可以比较的数字
|
|
14
|
+
function versionToNumber(version) {
|
|
15
|
+
if(version==undefined) return 0;
|
|
16
|
+
return version.split('.').map(Number).reduce((acc, val) => acc * 1000 + val);
|
|
17
|
+
}
|
|
18
|
+
//提示表
|
|
19
|
+
const infoTable = {
|
|
20
|
+
"1.0.17":"fileSearch 函数进入 UtilFT 命名空间",
|
|
21
|
+
"1.0.47":"UtilFT.fileSearch 函数与返回值发生变动, 请使用fileSearchRegex 或 fileSearchGlob"
|
|
22
|
+
}
|
|
23
|
+
//显示提示
|
|
24
|
+
function showUpgradeMessages(prevVersion, currentVersion, infoTable) {
|
|
25
|
+
let prevVersionNumber = versionToNumber(prevVersion);
|
|
26
|
+
let currentVersionNumber = versionToNumber(currentVersion);
|
|
27
|
+
// 遍历infoTable中的所有版本
|
|
28
|
+
for (let version in infoTable) {
|
|
29
|
+
let versionNumber = versionToNumber(version);
|
|
30
|
+
// 如果用户的上一个版本低于这个版本,而当前版本高于或等于这个版本
|
|
31
|
+
if (versionNumber > prevVersionNumber && versionNumber <= currentVersionNumber) {
|
|
32
|
+
// 显示这个版本的提示信息
|
|
33
|
+
console.log(infoTable[version]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 使用这个函数来显示升级信息
|
|
39
|
+
showUpgradeMessages(prevVersion, currentVersion, infoTable);
|
package/publish.bat
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
npm publish --access public
|
|
1
|
+
call npm publish --access public
|
|
2
2
|
pause
|
package/src/UtilClass.ts
CHANGED
|
@@ -981,3 +981,71 @@ class SKVC {
|
|
|
981
981
|
|
|
982
982
|
constructor() {}
|
|
983
983
|
}
|
|
984
|
+
|
|
985
|
+
/**函数组合器 */
|
|
986
|
+
export class Composer<Result,PreArg = Result> {
|
|
987
|
+
/**组合函数列表 */
|
|
988
|
+
private funcs: Function[] = [];
|
|
989
|
+
private constructor(){};
|
|
990
|
+
/**添加单个参数与返回值不同的函数 */
|
|
991
|
+
public static push<Result,Arg>(func: (arg: Arg) => Result): Composer<Result,Arg>;
|
|
992
|
+
/**添加多个参数与返回值相同的函数 */
|
|
993
|
+
public static push<Result>(func:((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Composer<Result>;
|
|
994
|
+
public static push(...funcs: Function[]){
|
|
995
|
+
const newComposer = new Composer<any>();
|
|
996
|
+
newComposer.funcs = [...funcs];
|
|
997
|
+
return newComposer;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/**添加单个参数与返回值不同的函数 */
|
|
1001
|
+
public push<T>(func: (arg: T) => PreArg): Composer<Result,T>;
|
|
1002
|
+
/**添加多个参数与返回值相同的函数 */
|
|
1003
|
+
public push(func:((arg: Result) => Result), ...funcs: ((arg: PreArg) => PreArg)[]): Composer<Result,PreArg>;
|
|
1004
|
+
public push(...funcs: Function[]): Composer<Result,any> {
|
|
1005
|
+
const newComposer = new Composer<Result>();
|
|
1006
|
+
newComposer.funcs = [...this.funcs, ...funcs];
|
|
1007
|
+
return newComposer;
|
|
1008
|
+
}
|
|
1009
|
+
/**组合函数 */
|
|
1010
|
+
public compose():(arg:PreArg)=>Result {
|
|
1011
|
+
return (arg:PreArg) => this.funcs.reduceRight((value, func) => func(value), arg) as any as Result;
|
|
1012
|
+
}
|
|
1013
|
+
/**直接调用 */
|
|
1014
|
+
public invoke(arg:PreArg):Result {
|
|
1015
|
+
return this.funcs.reduceRight((value, func) => func(value), arg) as any as Result;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
/**函数管道器 */
|
|
1019
|
+
export class Piper<Arg, Result = Arg> {
|
|
1020
|
+
/**管道函数列表 */
|
|
1021
|
+
private funcs: Function[] = [];
|
|
1022
|
+
private constructor(){};
|
|
1023
|
+
/**添加单个参数与返回值不同的函数 */
|
|
1024
|
+
public static pipe<Arg, Result>(func: (arg: Arg) => Result): Piper<Arg, Result>;
|
|
1025
|
+
/**添加多个参数与返回值相同的函数 */
|
|
1026
|
+
public static pipe<Arg>(func:((arg: Arg) => Arg), ...funcs: ((arg: Arg) => Arg)[]): Piper<Arg>;
|
|
1027
|
+
public static pipe(...funcs: Function[]){
|
|
1028
|
+
const newPiper = new Piper<any>();
|
|
1029
|
+
newPiper.funcs = [...funcs];
|
|
1030
|
+
return newPiper;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**添加单个参数与返回值不同的函数 */
|
|
1034
|
+
public pipe<T>(func: (arg: Result) => T): Piper<Arg, T>;
|
|
1035
|
+
/**添加多个参数与返回值相同的函数 */
|
|
1036
|
+
public pipe(func:((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Piper<Arg, Result>;
|
|
1037
|
+
public pipe(...funcs: Function[]): Piper<Arg, any> {
|
|
1038
|
+
const newPiper = new Piper<Arg>();
|
|
1039
|
+
newPiper.funcs = [...this.funcs, ...funcs];
|
|
1040
|
+
return newPiper;
|
|
1041
|
+
}
|
|
1042
|
+
/**管道函数 */
|
|
1043
|
+
public pipeline():(arg:Arg)=>Result {
|
|
1044
|
+
return (arg:Arg) => this.funcs.reduce((value, func) => func(value), arg) as any as Result;
|
|
1045
|
+
}
|
|
1046
|
+
/**直接调用 */
|
|
1047
|
+
public invoke(arg:Arg):Result {
|
|
1048
|
+
return this.funcs.reduce((value, func) => func(value), arg) as any as Result;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
package/src/UtilCom.ts
CHANGED
|
@@ -22,7 +22,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
22
22
|
timeLimit*=1000
|
|
23
23
|
|
|
24
24
|
const jsonStr = stringifyJToken(json);
|
|
25
|
-
const funcName =
|
|
25
|
+
const funcName = `s${posttype}Psot`;
|
|
26
26
|
|
|
27
27
|
return new Promise((resolve, rejecte)=>{
|
|
28
28
|
const resFunc = (res:http.IncomingMessage)=>{
|
|
@@ -31,7 +31,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
31
31
|
if(hasTimeLimit){
|
|
32
32
|
res.setTimeout(timeLimit, ()=>{
|
|
33
33
|
//res.abort();
|
|
34
|
-
SLogger.warn(funcName
|
|
34
|
+
SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
35
35
|
resolve(null);
|
|
36
36
|
return;
|
|
37
37
|
});
|
|
@@ -42,7 +42,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
42
42
|
res.on('data',(chunk)=>resdata+=chunk);
|
|
43
43
|
|
|
44
44
|
res.on('error',(e)=>{
|
|
45
|
-
SLogger.warn(funcName
|
|
45
|
+
SLogger.warn(`${funcName} 接收反馈错误:${e}`);
|
|
46
46
|
resolve(null);
|
|
47
47
|
return;
|
|
48
48
|
});
|
|
@@ -60,7 +60,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
62
|
catch(e){
|
|
63
|
-
SLogger.warn(funcName
|
|
63
|
+
SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
64
64
|
resolve(null);
|
|
65
65
|
return;
|
|
66
66
|
}
|
|
@@ -81,13 +81,13 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
81
81
|
//请求超时
|
|
82
82
|
if(hasTimeLimit){
|
|
83
83
|
req.setTimeout(timeLimit, ()=>{
|
|
84
|
-
SLogger.warn(funcName
|
|
84
|
+
SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
|
|
85
85
|
req.destroy();
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
req.on('error', (e)=>{
|
|
90
|
-
SLogger.warn(funcName
|
|
90
|
+
SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
91
91
|
resolve(null);
|
|
92
92
|
});
|
|
93
93
|
|
package/src/UtilDecorators.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { SLogger } from "./UtilLogger";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
/**用于打印方法的调用
|
|
5
|
-
* @returns {void}
|
|
6
|
-
*/
|
|
4
|
+
/**用于打印方法的调用 */
|
|
7
5
|
export function DLogger(){
|
|
8
6
|
return function (target:any, propertyKey:string, descriptor:PropertyDescriptor){
|
|
9
7
|
const originalMethod = descriptor.value;
|
|
@@ -15,9 +13,7 @@ export function DLogger(){
|
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
/**用于打印异步方法的调用
|
|
19
|
-
* @returns {void}
|
|
20
|
-
*/
|
|
16
|
+
/**用于打印异步方法的调用 */
|
|
21
17
|
export function DLoggerAsync(){
|
|
22
18
|
return function (target:any, propertyKey:string, descriptor:PropertyDescriptor){
|
|
23
19
|
const originalMethod = descriptor.value;
|
|
@@ -28,10 +24,42 @@ export function DLoggerAsync(){
|
|
|
28
24
|
}
|
|
29
25
|
}
|
|
30
26
|
}
|
|
27
|
+
/**try-finally包装 */
|
|
28
|
+
export function Defer(deferLogic: () => void) {
|
|
29
|
+
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
30
|
+
const originalMethod = descriptor.value;
|
|
31
|
+
descriptor.value = function (...args: any[]) {
|
|
32
|
+
try {
|
|
33
|
+
const result = originalMethod.apply(this, args);
|
|
34
|
+
deferLogic();
|
|
35
|
+
return result;
|
|
36
|
+
} catch(e) {
|
|
37
|
+
deferLogic();
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return descriptor;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**异步的try-finally包装 */
|
|
45
|
+
export function DeferAsync(deferLogic: () => void) {
|
|
46
|
+
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
47
|
+
const originalMethod = descriptor.value;
|
|
48
|
+
descriptor.value = async function (...args: any[]) {
|
|
49
|
+
try {
|
|
50
|
+
const result = await originalMethod.apply(this, args);
|
|
51
|
+
deferLogic();
|
|
52
|
+
return result;
|
|
53
|
+
} catch(e) {
|
|
54
|
+
deferLogic();
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
return descriptor;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
31
61
|
|
|
32
|
-
/**用于捕获方法中的错误
|
|
33
|
-
* @returns {void}
|
|
34
|
-
*/
|
|
62
|
+
/**用于捕获方法中的错误 */
|
|
35
63
|
export function DCatchErrors() {
|
|
36
64
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
37
65
|
const originalMethod = descriptor.value;
|
|
@@ -45,9 +73,7 @@ export function DCatchErrors() {
|
|
|
45
73
|
};
|
|
46
74
|
}
|
|
47
75
|
|
|
48
|
-
/**用于捕获异步方法中的错误
|
|
49
|
-
* @returns {void}
|
|
50
|
-
*/
|
|
76
|
+
/**用于捕获异步方法中的错误 */
|
|
51
77
|
export function DCatchErrorsAsync() {
|
|
52
78
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
53
79
|
const originalMethod = descriptor.value;
|
|
@@ -60,3 +86,31 @@ export function DCatchErrorsAsync() {
|
|
|
60
86
|
};
|
|
61
87
|
};
|
|
62
88
|
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
function AddNumberDecorator(n: number) {
|
|
92
|
+
type NumberToNumberFunc = (num: number) => number;
|
|
93
|
+
return function(
|
|
94
|
+
this:any,
|
|
95
|
+
target: Object,
|
|
96
|
+
propertyKey: string,
|
|
97
|
+
descriptor: TypedPropertyDescriptor<NumberToNumberFunc>
|
|
98
|
+
) {
|
|
99
|
+
const originalMethod = descriptor.value!;
|
|
100
|
+
descriptor.value = function (num: number) {
|
|
101
|
+
const result = originalMethod.apply(this, [num]);
|
|
102
|
+
return result + n;
|
|
103
|
+
};
|
|
104
|
+
return descriptor;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
class Example {
|
|
109
|
+
@AddNumberDecorator(10)
|
|
110
|
+
myMethod(num: number): number {
|
|
111
|
+
return num;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
//let a = new Example();
|
|
116
|
+
//a.myMethod(10);//?
|
package/src/UtilFileTools.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as path from "path";
|
|
|
3
3
|
import { JObject, JToken, stringifyJToken } from "./UtilInterfaces";
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
|
5
5
|
import * as JSON5 from 'json5';
|
|
6
|
+
import { globSync } from "glob";
|
|
6
7
|
|
|
7
8
|
/**文件工具 */
|
|
8
9
|
export namespace UtilFT{
|
|
@@ -201,25 +202,36 @@ export async function writeJSONFile(
|
|
|
201
202
|
/**搜索路径符合正则表达式的文件
|
|
202
203
|
* @param folder - 文件夹路径
|
|
203
204
|
* @param traitRegex - 正则表达式
|
|
204
|
-
* @returns
|
|
205
|
+
* @returns 文件名路径数组
|
|
205
206
|
*/
|
|
206
|
-
export function
|
|
207
|
-
let
|
|
207
|
+
export function fileSearchRegex(folder: string, traitRegex: string) {
|
|
208
|
+
let outArray: string[] = [];
|
|
208
209
|
let subFiles = fs.readdirSync(folder);
|
|
209
210
|
let regex = new RegExp(traitRegex);
|
|
210
211
|
for (let subFile of subFiles) {
|
|
211
212
|
let subFilePath = path.join(folder, subFile);
|
|
212
|
-
subFilePath = subFilePath.replace(/\\/g, "/");
|
|
213
213
|
let stat = fs.lstatSync(subFilePath);
|
|
214
|
-
|
|
215
214
|
//判断是否是文件夹,递归调用
|
|
216
215
|
if (stat.isDirectory()) {
|
|
217
|
-
|
|
218
|
-
for (let key in subMap) outMap[key] = subMap[key];
|
|
216
|
+
outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
219
217
|
continue;
|
|
220
218
|
}
|
|
221
|
-
if (regex.test(subFilePath))
|
|
219
|
+
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
222
220
|
}
|
|
223
|
-
return
|
|
221
|
+
return outArray;
|
|
222
|
+
}
|
|
223
|
+
/**搜索符合Glob匹配的文件
|
|
224
|
+
* @param globPattern - glob匹配
|
|
225
|
+
* @param ignore - 忽略的文件
|
|
226
|
+
* @returns 文件绝对路径数组
|
|
227
|
+
*/
|
|
228
|
+
export function fileSearchGlob(globPattern:string|string[],ignore?:string|string[]){
|
|
229
|
+
return globSync(globPattern,{ignore,absolute:true});
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
|
|
233
|
+
*/
|
|
234
|
+
export function fileSearch(...patams:any[]){
|
|
235
|
+
throw "请使用 fileSearchRegex 或 fileSearchGlob"
|
|
224
236
|
}
|
|
225
237
|
}
|