From da281ea9ece0d516ac9d13ae042cfe80bb5d39f4 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Tue, 15 Oct 2024 16:42:42 +0800 Subject: [PATCH] perf: variable replace rang (#2923) --- packages/global/common/string/tools.ts | 5 +++-- packages/global/core/workflow/utils.ts | 15 ++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/global/common/string/tools.ts b/packages/global/common/string/tools.ts index eabf8d685..bc324c804 100644 --- a/packages/global/common/string/tools.ts +++ b/packages/global/common/string/tools.ts @@ -33,9 +33,9 @@ export function replaceVariable(text: any, obj: Record) 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, '\\$&'); diff --git a/packages/global/core/workflow/utils.ts b/packages/global/core/workflow/utils.ts index 34b1bc1db..aa7e66dd1 100644 --- a/packages/global/core/workflow/utils.ts +++ b/packages/global/core/workflow/utils.ts @@ -398,15 +398,12 @@ export function replaceEditorVariable({ // Replace {{$xxx.xxx$}} to value for (const key in allVariables) { - const val = allVariables[key]; - const regex = new RegExp(`\\{\\{\\$(${val.nodeId}\\.${val.id})\\$\\}\\}`, 'g'); - if (['string', 'number'].includes(typeof val.value)) { - text = text.replace(regex, String(val.value)); - } else if (['object'].includes(typeof val.value)) { - text = text.replace(regex, JSON.stringify(val.value)); - } else { - continue; - } + const variable = allVariables[key]; + const val = variable.value; + const formatVal = typeof val === 'object' ? JSON.stringify(val) : String(val); + + const regex = new RegExp(`\\{\\{\\$(${variable.nodeId}\\.${variable.id})\\$\\}\\}`, 'g'); + text = text.replace(regex, formatVal); } return text || ''; }