Upgraded Regex Search Node
This commit is contained in:
parent
8d043f6a77
commit
516618c0d2
@ -1,124 +1,140 @@
|
|||||||
////////// PROJECT FILE SEPARATION LINE ////////// CODE AFTER THIS LINE ARE FROM: <ProjectRoot>/Data/WebUI/src/nodes/Data Analysis/Node_Regex_Search.jsx
|
////////// PROJECT FILE SEPARATION LINE ////////// CODE AFTER THIS LINE ARE FROM: <ProjectRoot>/Data/WebUI/src/nodes/Data Analysis/Node_Regex_Search.jsx
|
||||||
import React, { useEffect, useState, useRef } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
import { Handle, Position, useReactFlow, useStore } from "reactflow";
|
import { Handle, Position, useReactFlow, useStore } from "reactflow";
|
||||||
|
|
||||||
|
// Modern Regex Search Node: Config via Sidebar
|
||||||
|
|
||||||
if (!window.BorealisValueBus) window.BorealisValueBus = {};
|
if (!window.BorealisValueBus) window.BorealisValueBus = {};
|
||||||
if (!window.BorealisUpdateRate) window.BorealisUpdateRate = 100;
|
if (!window.BorealisUpdateRate) window.BorealisUpdateRate = 100;
|
||||||
|
|
||||||
const RegexSearchNode = ({ id, data }) => {
|
const RegexSearchNode = ({ id, data }) => {
|
||||||
const edges = useStore((state) => state.edges);
|
const { setNodes } = useReactFlow();
|
||||||
const { setNodes } = useReactFlow();
|
const edges = useStore((state) => state.edges);
|
||||||
|
|
||||||
const [pattern, setPattern] = useState(data?.pattern || "");
|
// Pattern/flags always come from sidebar config (with defaults)
|
||||||
const [flags, setFlags] = useState(data?.flags || "i");
|
const pattern = data?.pattern ?? "";
|
||||||
|
const flags = data?.flags ?? "i";
|
||||||
|
|
||||||
const valueRef = useRef("0");
|
const valueRef = useRef("0");
|
||||||
|
const [matched, setMatched] = useState("0");
|
||||||
|
|
||||||
const updateNodeData = (key, val) => {
|
useEffect(() => {
|
||||||
setNodes(nds =>
|
let intervalId = null;
|
||||||
nds.map(n =>
|
let currentRate = window.BorealisUpdateRate;
|
||||||
n.id === id ? { ...n, data: { ...n.data, [key]: val } } : n
|
|
||||||
)
|
const runNodeLogic = () => {
|
||||||
|
const inputEdge = edges.find((e) => e.target === id);
|
||||||
|
const inputVal = inputEdge ? window.BorealisValueBus[inputEdge.source] || "" : "";
|
||||||
|
|
||||||
|
let matchResult = false;
|
||||||
|
try {
|
||||||
|
if (pattern) {
|
||||||
|
const regex = new RegExp(pattern, flags);
|
||||||
|
matchResult = regex.test(inputVal);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
matchResult = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = matchResult ? "1" : "0";
|
||||||
|
|
||||||
|
if (result !== valueRef.current) {
|
||||||
|
valueRef.current = result;
|
||||||
|
setMatched(result);
|
||||||
|
window.BorealisValueBus[id] = result;
|
||||||
|
setNodes((nds) =>
|
||||||
|
nds.map((n) =>
|
||||||
|
n.id === id ? { ...n, data: { ...n.data, match: result } } : n
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
intervalId = setInterval(runNodeLogic, currentRate);
|
||||||
let intervalId = null;
|
|
||||||
let currentRate = window.BorealisUpdateRate;
|
|
||||||
|
|
||||||
const runNodeLogic = () => {
|
const monitor = setInterval(() => {
|
||||||
const inputEdge = edges.find((e) => e.target === id);
|
const newRate = window.BorealisUpdateRate;
|
||||||
const inputVal = inputEdge ? window.BorealisValueBus[inputEdge.source] || "" : "";
|
if (newRate !== currentRate) {
|
||||||
|
clearInterval(intervalId);
|
||||||
|
intervalId = setInterval(runNodeLogic, newRate);
|
||||||
|
currentRate = newRate;
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
|
||||||
let matched = false;
|
return () => {
|
||||||
try {
|
clearInterval(intervalId);
|
||||||
if (pattern) {
|
clearInterval(monitor);
|
||||||
const regex = new RegExp(pattern, flags);
|
};
|
||||||
matched = regex.test(inputVal);
|
}, [id, edges, pattern, flags, setNodes]);
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
matched = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = matched ? "1" : "0";
|
return (
|
||||||
|
<div className="borealis-node">
|
||||||
if (result !== valueRef.current) {
|
<Handle type="target" position={Position.Left} className="borealis-handle" />
|
||||||
valueRef.current = result;
|
<div className="borealis-node-header">
|
||||||
window.BorealisValueBus[id] = result;
|
{data?.label || "Regex Search"}
|
||||||
}
|
</div>
|
||||||
};
|
<div className="borealis-node-content" style={{ fontSize: "9px", color: "#ccc" }}>
|
||||||
|
Match: {matched}
|
||||||
intervalId = setInterval(runNodeLogic, currentRate);
|
</div>
|
||||||
|
<Handle type="source" position={Position.Right} className="borealis-handle" />
|
||||||
const monitor = setInterval(() => {
|
</div>
|
||||||
const newRate = window.BorealisUpdateRate;
|
);
|
||||||
if (newRate !== currentRate) {
|
|
||||||
clearInterval(intervalId);
|
|
||||||
intervalId = setInterval(runNodeLogic, newRate);
|
|
||||||
currentRate = newRate;
|
|
||||||
}
|
|
||||||
}, 300);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearInterval(intervalId);
|
|
||||||
clearInterval(monitor);
|
|
||||||
};
|
|
||||||
}, [id, edges, pattern, flags]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="borealis-node">
|
|
||||||
<Handle type="target" position={Position.Left} className="borealis-handle" />
|
|
||||||
<div className="borealis-node-header">Regex Search</div>
|
|
||||||
<div className="borealis-node-content">
|
|
||||||
<label>Regex Pattern:</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={pattern}
|
|
||||||
onChange={(e) => {
|
|
||||||
setPattern(e.target.value);
|
|
||||||
updateNodeData("pattern", e.target.value);
|
|
||||||
}}
|
|
||||||
placeholder="e.g. World"
|
|
||||||
style={inputStyle}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<label>Regex Flags:</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={flags}
|
|
||||||
onChange={(e) => {
|
|
||||||
setFlags(e.target.value);
|
|
||||||
updateNodeData("flags", e.target.value);
|
|
||||||
}}
|
|
||||||
placeholder="e.g. i"
|
|
||||||
style={inputStyle}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<Handle type="source" position={Position.Right} className="borealis-handle" />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const inputStyle = {
|
|
||||||
width: "100%",
|
|
||||||
fontSize: "9px",
|
|
||||||
background: "#1e1e1e",
|
|
||||||
color: "#ccc",
|
|
||||||
border: "1px solid #444",
|
|
||||||
borderRadius: "2px",
|
|
||||||
padding: "3px",
|
|
||||||
marginBottom: "6px"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
type: "RegexSearch",
|
type: "RegexSearch",
|
||||||
label: "Regex Search",
|
label: "Regex Search",
|
||||||
description: `
|
description: `
|
||||||
Minimal RegEx Matcher:
|
Test for text matches with a regular expression pattern.
|
||||||
- Accepts pattern and flags
|
|
||||||
- Outputs "1" if match is found, else "0"
|
- Accepts a regex pattern and flags (e.g. "i", "g", "m")
|
||||||
- No visual output display
|
- Connect any node to the input to test its value.
|
||||||
`.trim(),
|
- Outputs "1" if the regex matches, otherwise "0".
|
||||||
content: "Outputs '1' if regex matches input, otherwise '0'",
|
- Useful for input validation, filtering, or text triggers.
|
||||||
component: RegexSearchNode
|
`.trim(),
|
||||||
|
content: "Outputs '1' if regex matches input, otherwise '0'",
|
||||||
|
component: RegexSearchNode,
|
||||||
|
config: [
|
||||||
|
{
|
||||||
|
key: "pattern",
|
||||||
|
label: "Regex Pattern",
|
||||||
|
type: "text",
|
||||||
|
defaultValue: "",
|
||||||
|
placeholder: "e.g. World"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "flags",
|
||||||
|
label: "Regex Flags",
|
||||||
|
type: "text",
|
||||||
|
defaultValue: "i",
|
||||||
|
placeholder: "e.g. i"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
usage_documentation: `
|
||||||
|
### Regex Search Node
|
||||||
|
|
||||||
|
This node tests its input value against a user-supplied regular expression pattern.
|
||||||
|
|
||||||
|
**Configuration (Sidebar):**
|
||||||
|
- **Regex Pattern**: Standard JavaScript regex pattern.
|
||||||
|
- **Regex Flags**: Any combination of \`i\` (ignore case), \`g\` (global), \`m\` (multiline), etc.
|
||||||
|
|
||||||
|
**Input:**
|
||||||
|
- Accepts a string from any upstream node.
|
||||||
|
|
||||||
|
**Output:**
|
||||||
|
- Emits "1" if the pattern matches the input string.
|
||||||
|
- Emits "0" if there is no match or the pattern/flags are invalid.
|
||||||
|
|
||||||
|
**Common Uses:**
|
||||||
|
- Search for words/phrases in extracted text.
|
||||||
|
- Filter values using custom patterns.
|
||||||
|
- Create triggers based on input structure (e.g. validate an email, detect phone numbers, etc).
|
||||||
|
|
||||||
|
#### Example:
|
||||||
|
- **Pattern:** \`World\`
|
||||||
|
- **Flags:** \`i\`
|
||||||
|
- **Input:** \`Hello world!\`
|
||||||
|
- **Output:** \`1\` (matched, case-insensitive)
|
||||||
|
`.trim()
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user