perf: variable replace rang (#2923)

This commit is contained in:
Archer
2024-10-15 16:42:42 +08:00
committed by GitHub
parent 4f1ce640a7
commit da281ea9ec
2 changed files with 9 additions and 11 deletions

View File

@@ -33,9 +33,9 @@ export function replaceVariable(text: any, obj: Record<string, string | number>)
for (const key in obj) {
const val = obj[key];
if (!['string', 'number'].includes(typeof val)) continue;
const formatVal = typeof val === 'object' ? JSON.stringify(val) : String(val);
text = text.replace(new RegExp(`{{(${key})}}`, 'g'), String(val));
text = text.replace(new RegExp(`{{(${key})}}`, 'g'), formatVal);
}
return text || '';
}
@@ -63,6 +63,7 @@ export const getNanoid = (size = 12) => {
return `${firstChar}${randomsStr}`;
};
export const customNanoid = (str: string, size: number) => customAlphabet(str, size)();
/* Custom text to reg, need to replace special chats */
export const replaceRegChars = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');