mirror of https://github.com/langgenius/dify.git
Complete RegExp.exec refactor for performance optimization
Co-authored-by: asukaminato0721 <30024051+asukaminato0721@users.noreply.github.com>
This commit is contained in:
parent
9533b88a9f
commit
b7bfa0ca6e
|
|
@ -108,7 +108,8 @@ const Configuration: FC = () => {
|
|||
const [hasFetchedDetail, setHasFetchedDetail] = useState(false)
|
||||
const isLoading = !hasFetchedDetail
|
||||
const pathname = usePathname()
|
||||
const matched = pathname.match(/\/app\/([^/]+)/)
|
||||
const appIdRegex = /\/app\/([^/]+)/
|
||||
const matched = appIdRegex.exec(pathname)
|
||||
const appId = (matched?.length && matched[1]) ? matched[1] : ''
|
||||
const [mode, setMode] = useState('')
|
||||
const [publishedConfig, setPublishedConfig] = useState<PublishConfig | null>(null)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ const AnnotationReply = ({
|
|||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
const matched = pathname.match(/\/app\/([^/]+)/)
|
||||
const appIdRegex = /\/app\/([^/]+)/
|
||||
const matched = appIdRegex.exec(pathname)
|
||||
const appId = (matched?.length && matched[1]) ? matched[1] : ''
|
||||
const featuresStore = useFeaturesStore()
|
||||
const annotationReply = useFeatures(s => s.features.annotationReply)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,8 @@ const VoiceParamConfig = ({
|
|||
}: VoiceParamConfigProps) => {
|
||||
const { t } = useTranslation()
|
||||
const pathname = usePathname()
|
||||
const matched = pathname.match(/\/app\/([^/]+)/)
|
||||
const appIdRegex = /\/app\/([^/]+)/
|
||||
const matched = appIdRegex.exec(pathname)
|
||||
const appId = (matched?.length && matched[1]) ? matched[1] : ''
|
||||
const text2speech = useFeatures(state => state.features.text2speech)
|
||||
const featuresStore = useFeaturesStore()
|
||||
|
|
|
|||
|
|
@ -11,7 +11,10 @@ export const preprocessLaTeX = (content: string) => {
|
|||
return content
|
||||
|
||||
const codeBlockRegex = /```[\s\S]*?```/g
|
||||
const codeBlocks = content.match(codeBlockRegex) || []
|
||||
const codeBlocks: string[] = []
|
||||
let match
|
||||
while ((match = codeBlockRegex.exec(content)) !== null)
|
||||
codeBlocks.push(match[0])
|
||||
const escapeReplacement = (str: string) => str.replace(/\$/g, '_TMP_REPLACE_DOLLAR_')
|
||||
let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
|
||||
|
||||
|
|
|
|||
|
|
@ -236,7 +236,8 @@ const Flowchart = (props: FlowchartProps) => {
|
|||
.split('\n')
|
||||
.map((line) => {
|
||||
// Gantt charts have specific syntax needs.
|
||||
const taskMatch = line.match(/^\s*([^:]+?)\s*:\s*(.*)/)
|
||||
const taskRegex = /^\s*([^:]+?)\s*:\s*(.*)/
|
||||
const taskMatch = taskRegex.exec(line)
|
||||
if (!taskMatch)
|
||||
return line // Not a task line, return as is.
|
||||
|
||||
|
|
@ -245,7 +246,12 @@ const Flowchart = (props: FlowchartProps) => {
|
|||
|
||||
// Rule 1: Correct multiple "after" dependencies ONLY if they exist.
|
||||
// This is a common mistake, e.g., "..., after task1, after task2, ..."
|
||||
const afterCount = (paramsStr.match(/after /g) || []).length
|
||||
const afterMatches: string[] = []
|
||||
const afterRegex = /after /g
|
||||
let afterMatch
|
||||
while ((afterMatch = afterRegex.exec(paramsStr)) !== null)
|
||||
afterMatches.push(afterMatch[0])
|
||||
const afterCount = afterMatches.length
|
||||
if (afterCount > 1)
|
||||
paramsStr = paramsStr.replace(/,\s*after\s+/g, ' ')
|
||||
|
||||
|
|
|
|||
|
|
@ -167,10 +167,11 @@ export function isMermaidCodeComplete(code: string): boolean {
|
|||
const isBalanced = true
|
||||
|
||||
// Check for common syntax errors
|
||||
const arrowRegex = /\S+\s*-->\s*\S+/
|
||||
const hasNoSyntaxErrors = !trimmedCode.includes('undefined')
|
||||
&& !trimmedCode.includes('[object Object]')
|
||||
&& trimmedCode.split('\n').every(line =>
|
||||
!(line.includes('-->') && !line.match(/\S+\s*-->\s*\S+/)))
|
||||
!(line.includes('-->') && !arrowRegex.test(line)))
|
||||
|
||||
return hasValidStart && isBalanced && hasNoSyntaxErrors
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,8 @@ export const pluginManifestInMarketToPluginProps = (pluginManifest: PluginManife
|
|||
}
|
||||
|
||||
export const parseGitHubUrl = (url: string): GitHubUrlInfo => {
|
||||
const match = url.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+)\/?$/)
|
||||
const regex = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/?$/
|
||||
const match = regex.exec(url)
|
||||
return match ? { isValid: true, owner: match[1], repo: match[2] } : { isValid: false }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,8 @@ export type DuplicateAppModalProps = {
|
|||
|
||||
const DEFAULT_ICON = { type: 'emoji', icon: '🧿', background: '#EFF1F5' }
|
||||
const extractFileId = (url: string) => {
|
||||
const match = url.match(/files\/(.+?)\/file-preview/)
|
||||
const regex = /files\/(.+?)\/file-preview/
|
||||
const match = regex.exec(url)
|
||||
return match ? match[1] : null
|
||||
}
|
||||
const getIcon = (data?: ToolWithProvider) => {
|
||||
|
|
|
|||
|
|
@ -84,7 +84,8 @@ const CodeEditor: FC<Props> = ({
|
|||
|
||||
const getUniqVarName = (varName: string) => {
|
||||
if (varList.find(v => v.variable === varName)) {
|
||||
const match = varName.match(/_(\d+)$/)
|
||||
const regex = /_(\d+)$/
|
||||
const match = regex.exec(varName)
|
||||
|
||||
const index = (() => {
|
||||
if (match)
|
||||
|
|
|
|||
|
|
@ -25,7 +25,8 @@ const SupportVarInput: FC<Props> = ({
|
|||
const renderSafeContent = (inputValue: string) => {
|
||||
const parts = inputValue.split(/(\{\{[^}]+\}\}|\n)/g)
|
||||
return parts.map((part, index) => {
|
||||
const variableMatch = part.match(/^\{\{([^}]+)\}\}$/)
|
||||
const variableRegex = /^\{\{([^}]+)\}\}$/
|
||||
const variableMatch = variableRegex.exec(part)
|
||||
if (variableMatch) {
|
||||
return (
|
||||
<VarHighlight
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ export const extractFunctionParams = (code: string, language: CodeLanguage) => {
|
|||
[CodeLanguage.python3]: /def\s+main\s*\((.*?)\)/,
|
||||
[CodeLanguage.javascript]: /function\s+main\s*\((.*?)\)/,
|
||||
}
|
||||
const match = code.match(patterns[language])
|
||||
const match = patterns[language].exec(code)
|
||||
const params: string[] = []
|
||||
|
||||
if (match?.[1]) {
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ const ConditionItem = ({
|
|||
) {
|
||||
const regex = isCommonVariable ? COMMON_VARIABLE_REGEX : VARIABLE_REGEX
|
||||
const matchedStartNumber = isCommonVariable ? 2 : 3
|
||||
const matched = condition.value.match(regex)
|
||||
const matched = regex.exec(condition.value)
|
||||
|
||||
if (matched?.length) {
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in New Issue