Update qkf/stepRegistry.js

This commit is contained in:
2026-02-08 21:00:42 -06:00
parent 8f22245637
commit f6eb926660

View File

@@ -9,12 +9,32 @@ function normalizeText(t) {
}
function patternToRegex(pattern) {
const parts = String(pattern).split("{string}");
const escaped = parts.map((p) => escapeRegex(normalizeText(p)));
const joined = escaped.join("\\s+(.+?)\\s+");
const final = "^" + joined.replace(/\s+/g, "\\s+") + "$";
return new RegExp(final, "i");
// Supports:
// - {string} -> "value" OR 'value' OR bareWord(s)
// Also normalizes whitespace
const norm = normalizeText(pattern);
// Split around {string}
const parts = norm.split("{string}").map((p) => escapeRegex(p));
// Capture:
// - "..." or '...' or unquoted token(s) (until end)
// We make it non-greedy and allow extra spaces.
const CAPTURE = `(?:"([^"]+)"|'([^']+)'|([^]+?))`;
let regexStr = "^";
for (let i = 0; i < parts.length; i++) {
regexStr += parts[i];
if (i < parts.length - 1) {
// allow flexible whitespace around the capture
regexStr += "\\s+" + CAPTURE + "\\s+";
}
}
regexStr += "$";
return new RegExp(regexStr.replace(/\s+/g, "\\s+"), "i");
}
function createStepRegistry(initialCtx = {}) {
const defs = [];
@@ -41,7 +61,11 @@ function createStepRegistry(initialCtx = {}) {
const m = d.regex.exec(text);
if (!m) continue;
const captures = m.slice(1).map((x) => normalizeText(x));
const captures = [];
for (let i = 1; i < m.length; i += 3) {
const v = m[i] || m[i + 1] || m[i + 2] || "";
captures.push(normalizeText(v));
}
const ctx = { ...initialCtx, ...extraCtx };
// ✅ Begin step tracking so QKF calls are awaited even if step defs don't `await`