-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rule.ts
410 lines (384 loc) · 11.3 KB
/
Rule.ts
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/** 规则接口(所有规则都必须实现此接口) */
export interface IRule<RET> {
/** 规则标识 */
identifier: string;
/** 规则解析器 */
parser: (text: string, params?: string) => RET;
/** 规则类型转换器 */
transformer: (rule: string, params?: string) => string;
}
/**
* 使用正则表达式确保字符串只包含有效的数字字符(包括前置负号和小数点)
* @param input 输入字符串
* @returns
*/
function convertStringToNumber(input: string): number {
if (typeof input == "number") return input;
const ret = Number(input.replace(/[^\d.-]/g, ""));
return isNaN(ret) ? 0 : ret;
}
/**
* 布尔解析器
* @param text 文本
* @example 0 => false
* @example 1 => true
* @returns
*/
function BooleanParser(text: string | number): boolean {
return [1, "1", "T", "true", "TRUE"].includes(text);
}
/**
* 整数解析器
* @param text 文本
* @example 100
* @returns
*/
function IntegerParser(text: string): number {
text ??= "0";
const ret = convertStringToNumber(text);
return Math.floor(ret);
}
/**
* 数值解析器
* @param text 文本
* @example 52.0
* @returns
*/
function NumberParser(text: string): number {
text ??= "0";
return convertStringToNumber(text);
}
/**
* 字符串解析器
* @param text 文本
* @example hello, world
* @returns
*/
function StringParser(text: string): string {
return text ?? "";
}
/**
* 选择器解析器
* @param text 文本
* @param params 参数
* @example 0|1|2
* @example 未知|男|女
* @returns
*/
function PickParser(text: string, params?: string): number {
const values: Record<string, number> = {};
params!.split(",").forEach((v, i) => {
values[v] = i;
});
if (text == undefined) {
throw new Error(`[OP] "${text}" Not a valid option`);
}
let ret = values[text];
if (ret === undefined) {
throw new Error(`[OP] "${text}" Not a valid option`);
}
return ret;
}
/**
* 数组解析器
* @param text 文本
* @param params 参数
* @returns
*/
function ListParser(text: string, params?: string): any[] {
const pk = params!.split("#")[0];
return text.split(",").map((v) => Ruler.parse(pk, v));
}
/**
* 布尔数值解析器
* @param text 文本
* @returns
*/
function ListBooleanParser(text: string): boolean[] {
return ListParser(text, "B");
}
/**
* 整数解析器
* @param text 文本
* @returns
*/
function ListIntegerParser(text: string): number[] {
return ListParser(text, "I");
}
/**
* 数值数组解析器
* @param text 文本
* @returns
*/
function ListNumberParser(text: string): number[] {
return ListParser(text, "N");
}
/**
* 字符串数组解析器
* @param text 文本
* @returns
*/
function ListStringParser(text: string): string[] {
return ListParser(text, "S");
}
/**
* 数组逐项解析器
* @param text 文本
* @param params 参数
*/
function ListItemParser(text: string, params?: string): any[] {
const ps = params!.split("#")[0].split(",");
return text.split(",").map((v, i) => Ruler.parse(ps[i], v));
}
/**
* 映射表解析器
* @param text 文本
* @param params 参数
* @returns
*/
function MapParser(text: string, params?: string): Record<string, any> {
const ret: Record<string, any> = {};
const pk = params!.split("#")[0];
text.split(";").forEach((sub) => {
const [k, v] = sub.split(",");
const key = Ruler.parse("S", k);
const val = Ruler.parse(pk, v);
ret[key] = val;
});
return ret;
}
/**
* 布尔映射解析器
* @param text 文本
* @returns
*/
function MapBooleanParser(text: string): Record<string, boolean> {
return MapParser(text, "B");
}
/**
* 整数映射解析器
* @param text 文本
* @returns
*/
function MapIntegerParser(text: string): Record<string, number> {
return MapParser(text, "I");
}
/**
* 数值映射解析器
* @param text 文本
* @returns
*/
function MapNumberParser(text: string): Record<string, number> {
return MapParser(text, "N");
}
/**
* 字符串映射解析器
* @param text 文本
* @returns
*/
function MapStringParser(text: string): Record<string, string> {
return MapParser(text, "S");
}
/**
* 统一类型映射表解析器
* @param text 文本
* @param params 参数
* @returns
*/
function MapAllParser(text: string, params?: string): Record<string, any> {
const ret: Record<string, any> = {};
const pks = params!.split("#")[0].split(",");
const ps = pks[0];
text.split(",").forEach((sub, i) => {
const key = pks[i + 1];
const val = Ruler.parse(ps, sub);
ret[key] = val;
});
return ret;
}
/**
* 映射表逐项解析器
* @param text 文本
* @param params 参数
* @returns
*/
function MapItemParser(text: string, params?: string): Record<string, any> {
const ps = params!.split("#")[0].split(";");
const ret: Record<string, any> = {};
text.split(",").forEach((sub, i) => {
const [pk, pv] = ps[i].split(",");
const val = Ruler.parse(pv, sub);
ret[pk] = val;
});
return ret;
}
/**
* 根据规则转换类型
* @param identifier 规则标识符
* @param params 规则参数
* @returns
*/
function BasicTypeTransformer(identifier: string, params?: string) {
let type: string = "";
switch (identifier) {
case "B":
type = "boolean";
break;
case "I":
case "N":
type = "number";
break;
case "P":
type = params!
.split("#")[0]
.split(",")
.map((v, i) => i)
.join("|");
break;
case "S":
type = "string";
break;
case "LB":
type = "Array<boolean>";
break;
case "LI":
case "LN":
type = "Array<number>";
break;
case "LS":
type = "Array<string>";
break;
case "MB":
type = "Record<string, boolean>";
break;
case "MI":
case "MN":
type = "Record<string, number>";
break;
case "MS":
type = "Record<string, string>";
break;
case "L":
{
const t = BasicTypeTransformer(params!.split("#")[0]);
type = `Array<${t}>`;
}
break;
case "LE":
{
const ts = params!.split("#")[0].split(",");
const tbl = [];
for (let i = 0; i < ts.length; i++) {
tbl.push(BasicTypeTransformer(ts[i]));
}
type = `[${tbl.join(", ")}]`;
}
break;
case "M":
{
const t = BasicTypeTransformer(params!.split("#")[0]);
type = `Record<string, ${t}>`;
}
break;
case "MA":
{
const map = [];
const ts = params!.split("#")[0].split(",");
const t1 = BasicTypeTransformer(ts[0]);
for (let i = 1; i < ts.length; i++) {
const t2 = ts[i];
map.push(`${t2}: ${t1}`);
}
type = `{ ${map.join("; ")} }`;
}
break;
case "ME":
{
const map = [];
const ts = params!.split("#")[0].split(";");
for (let i = 0; i < ts.length; i++) {
const [k, v] = ts[i].split(",");
const t = BasicTypeTransformer(v);
map.push(`${k}: ${t}`);
}
type = `{ ${map.join("; ")} }`;
}
break;
}
return type;
}
/**
* 规则解析器
*/
export class Ruler {
private static _rules: Map<string, IRule<any>> = new Map();
public static initialize() {
this.register<boolean>({ identifier: "B", parser: BooleanParser, transformer: BasicTypeTransformer });
this.register<number>({ identifier: "I", parser: IntegerParser, transformer: BasicTypeTransformer });
this.register<number>({ identifier: "N", parser: NumberParser, transformer: BasicTypeTransformer });
this.register<string>({ identifier: "S", parser: StringParser, transformer: BasicTypeTransformer });
this.register<number>({ identifier: "P", parser: PickParser, transformer: BasicTypeTransformer });
this.register<any[]>({ identifier: "L", parser: ListParser, transformer: BasicTypeTransformer });
this.register<any[]>({ identifier: "LE", parser: ListItemParser, transformer: BasicTypeTransformer });
this.register<boolean[]>({ identifier: "LB", parser: ListBooleanParser, transformer: BasicTypeTransformer });
this.register<number[]>({ identifier: "LI", parser: ListIntegerParser, transformer: BasicTypeTransformer });
this.register<number[]>({ identifier: "LN", parser: ListNumberParser, transformer: BasicTypeTransformer });
this.register<string[]>({ identifier: "LS", parser: ListStringParser, transformer: BasicTypeTransformer });
this.register<Record<string, any>>({ identifier: "M", parser: MapParser, transformer: BasicTypeTransformer });
this.register<Record<string, boolean>>({
identifier: "MB",
parser: MapBooleanParser,
transformer: BasicTypeTransformer,
});
this.register<Record<string, number>>({
identifier: "MI",
parser: MapIntegerParser,
transformer: BasicTypeTransformer,
});
this.register<Record<string, number>>({ identifier: "MN", parser: MapNumberParser, transformer: BasicTypeTransformer });
this.register<Record<string, string>>({ identifier: "MS", parser: MapStringParser, transformer: BasicTypeTransformer });
this.register<Record<string, any>>({ identifier: "MA", parser: MapAllParser, transformer: BasicTypeTransformer });
this.register<Record<string, any>>({ identifier: "ME", parser: MapItemParser, transformer: BasicTypeTransformer });
}
/**
* 注册规则
* @param rule 规则
* @param replace 是否替换原规则(默认否)
*/
public static register<RET>(rule: IRule<RET>, replace: boolean = false) {
if (replace || !this._rules.has(rule.identifier)) {
this._rules.set(rule.identifier, rule);
} else {
throw new Error("规则已存在,如需替换,请将 replace 置为真:" + rule.identifier);
}
}
/**
* 根据规则解析文本
* @param rule 规则文本
* @param text 待解析文本
* @returns
*/
public static parse(rule: string, text: string) {
const [identifier, params] = rule.split("=");
const ruler = this._rules.get(identifier);
if (ruler) {
return ruler.parser(text, params);
} else {
throw new Error("未注册的规则解析器:" + rule);
}
}
/**
* 根据规则转换文本类型
* @param rule 规则文本
* @returns
*/
public static transform(rule: string) {
const [identifier, params] = rule.split("=");
const ruler = this._rules.get(identifier);
if (ruler) {
return ruler.transformer(identifier, params);
} else {
throw new Error("未注册的规则转换器:" + rule);
}
}
}