mirror of
https://github.com/al0cam/AutoMover.git
synced 2026-07-22 12:10:26 +00:00
125 lines
2.8 KiB
TypeScript
125 lines
2.8 KiB
TypeScript
import type { MovingRule } from "Models/MovingRule";
|
|
import type { TFile } from "obsidian";
|
|
|
|
class RuleMatcherUtil {
|
|
private static instance: RuleMatcherUtil;
|
|
|
|
private constructor() { }
|
|
|
|
public static getInstance(): RuleMatcherUtil {
|
|
if (!RuleMatcherUtil.instance) {
|
|
RuleMatcherUtil.instance = new RuleMatcherUtil();
|
|
}
|
|
return RuleMatcherUtil.instance;
|
|
}
|
|
|
|
/**
|
|
* Returns the first rule that matches the file
|
|
* If no rule matches, returns null
|
|
*
|
|
* @param file
|
|
* @param rules
|
|
* @returns MovingRule | null
|
|
*/
|
|
public getMatchingRule(file: TFile, rules: MovingRule[]): MovingRule | null {
|
|
for (const rule of rules) {
|
|
if (rule.regex == null || rule.regex === "") {
|
|
console.error("Rule does not have a regex: ", rule);
|
|
continue;
|
|
}
|
|
if (!this.isValidRegex(rule.regex)) {
|
|
console.error("Rule has an invalid regex: ", rule);
|
|
continue;
|
|
}
|
|
if (file.name.match(rule.regex)) {
|
|
return rule;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Returns the regex groups of the file that match the rule
|
|
* If no groups match, returns an empty array
|
|
*
|
|
* @param file
|
|
* @param rule
|
|
* @returns string[]
|
|
*/
|
|
public getGroupMatches(
|
|
file: TFile,
|
|
rule: MovingRule,
|
|
): RegExpMatchArray | null {
|
|
const matches = file.name.match(rule.regex);
|
|
return matches;
|
|
}
|
|
|
|
/**
|
|
* Checks if the regex pattern is valid
|
|
* Meaning, syntax errors are caught, but not logical errors
|
|
*
|
|
* @param pattern
|
|
* @returns boolean
|
|
*/
|
|
private isValidRegex(pattern: string): boolean {
|
|
try {
|
|
new RegExp(pattern);
|
|
return true;
|
|
} catch (e) {
|
|
if (e instanceof SyntaxError) {
|
|
console.error(`Invalid regex pattern: ${e.message}`);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public constructFinalDesinationPath(
|
|
rule: MovingRule,
|
|
matches: RegExpMatchArray,
|
|
): string {
|
|
let folderPath = rule.folder;
|
|
const unnamedGroups = this.getUnnamedGroups(rule.regex);
|
|
for (let i = 0; i < unnamedGroups!.length; i++) {
|
|
folderPath = folderPath.replace(`$${i + 1}`, matches[i + 1] ?? "");
|
|
}
|
|
return folderPath;
|
|
}
|
|
|
|
/**
|
|
* Is regex grouped
|
|
* Groups are defined by (...) and used with $1, $2, $3, etc.
|
|
*
|
|
* @param rule
|
|
* @returns boolean
|
|
*/
|
|
public isRegexGrouped(rule: MovingRule): boolean {
|
|
return (
|
|
this.doesDestinationUseGroups(rule) &&
|
|
this.getUnnamedGroups(rule.regex) != null
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns the unnamed groups in the regex pattern
|
|
*
|
|
* @param pattern
|
|
* @returns RegExpMatchArray | null
|
|
*/
|
|
private getUnnamedGroups(pattern: string): RegExpMatchArray | null {
|
|
return pattern.match(/\(/g);
|
|
}
|
|
|
|
/**
|
|
* Does destination use groups
|
|
* Groups are defined by $1, $2, $3, etc.
|
|
*
|
|
* @param rule
|
|
* @returns boolean
|
|
*/
|
|
private doesDestinationUseGroups(rule: MovingRule): boolean {
|
|
return /\$\d/.test(rule.folder);
|
|
}
|
|
}
|
|
|
|
const ruleMatcherUtil = RuleMatcherUtil.getInstance();
|
|
export default ruleMatcherUtil;
|