Skip to content

Commit

Permalink
[AMR-86] feat: add support for brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
N0chteil committed Oct 21, 2023
1 parent 415eb94 commit 0a8024e
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/utils/replaceVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export class replaceVariables {
if (!config) return undefined;

// e.g. "%title% - %album%" => "-"
const separator =
config
.split(/%title%|%album%|%artist%|%year%|%version%/g)
.find((e) => e.length > 0)
?.trim()
?.replace(/[a-zA-Z0-9 ]/g, "") ?? "-";
const separator = config
.replace(/\([^)]*\)/g, "") // Remove all brackets w/ content
?.split(/%title%|%album%|%artist%|%year%|%version%/g)
?.find((e) => e.length > 0)
?.trim()
?.replace(/[a-zA-Z0-9 ]/g, "");

let returnStr = "";

Expand Down Expand Up @@ -53,13 +53,34 @@ export class replaceVariables {
);

testStr
.replace(/\([^)]*_NOT_AV_[^)]*\)/g, "") // Remove all brackets w/ content if variable is not available
.split(/_[a-zA-Z]*_NOT_AV_|-/g)
.filter((e) => {
return e.trim() !== undefined && e.trim() !== "";
})
.forEach((e) => {
e = e.trim();

const regexBracketsMatch = e.match(/\([^)]+\)/g);

regexBracketsMatch?.forEach((bracketE) => {
const regexMatch =
bracketE.match(/_[a-zA-Z]*_IS_AV_|-/g)?.[0] ?? "",
cfgElement = regexMatch
.replace("_IS_AV_", "")
.slice(1)
.toLowerCase(),
cfgValue = this.getValue(cfgElement);

let tempE = e;

if (bracketE.includes("_IS_AV_"))
tempE = tempE.replace(regexMatch, cfgValue);
else tempE = tempE.replace(bracketE, "");

e = tempE;
});

// e.g. "_TITLE_IS_AV" => "title"
const regexMatch = e.match(/_[a-zA-Z]*_IS_AV_|-/g)?.[0] ?? "",
cfgElement = regexMatch
Expand All @@ -75,6 +96,7 @@ export class replaceVariables {
(!cfgValue && e !== config.replace(regex, "").trim()))
)
return;

if (returnStr) returnStr += ` ${separator} `;

returnStr +=
Expand Down

0 comments on commit 0a8024e

Please sign in to comment.