-
-
Notifications
You must be signed in to change notification settings - Fork 852
Enhance blacklist handling: regex replacements and partial date removal #5718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run make fmt-ui
to fix up the prettier errors.
Edit: don't forget to add some documentation somewhere
const regexReplacements = new Map<string, string>(); | ||
const regexs = blacklist | ||
.map((entry) => { | ||
let [pattern, replacement] = entry.split("||"); // Extract regex and replacement | ||
|
||
try { | ||
const compiledRegex = new RegExp(pattern, "gi"); | ||
|
||
if (replacement !== undefined ){ | ||
regexReplacements.set(compiledRegex.source, replacement); // Store replacement | ||
} | ||
|
||
return compiledRegex; | ||
} catch { | ||
return null; // Ignore invalid regex patterns | ||
} | ||
}) | ||
.filter((r) => r !== null) as RegExp[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not ideal to use map
and then mutate stuff outside it. Instead, I would return a tuple (return [compiledRegex, replacement]
). Then, these pairs of values can be handled in the code below. I would also move this into a separate function.
regexs.forEach((regex) => { | ||
const replacement = regexReplacements.get(regex.source); | ||
if (replacement) { | ||
str = str.replace(regex, (match:string, ...groups: string[]) => { | ||
return replacement.replace(/\\(\d+)/g, (_, groupIndex) => groups[parseInt(groupIndex, 10) - 1] || ""); | ||
}); | ||
} else { | ||
str = str.replace(regex, ""); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be moved into a separate function. The code between 154-156 needs some explanation.
So this is still a draft as I am TERRIBLE with Regex and just as bad with coding. Please let me know if there is anything that needs fixed, I'm happy to do so.
||
delimiter to allow custom replacements (e.g., "bwb||bigwetbutts")-DD
) fromYYYY-MM-DD
while keepingYYYY-MM
"(\d{4}-\d{2})-\d{2}||$1"
removes-DD
but preservesYYYY-MM
try/catch