feat:删除规则支持正则表达式,同时位置支持负数(从末尾计算,-1表示到倒数第一个字符)

This commit is contained in:
fanxb 2024-11-07 23:18:38 +08:00
parent 95a382835a
commit f85890bda8
2 changed files with 132 additions and 95 deletions

View File

@ -20,16 +20,17 @@ export default class DeleteRule implements RuleInterface {
* true:false
*/
ignorePostfix: boolean;
regI: boolean;
constructor(data: any) {
this.type = data.type;
this.start = new DeleteRuleItem(data.start);
this.end = new DeleteRuleItem(data.end);
this.regI = data.regI != undefined && data.regI;
this.start = new DeleteRuleItem(data.start, this.regI);
this.end = new DeleteRuleItem(data.end, this.regI);
this.ignorePostfix = data.ignorePostfix;
}
deal(file: FileObj): void {
if (this.type === 'deleteAll') {
file.realName = "";
@ -38,9 +39,9 @@ export default class DeleteRule implements RuleInterface {
}
} else {
let str = file.realName + (this.ignorePostfix ? "" : file.expandName);
let startIndex = this.start.calIndex(str);
let endIndex = this.end.calIndex(str);
if (startIndex < 0 || endIndex < 0) {
let startIndex = this.start.calIndex(str, false);
let endIndex = this.end.calIndex(str, true);
if (startIndex < 0 || endIndex < 0 || startIndex > endIndex) {
return;
}
str = str.substring(0, startIndex) + str.substring(endIndex + 1);
@ -70,22 +71,35 @@ class DeleteRuleItem {
*
*/
value: string;
/**
*
*/
reg: RegExp;
constructor(data: any) {
constructor(data: any, regI: boolean) {
this.type = data.type;
this.value = data.value;
if (this.type === 'reg') {
this.reg = regI ? new RegExp(this.value) : new RegExp(this.value, 'i');
}
}
/**
*
* @param str
* @param end
*/
calIndex(str: string): number {
calIndex(str: string, end: boolean): number {
if (this.type === 'location') {
return parseInt(this.value) - 1;
let val = parseInt(this.value);
return val > 0 ? val - 1 : str.length + val;
} else if (this.type === 'text') {
return str.indexOf(this.value);
} else if (this.type === 'end') {
return str.length - 1;
} else if (this.type === 'reg') {
let res = this.reg.exec(str);
return res == null ? -1 : (res.index + (end ? 0 : res[0].length));
}
return -1;
}

View File

@ -12,6 +12,10 @@
<el-radio v-model="ruleObj.data.start.type" label="text" :disabled="deleteAll">文本:</el-radio>
<el-input v-model="startText" size="small" :disabled="deleteAll"/>
</div>
<div class="line">
<el-radio v-model="ruleObj.data.start.type" label="reg" :disabled="deleteAll">正则:</el-radio>
<el-input v-model="startReg" size="small" :disabled="deleteAll"/>
</div>
</div>
<div style="margin-left: 4em">
<div>结束</div>
@ -23,12 +27,20 @@
<el-radio v-model="ruleObj.data.end.type" label="text" :disabled="deleteAll">文本:</el-radio>
<el-input v-model="endText" size="small" :disabled="deleteAll"/>
</div>
<div class="line">
<el-radio v-model="ruleObj.data.end.type" label="reg" :disabled="deleteAll">正则:</el-radio>
<el-input v-model="endReg" size="small" :disabled="deleteAll"/>
</div>
<div class="line">
<el-radio v-model="ruleObj.data.end.type" label="end" :disabled="deleteAll">直到末尾</el-radio>
</div>
</div>
</div>
</div>
<div v-if="ruleObj.data.start.type==='reg' || ruleObj.data.end.type==='reg'" class="flex">
<div class="left">区分大小写:</div>
<el-switch v-model="ruleObj.data.regI"/>
</div>
<div class="flex">
<div class="left">全部删除:</div>
<el-switch v-model="deleteAll" @change="allDeleteChange"/>
@ -60,28 +72,36 @@ export default {
value: "",
},
ignorePostfix: true,
regI: false,//reg
},
},
startIndex: 1,
endIndex: 1,
startText: "",
startReg: "",
endText: "",
endReg: "",
deleteAll: false,
};
},
created() {
if (this.editRule) {
this.ruleObj = JSON.parse(JSON.stringify(this.editRule));
if (this.ruleObj.data.type == "deletePart") {
if (this.ruleObj.data.start.type == "location") {
if (this.ruleObj.data.type === "deletePart") {
if (this.ruleObj.data.start.type === "location") {
this.startIndex = parseInt(this.ruleObj.data.start.value);
} else {
} else if (this.ruleObj.data.start.type === "text") {
this.startText = this.ruleObj.data.start.value;
}
if (this.ruleObj.data.end.type == "location") {
this.endIndex = parseInt(this.ruleObj.data.end.value);
} else {
this.startReg = this.ruleObj.data.start.value;
}
if (this.ruleObj.data.end.type === "location") {
this.endIndex = parseInt(this.ruleObj.data.end.value);
} else if (this.ruleObj.data.end.type === "text") {
this.endText = this.ruleObj.data.end.value;
} else {
this.endReg = this.ruleObj.data.end.value;
}
} else {
this.deleteAll = true;
@ -94,10 +114,12 @@ export default {
this.$message({message: "请填写完整", type: "warning"});
return null;
}
if (this.ruleObj.data.type == "deletePart") {
if (this.ruleObj.data.type === "deletePart") {
if (
(this.ruleObj.data.start.type == "text" && this.startText.length == 0) ||
(this.ruleObj.data.start.type == "text" && this.startText.length == 0)
('text' === this.ruleObj.data.start.type && this.startText.length === 0) ||
('text' === this.ruleObj.data.end.type && this.endText.length === 0) ||
('reg' === this.ruleObj.data.start.type && this.startReg.length === 0) ||
('reg' === this.ruleObj.data.end.type && this.endReg.length === 0)
) {
this.$message({
message: "开始或者结束文本不能为空",
@ -106,19 +128,20 @@ export default {
return null;
}
}
this.ruleObj.data.start.value = this.ruleObj.data.start.type == "location" ? this.startIndex.toString() : this.startText;
this.ruleObj.data.end.value = this.ruleObj.data.end.type == "location" ? this.endIndex.toString() : this.endText;
let startType = this.ruleObj.data.start.type;
this.ruleObj.data.start.value = startType === "location" ? this.startIndex.toString() : startType === 'text' ? this.startText : this.startReg;
let endType = this.ruleObj.data.end.type;
this.ruleObj.data.end.value = endType === "location" ? this.endIndex.toString() : endType === 'text' ? this.endText : this.endReg;
let message = `删除:`;
if (this.deleteAll) {
message += "全部删除";
} else {
message += `从"${this.ruleObj.data.start.value}"到"${this.ruleObj.data.end.type == "untilEnd" ? "末尾" : this.ruleObj.data.end.value}"`;
message += `从"${this.ruleObj.data.start.value}"到"${this.ruleObj.data.end.type === "end" ? "末尾" : this.ruleObj.data.end.value}"`;
}
this.ruleObj.message = message;
return this.ruleObj;
},
allDeleteChange(val) {
console.log(val);
this.deleteAll = val;
this.ruleObj.data.type = val ? "deleteAll" : "deletePart";
},