93 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-11-27 20:22:42 +08:00
import RuleInterface from "./RuleInterface";
import FileObj from "../../vo/FileObj";
import path from 'path';
export default class DeleteRule implements RuleInterface {
/**
* deletePart:部分删除deleteAll:全部删除
*/
type: string;
/**
*
*/
start: DeleteRuleItem;
/**
*
*/
end: DeleteRuleItem;
/**
* true:false
*/
ignorePostfix: boolean;
constructor(data: any) {
this.type = data.type;
this.start = new DeleteRuleItem(data.start);
this.end = new DeleteRuleItem(data.end);
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);
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 {
/**
* location:位置text:文本end:直到末尾
*/
type: string;
/**
*
*/
value: string;
constructor(data: any) {
this.type = data.type;
this.value = data.value;
}
/**
*
*/
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;
}
}