|
1 | 1 | import RuleInterface from "./RuleInterface";
|
| 2 | +import {dealFileName} from "./RuleInterface"; |
2 | 3 | import FileObj from "../../vo/FileObj";
|
3 | 4 | import path from 'path';
|
4 | 5 |
|
5 | 6 | export default class DeleteRule implements RuleInterface {
|
6 |
| - /** |
7 |
| - * 类别:deletePart:部分删除,deleteAll:全部删除 |
8 |
| - */ |
9 |
| - type: string; |
10 |
| - /** |
11 |
| - * 部分删除时的开始信息 |
12 |
| - */ |
13 |
| - start: DeleteRuleItem; |
14 |
| - /** |
15 |
| - * 部分删除时的结束信息 |
| 7 | + /** |
| 8 | + * 类别:deletePart:部分删除,deleteAll:全部删除 |
| 9 | + */ |
| 10 | + type: string; |
| 11 | + /** |
| 12 | + * 部分删除时的开始信息 |
| 13 | + */ |
| 14 | + start: DeleteRuleItem; |
| 15 | + /** |
| 16 | + * 部分删除时的结束信息 |
16 | 17 |
|
17 |
| - */ |
18 |
| - end: DeleteRuleItem; |
19 |
| - /** |
20 |
| - * 忽略拓展名,true:忽略,false:不忽略 |
21 |
| - */ |
22 |
| - ignorePostfix: boolean; |
| 18 | + */ |
| 19 | + end: DeleteRuleItem; |
| 20 | + /** |
| 21 | + * 忽略拓展名,true:忽略,false:不忽略 |
| 22 | + */ |
| 23 | + ignorePostfix: boolean; |
| 24 | + /* |
| 25 | + * 是否区分大小写 |
| 26 | + */ |
| 27 | + regI: boolean; |
23 | 28 |
|
24 |
| - constructor(data: any) { |
25 |
| - this.type = data.type; |
26 |
| - this.start = new DeleteRuleItem(data.start); |
27 |
| - this.end = new DeleteRuleItem(data.end); |
28 |
| - this.ignorePostfix = data.ignorePostfix; |
29 |
| - } |
| 29 | + constructor(data: any) { |
| 30 | + this.type = data.type; |
| 31 | + this.regI = data.regI != undefined && data.regI; |
| 32 | + this.start = new DeleteRuleItem(data.start, this.regI); |
| 33 | + this.end = new DeleteRuleItem(data.end, this.regI); |
| 34 | + this.ignorePostfix = data.ignorePostfix; |
| 35 | + } |
30 | 36 |
|
31 | 37 |
|
32 |
| - |
33 |
| - deal(file: FileObj): void { |
34 |
| - if (this.type === 'deleteAll') { |
35 |
| - file.realName = ""; |
36 |
| - if (!this.ignorePostfix) { |
37 |
| - file.expandName = ""; |
38 |
| - } |
39 |
| - } else { |
40 |
| - let str = file.realName + (this.ignorePostfix ? "" : file.expandName); |
41 |
| - let startIndex = this.start.calIndex(str); |
42 |
| - let endIndex = this.end.calIndex(str); |
43 |
| - if (startIndex < 0 || endIndex < 0) { |
44 |
| - return; |
45 |
| - } |
46 |
| - str = str.substring(0, startIndex) + str.substring(endIndex + 1); |
47 |
| - if (this.ignorePostfix) { |
48 |
| - file.realName = str; |
49 |
| - } else { |
50 |
| - file.expandName = path.extname(str); |
51 |
| - if (file.expandName.length > 0) { |
52 |
| - file.realName = str.substring(0, str.lastIndexOf(".")); |
53 |
| - } else { |
54 |
| - file.realName = str; |
55 |
| - } |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - file.name = file.realName + file.expandName; |
60 |
| - } |
| 38 | + deal(file: FileObj): void { |
| 39 | + let target = ""; |
| 40 | + if (this.type === 'deleteAll') { |
| 41 | + target = ""; |
| 42 | + } else { |
| 43 | + let str = file.realName + (this.ignorePostfix ? "" : file.expandName); |
| 44 | + let startIndex = this.start.calIndex(str, false); |
| 45 | + let endIndex = this.end.calIndex(str, true); |
| 46 | + if (startIndex < 0 || endIndex < 0 || startIndex > endIndex) { |
| 47 | + return; |
| 48 | + } |
| 49 | + str = str.substring(0, startIndex) + str.substring(endIndex + 1); |
| 50 | + target = str; |
| 51 | + } |
| 52 | + dealFileName(file, target, this.ignorePostfix); |
| 53 | + } |
61 | 54 |
|
62 | 55 | }
|
63 | 56 |
|
64 | 57 | class DeleteRuleItem {
|
65 |
| - /** |
66 |
| - * location:位置,text:文本,end:直到末尾 |
67 |
| - */ |
68 |
| - type: string; |
69 |
| - /** |
70 |
| - * 对应的值 |
71 |
| - */ |
72 |
| - value: string; |
| 58 | + /** |
| 59 | + * location:位置,text:文本,end:直到末尾 |
| 60 | + */ |
| 61 | + type: string; |
| 62 | + /** |
| 63 | + * 对应的值 |
| 64 | + */ |
| 65 | + value: string; |
| 66 | + /** |
| 67 | + * 正则对象 |
| 68 | + */ |
| 69 | + reg: RegExp; |
73 | 70 |
|
74 |
| - constructor(data: any) { |
75 |
| - this.type = data.type; |
76 |
| - this.value = data.value; |
77 |
| - } |
| 71 | + constructor(data: any, regI: boolean) { |
| 72 | + this.type = data.type; |
| 73 | + this.value = data.value; |
| 74 | + if (this.type === 'reg') { |
| 75 | + this.reg = regI ? new RegExp(this.value) : new RegExp(this.value, 'i'); |
| 76 | + } |
| 77 | + } |
78 | 78 |
|
79 |
| - /** |
80 |
| - * 计算位置 |
81 |
| - */ |
82 |
| - calIndex(str: string): number { |
83 |
| - if (this.type === 'location') { |
84 |
| - return parseInt(this.value) - 1; |
85 |
| - } else if (this.type === 'text') { |
86 |
| - return str.indexOf(this.value); |
87 |
| - } else if (this.type === 'end') { |
88 |
| - return str.length - 1; |
89 |
| - } |
90 |
| - return -1; |
91 |
| - } |
| 79 | + /** |
| 80 | + * 计算位置 |
| 81 | + * @param str 字符串 |
| 82 | + * @param end 是否末尾计算 |
| 83 | + */ |
| 84 | + calIndex(str: string, end: boolean): number { |
| 85 | + if (this.type === 'location') { |
| 86 | + let val = parseInt(this.value); |
| 87 | + return val > 0 ? val - 1 : str.length + val; |
| 88 | + } else if (this.type === 'text') { |
| 89 | + let index = str.indexOf(this.value); |
| 90 | + return index + (end ? this.value.length - 1 : 0); |
| 91 | + } else if (this.type === 'end') { |
| 92 | + return str.length - 1; |
| 93 | + } else if (this.type === 'reg') { |
| 94 | + let res = this.reg.exec(str); |
| 95 | + return res == null ? -1 : (res.index + (end ? res[0].length - 1 : 0)); |
| 96 | + } |
| 97 | + return -1; |
| 98 | + } |
92 | 99 | }
|
0 commit comments