Skip to content

Commit

Permalink
refactor regex
Browse files Browse the repository at this point in the history
  • Loading branch information
KentoNishi committed Jun 7, 2024
1 parent 08d276c commit ce0208b
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions src/ts/ytcf-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ export function shouldFilterMessage(action: Chat.MessageAction): boolean {
if (!condition.value) {
numValidFilters--;
} else {
try {
const regex = parseRegex(condition.value);
const result = regex.test(compStr);
if (result === condition.invert) {
break;
}
} catch (e) {
console.error(e);
// try {
// const regex = parseRegex(condition.value);
// const result = regex.test(compStr);
// if (result === condition.invert) {
// break;
// }
// } catch (e) {
// console.error(e);
// break;
// }
const { result, error } = testRegex(condition.value, compStr);
if (error || result === condition.invert) {
break;
}
}
Expand All @@ -84,6 +88,22 @@ export function shouldFilterMessage(action: Chat.MessageAction): boolean {
return false;
}

export const testRegex = (expression: string, value: string): { result: boolean; error: Error | false; } => {
try {
const regex = parseRegex(expression);
return {
result: regex.test(value),
error: false
};
} catch (e) {
console.error(e);
return {
result: false,
error: e as Error
};
}
}

export function shouldActivatePreset(preset: YtcF.FilterPreset, info: SimpleVideoInfo): boolean {
for (const trigger of preset.triggers) {
let compStr = '';
Expand Down Expand Up @@ -111,13 +131,15 @@ export function shouldActivatePreset(preset: YtcF.FilterPreset, info: SimpleVide
const result = s1[trigger.type](s2);
if (result) return true;
} else {
try {
const regex = parseRegex(trigger.value);
const result = regex.test(compStr);
if (result) return true;
} catch (e) {
console.error(e);
}
// try {
// const regex = parseRegex(trigger.value);
// const result = regex.test(compStr);
// if (result) return true;
// } catch (e) {
// console.error(e);
// }
const { result } = testRegex(trigger.value, compStr);
if (result) return true;
}
}
return false;
Expand Down

0 comments on commit ce0208b

Please sign in to comment.