mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
perf: variable replace rang (#2923)
This commit is contained in:
@@ -33,9 +33,9 @@ export function replaceVariable(text: any, obj: Record<string, string | number>)
|
|||||||
|
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
const val = obj[key];
|
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 || '';
|
return text || '';
|
||||||
}
|
}
|
||||||
@@ -63,6 +63,7 @@ export const getNanoid = (size = 12) => {
|
|||||||
|
|
||||||
return `${firstChar}${randomsStr}`;
|
return `${firstChar}${randomsStr}`;
|
||||||
};
|
};
|
||||||
|
export const customNanoid = (str: string, size: number) => customAlphabet(str, size)();
|
||||||
|
|
||||||
/* Custom text to reg, need to replace special chats */
|
/* Custom text to reg, need to replace special chats */
|
||||||
export const replaceRegChars = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
export const replaceRegChars = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
|
@@ -398,15 +398,12 @@ export function replaceEditorVariable({
|
|||||||
|
|
||||||
// Replace {{$xxx.xxx$}} to value
|
// Replace {{$xxx.xxx$}} to value
|
||||||
for (const key in allVariables) {
|
for (const key in allVariables) {
|
||||||
const val = allVariables[key];
|
const variable = allVariables[key];
|
||||||
const regex = new RegExp(`\\{\\{\\$(${val.nodeId}\\.${val.id})\\$\\}\\}`, 'g');
|
const val = variable.value;
|
||||||
if (['string', 'number'].includes(typeof val.value)) {
|
const formatVal = typeof val === 'object' ? JSON.stringify(val) : String(val);
|
||||||
text = text.replace(regex, String(val.value));
|
|
||||||
} else if (['object'].includes(typeof val.value)) {
|
const regex = new RegExp(`\\{\\{\\$(${variable.nodeId}\\.${variable.id})\\$\\}\\}`, 'g');
|
||||||
text = text.replace(regex, JSON.stringify(val.value));
|
text = text.replace(regex, formatVal);
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return text || '';
|
return text || '';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user