Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin-flow-builder: resolve regex in keywords #2797

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/botonic-plugin-flow-builder/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input, PluginPreRequest } from '@botonic/core'
import axios from 'axios'

import { SEPARATOR } from './constants'
import { REG_EXP_PATTERN, SEPARATOR } from './constants'
import {
HtBotActionNode,
HtFallbackNode,
Expand Down Expand Up @@ -169,13 +169,30 @@ export class FlowBuilderApi {
locale: string
): boolean {
const result = node.content.keywords.find(
i => i.locale === locale && this.containsAnyKeywords(input, i.values)
keywords =>
keywords.locale === locale &&
this.inputMatchesAnyKeyword(input, keywords.values)
)
return Boolean(result)
}

private containsAnyKeywords(input: string, keywords: string[]): boolean {
return keywords.some(keyword => input.includes(keyword))
private inputMatchesAnyKeyword(input: string, keywords: string[]): boolean {
return keywords.some(keyword => {
const regExpMatchArray = keyword.match(REG_EXP_PATTERN)
if (regExpMatchArray) {
return this.resolveKeywordAsRegExp(regExpMatchArray, input)
}
return input.includes(keyword)
})
}

private resolveKeywordAsRegExp(
regExpMatchArray: RegExpMatchArray,
input: string
) {
const [, pattern, flags] = regExpMatchArray
const keywordAsRegExp = new RegExp(pattern, flags)
return input.match(keywordAsRegExp)
}

getPayload(target?: HtNodeLink): string | undefined {
Expand Down
1 change: 1 addition & 0 deletions packages/botonic-plugin-flow-builder/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const SEPARATOR = '|'
export const SOURCE_INFO_SEPARATOR = `${SEPARATOR}source_`
export const VARIABLE_PATTERN = /{([^}]+)}/g
export const ACCESS_TOKEN_VARIABLE_KEY = '_access_token'
export const REG_EXP_PATTERN = /^\/(.*)\/([gimyus]*)$/
Loading