-6 loops overall by only incrementing loop count when regex/str.replace is being called

This commit is contained in:
Ean Milligan 2025-08-06 14:50:20 -04:00
parent c58ebcc9f9
commit 2cfa923093
1 changed files with 7 additions and 5 deletions

View File

@ -11,13 +11,15 @@ import { loggingEnabled } from 'artigen/utils/logFlag.ts';
export const escapeCharacters = (str: string, esc: string): string => { export const escapeCharacters = (str: string, esc: string): string => {
// Loop thru each esc char one at a time // Loop thru each esc char one at a time
for (const e of esc) { for (const e of esc) {
loggingEnabled && log(LT.LOG, `Escaping character ${e} | ${str}, ${esc}`);
if (str.includes(e)) {
loopCountCheck(`escape.ts - escaping character ${e}`); loopCountCheck(`escape.ts - escaping character ${e}`);
loggingEnabled && log(LT.LOG, `Escaping character ${e} | ${str}, ${esc}`);
// Create a new regex to look for that char that needs replaced and escape it // Create a new regex to look for that char that needs replaced and escape it
const tempRgx = new RegExp(`[${e}]`, 'g'); const tempRgx = new RegExp(`[${e}]`, 'g');
str = str.replace(tempRgx, `\\${e}`); str = str.replace(tempRgx, `\\${e}`);
} }
}
return str; return str;
}; };