feat:删除规则支持正则表达式,同时位置支持负数(从末尾计算,-1表示到倒数第一个字符)
This commit is contained in:
parent
95a382835a
commit
f85890bda8
@ -3,90 +3,104 @@ import FileObj from "../../vo/FileObj";
|
||||
import path from 'path';
|
||||
|
||||
export default class DeleteRule implements RuleInterface {
|
||||
/**
|
||||
* 类别:deletePart:部分删除,deleteAll:全部删除
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* 部分删除时的开始信息
|
||||
*/
|
||||
start: DeleteRuleItem;
|
||||
/**
|
||||
* 部分删除时的结束信息
|
||||
/**
|
||||
* 类别:deletePart:部分删除,deleteAll:全部删除
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* 部分删除时的开始信息
|
||||
*/
|
||||
start: DeleteRuleItem;
|
||||
/**
|
||||
* 部分删除时的结束信息
|
||||
|
||||
*/
|
||||
end: DeleteRuleItem;
|
||||
/**
|
||||
* 忽略拓展名,true:忽略,false:不忽略
|
||||
*/
|
||||
ignorePostfix: boolean;
|
||||
*/
|
||||
end: DeleteRuleItem;
|
||||
/**
|
||||
* 忽略拓展名,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.ignorePostfix = data.ignorePostfix;
|
||||
}
|
||||
constructor(data: any) {
|
||||
this.type = data.type;
|
||||
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 = "";
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
file.name = file.realName + file.expandName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DeleteRuleItem {
|
||||
/**
|
||||
* location:位置,text:文本,end:直到末尾
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* 对应的值
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* location:位置,text:文本,end:直到末尾
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* 对应的值
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* 正则对象
|
||||
*/
|
||||
reg: RegExp;
|
||||
|
||||
constructor(data: any) {
|
||||
this.type = data.type;
|
||||
this.value = data.value;
|
||||
}
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算位置
|
||||
*/
|
||||
calIndex(str: string): number {
|
||||
if (this.type === 'location') {
|
||||
return parseInt(this.value) - 1;
|
||||
} else if (this.type === 'text') {
|
||||
return str.indexOf(this.value);
|
||||
} else if (this.type === 'end') {
|
||||
return str.length - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
* 计算位置
|
||||
* @param str 字符串
|
||||
* @param end 是否末尾计算
|
||||
*/
|
||||
calIndex(str: string, end: boolean): number {
|
||||
if (this.type === 'location') {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -6,22 +6,30 @@
|
||||
<div>开始</div>
|
||||
<div class="line">
|
||||
<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 class="line">
|
||||
<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 style="margin-left: 4em">
|
||||
<div>结束</div>
|
||||
<div class="line">
|
||||
<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 class="line">
|
||||
<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 class="line">
|
||||
<el-radio v-model="ruleObj.data.end.type" label="end" :disabled="deleteAll">直到末尾</el-radio>
|
||||
@ -29,14 +37,18 @@
|
||||
</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" />
|
||||
<el-switch v-model="deleteAll" @change="allDeleteChange"/>
|
||||
</div>
|
||||
|
||||
<div class="flex">
|
||||
<div class="left">忽略拓展名:</div>
|
||||
<el-switch v-model="ruleObj.data.ignorePostfix" />
|
||||
<el-switch v-model="ruleObj.data.ignorePostfix"/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@ -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;
|
||||
@ -91,13 +111,15 @@ export default {
|
||||
methods: {
|
||||
exportObj() {
|
||||
if (this.ruleObj.data.type.length == 0) {
|
||||
this.$message({ message: "请填写完整", type: "warning" });
|
||||
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";
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user