Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
da281ea9
authored
Oct 15, 2024
by
Archer
Committed by
GitHub
Oct 15, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: variable replace rang (#2923)
parent
4f1ce640
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
11 deletions
+9
-11
packages/global/common/string/tools.ts
+3
-2
packages/global/core/workflow/utils.ts
+6
-9
No files found.
packages/global/common/string/tools.ts
View file @
da281ea9
...
...
@@ -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
,
'\\$&'
);
...
...
packages/global/core/workflow/utils.ts
View file @
da281ea9
...
...
@@ -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
||
''
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment