54 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-05-10 20:24:29 +08:00
import RuleInterface from "./RuleInterface";
import FileObj from "../../vo/FileObj";
import path from 'path';
export default class ReplaceRule implements RuleInterface {
/**
* 1:替换第一个23
*/
type: number;
/**
*
*/
source: string;
/**
*
*/
target: string;
constructor(data: any) {
this.type = data.type;
this.source = data.source;
this.target = data.target;
}
deal(file: FileObj): void {
let start = 0;
let changed = false;
for (; ;) {
let index = this.type == 1 || this.type == 3 ? file.name.indexOf(this.source, start) : file.name.lastIndexOf(this.source);
if (index > -1) {
file.name = file.name.substring(0, index) + this.target + file.name.substring(index + this.source.length);
start = index + this.target.length;
changed = true;
if (this.type != 3) {
break;
}
} else {
break;
}
}
if (changed) {
file.originName = file.name;
file.expandName = path.extname(file.name);
if (file.expandName && file.expandName.length > 0) {
file.realName = file.name.substring(0, file.name.lastIndexOf("."));
} else {
file.realName = file.name;
}
}
}
}