Skip to content

Commit

Permalink
✨ 新增正则匹配节点
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefean committed Jan 20, 2025
1 parent 081b313 commit 9063bc0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/stage/autoComputeEngine/functions/stringLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,29 @@ export namespace StringFunctions {
const suffix = strings[1];
return [str.endsWith(suffix) ? "1" : "0"];
}

/**
* 检查正则匹配
* 参数数量必须>=2, 最后一个参数为正则表达式
* 举例:
* 输入 ["hello world", "^[a-zA-Z]+$"]
* 输出 ["1"]
* 当输入多个待检查字符串参数时,分别检查每个参数是否匹配正则表达式
* 举例
* 输入 ["hello world", "world", "^[a-zA-Z]+$"]
* 输出 ["1", "0"]
* @param strings
*/
export function checkRegexMatch(strings: string[]): string[] {
if (strings.length < 2) return ["0"];
try {
const regex = new RegExp(strings[strings.length - 1]);
const results = strings
.slice(0, -1)
.map((str) => (regex.test(str) ? "1" : "0"));
return results;
} catch (e: any) {
return [e.toString()];
}
}
}
2 changes: 2 additions & 0 deletions src/core/stage/autoComputeEngine/logicNodeNameEnum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export enum LogicNodeNameEnum {
SPLIT = "#SPLIT#",
REPLACE = "#REPLACE#",
CONNECT = "#CONNECT#",
CHECK_REGEX_MATCH = "#CHECK_REGEX_MATCH#",
// 统计
COUNT = "#COUNT#",
// 其他
Expand Down Expand Up @@ -102,6 +103,7 @@ export const LogicNodeNameToRenderNameMap: {
[LogicNodeNameEnum.SPLIT]: "分割",
[LogicNodeNameEnum.REPLACE]: "替换",
[LogicNodeNameEnum.CONNECT]: "连接",
[LogicNodeNameEnum.CHECK_REGEX_MATCH]: "正则匹配",
[LogicNodeNameEnum.COUNT]: "count",
[LogicNodeNameEnum.RGB]: "rgb",
[LogicNodeNameEnum.RGBA]: "rgba",
Expand Down
4 changes: 4 additions & 0 deletions src/core/stage/autoComputeEngine/mainTick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ const MapNameFunction: StringFunctionMap = {
[LogicNodeNameEnum.SPLIT]: StringFunctions.split,
[LogicNodeNameEnum.REPLACE]: StringFunctions.replace,
[LogicNodeNameEnum.CONNECT]: StringFunctions.connect,
[LogicNodeNameEnum.CHECK_REGEX_MATCH]: StringFunctions.checkRegexMatch,
// 集合计算
[LogicNodeNameEnum.COUNT]: funcTypeTrans(MathFunctions.count),
};

/**
* 其他特殊功能的函数
*/
const MapOtherFunction: OtherFunctionMap = {
[LogicNodeNameEnum.RGB]: NodeLogic.rgb,
[LogicNodeNameEnum.RGBA]: NodeLogic.rgba,
Expand Down

0 comments on commit 9063bc0

Please sign in to comment.