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

@ -3,90 +3,104 @@ import FileObj from "../../vo/FileObj";
import path from 'path'; import path from 'path';
export default class DeleteRule implements RuleInterface { export default class DeleteRule implements RuleInterface {
/** /**
* deletePart:部分删除deleteAll:全部删除 * deletePart:部分删除deleteAll:全部删除
*/ */
type: string; type: string;
/** /**
* *
*/ */
start: DeleteRuleItem; start: DeleteRuleItem;
/** /**
* *
*/ */
end: DeleteRuleItem; end: DeleteRuleItem;
/** /**
* true:false * true:false
*/ */
ignorePostfix: boolean; ignorePostfix: boolean;
regI: boolean;
constructor(data: any) { constructor(data: any) {
this.type = data.type; this.type = data.type;
this.start = new DeleteRuleItem(data.start); this.regI = data.regI != undefined && data.regI;
this.end = new DeleteRuleItem(data.end); this.start = new DeleteRuleItem(data.start, this.regI);
this.ignorePostfix = data.ignorePostfix; this.end = new DeleteRuleItem(data.end, this.regI);
} this.ignorePostfix = data.ignorePostfix;
}
deal(file: FileObj): void {
if (this.type === 'deleteAll') {
file.realName = "";
if (!this.ignorePostfix) {
file.expandName = "";
}
} else {
let str = file.realName + (this.ignorePostfix ? "" : file.expandName);
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);
if (this.ignorePostfix) {
file.realName = str;
} else {
file.expandName = path.extname(str);
if (file.expandName.length > 0) {
file.realName = str.substring(0, str.lastIndexOf("."));
} else {
file.realName = str;
}
}
}
deal(file: FileObj): void { file.name = file.realName + file.expandName;
if (this.type === 'deleteAll') { }
file.realName = "";
if (!this.ignorePostfix) {
file.expandName = "";
}
} 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) {
return;
}
str = str.substring(0, startIndex) + str.substring(endIndex + 1);
if (this.ignorePostfix) {
file.realName = str;
} else {
file.expandName = path.extname(str);
if (file.expandName.length > 0) {
file.realName = str.substring(0, str.lastIndexOf("."));
} else {
file.realName = str;
}
}
}
file.name = file.realName + file.expandName;
}
} }
class DeleteRuleItem { class DeleteRuleItem {
/** /**
* location:位置text:文本end:直到末尾 * location:位置text:文本end:直到末尾
*/ */
type: string; type: string;
/** /**
* *
*/ */
value: string; value: string;
/**
*
*/
reg: RegExp;
constructor(data: any) { constructor(data: any, regI: boolean) {
this.type = data.type; this.type = data.type;
this.value = data.value; this.value = data.value;
} if (this.type === 'reg') {
this.reg = regI ? new RegExp(this.value) : new RegExp(this.value, 'i');
}
}
/** /**
* *
*/ * @param str
calIndex(str: string): number { * @param end
if (this.type === 'location') { */
return parseInt(this.value) - 1; calIndex(str: string, end: boolean): number {
} else if (this.type === 'text') { if (this.type === 'location') {
return str.indexOf(this.value); let val = parseInt(this.value);
} else if (this.type === 'end') { return val > 0 ? val - 1 : str.length + val;
return str.length - 1; } else if (this.type === 'text') {
} return str.indexOf(this.value);
return -1; } 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

@ -6,22 +6,30 @@
<div>开始</div> <div>开始</div>
<div class="line"> <div class="line">
<el-radio v-model="ruleObj.data.start.type" label="location" :disabled="deleteAll">位置</el-radio> <el-radio v-model="ruleObj.data.start.type" label="location" :disabled="deleteAll">位置</el-radio>
<el-input-number :min="1" size="small" :disabled="deleteAll" v-model="startIndex" /> <el-input-number :min="1" size="small" :disabled="deleteAll" v-model="startIndex"/>
</div> </div>
<div class="line"> <div class="line">
<el-radio v-model="ruleObj.data.start.type" label="text" :disabled="deleteAll">文本:</el-radio> <el-radio v-model="ruleObj.data.start.type" label="text" :disabled="deleteAll">文本:</el-radio>
<el-input v-model="startText" size="small" :disabled="deleteAll" /> <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> </div>
<div style="margin-left: 4em"> <div style="margin-left: 4em">
<div>结束</div> <div>结束</div>
<div class="line"> <div class="line">
<el-radio v-model="ruleObj.data.end.type" label="location" :disabled="deleteAll">位置</el-radio> <el-radio v-model="ruleObj.data.end.type" label="location" :disabled="deleteAll">位置</el-radio>
<el-input-number size="small" :disabled="deleteAll" v-model="endIndex" /> <el-input-number size="small" :disabled="deleteAll" v-model="endIndex"/>
</div> </div>
<div class="line"> <div class="line">
<el-radio v-model="ruleObj.data.end.type" label="text" :disabled="deleteAll">文本:</el-radio> <el-radio v-model="ruleObj.data.end.type" label="text" :disabled="deleteAll">文本:</el-radio>
<el-input v-model="endText" size="small" :disabled="deleteAll" /> <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>
<div class="line"> <div class="line">
<el-radio v-model="ruleObj.data.end.type" label="end" :disabled="deleteAll">直到末尾</el-radio> <el-radio v-model="ruleObj.data.end.type" label="end" :disabled="deleteAll">直到末尾</el-radio>
@ -29,14 +37,18 @@
</div> </div>
</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="flex">
<div class="left">全部删除:</div> <div class="left">全部删除:</div>
<el-switch v-model="deleteAll" @change="allDeleteChange" /> <el-switch v-model="deleteAll" @change="allDeleteChange"/>
</div> </div>
<div class="flex"> <div class="flex">
<div class="left">忽略拓展名:</div> <div class="left">忽略拓展名:</div>
<el-switch v-model="ruleObj.data.ignorePostfix" /> <el-switch v-model="ruleObj.data.ignorePostfix"/>
</div> </div>
</template> </template>
@ -60,28 +72,36 @@ export default {
value: "", value: "",
}, },
ignorePostfix: true, ignorePostfix: true,
regI: false,//reg
}, },
}, },
startIndex: 1, startIndex: 1,
endIndex: 1, endIndex: 1,
startText: "", startText: "",
startReg: "",
endText: "", endText: "",
endReg: "",
deleteAll: false, deleteAll: false,
}; };
}, },
created() { created() {
if (this.editRule) { if (this.editRule) {
this.ruleObj = JSON.parse(JSON.stringify(this.editRule)); this.ruleObj = JSON.parse(JSON.stringify(this.editRule));
if (this.ruleObj.data.type == "deletePart") { if (this.ruleObj.data.type === "deletePart") {
if (this.ruleObj.data.start.type == "location") { if (this.ruleObj.data.start.type === "location") {
this.startIndex = parseInt(this.ruleObj.data.start.value); this.startIndex = parseInt(this.ruleObj.data.start.value);
} else { } else if (this.ruleObj.data.start.type === "text") {
this.startText = this.ruleObj.data.start.value; this.startText = this.ruleObj.data.start.value;
}
if (this.ruleObj.data.end.type == "location") {
this.endIndex = parseInt(this.ruleObj.data.end.value);
} else { } 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; this.endText = this.ruleObj.data.end.value;
} else {
this.endReg = this.ruleObj.data.end.value;
} }
} else { } else {
this.deleteAll = true; this.deleteAll = true;
@ -91,13 +111,15 @@ export default {
methods: { methods: {
exportObj() { exportObj() {
if (this.ruleObj.data.type.length == 0) { if (this.ruleObj.data.type.length == 0) {
this.$message({ message: "请填写完整", type: "warning" }); this.$message({message: "请填写完整", type: "warning"});
return null; return null;
} }
if (this.ruleObj.data.type == "deletePart") { if (this.ruleObj.data.type === "deletePart") {
if ( if (
(this.ruleObj.data.start.type == "text" && this.startText.length == 0) || ('text' === this.ruleObj.data.start.type && this.startText.length === 0) ||
(this.ruleObj.data.start.type == "text" && 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({ this.$message({
message: "开始或者结束文本不能为空", message: "开始或者结束文本不能为空",
@ -106,19 +128,20 @@ export default {
return null; return null;
} }
} }
this.ruleObj.data.start.value = this.ruleObj.data.start.type == "location" ? this.startIndex.toString() : this.startText; let startType = this.ruleObj.data.start.type;
this.ruleObj.data.end.value = this.ruleObj.data.end.type == "location" ? this.endIndex.toString() : this.endText; 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 = `删除:`; let message = `删除:`;
if (this.deleteAll) { if (this.deleteAll) {
message += "全部删除"; message += "全部删除";
} else { } 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; this.ruleObj.message = message;
return this.ruleObj; return this.ruleObj;
}, },
allDeleteChange(val) { allDeleteChange(val) {
console.log(val);
this.deleteAll = val; this.deleteAll = val;
this.ruleObj.data.type = val ? "deleteAll" : "deletePart"; this.ruleObj.data.type = val ? "deleteAll" : "deletePart";
}, },