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