// What the Research ask bar accepts, and what it hands the engine // (plans/umtool-redesign.md, "Research, first-class" 1). // // how many employees a phrase: the engine's plain search is already a // case-insensitive substring, so the words match // together, in order // "how many employees" the same; the quotes are dropped // /fir(ed|ing) them/ a regex (case-insensitive), as enumerate_matches' // `regex: true`; aliases do not apply to a regex // fired near:4 employees the two terms within 4 words of each other, either // order, INSIDE ONE CAPTION LINE -- compiled to a // regex, because the engine matches cue by cue // // Pure, so a unit test, the server and the page agree on what a query means. const RE_SPECIALS = /[.*+?^${}()|[\]\\]/g; export const escapeRegex = (s) => s.replace(RE_SPECIALS, "\\$&"); /** * @param {string} raw * @returns {{ query: string, regex: boolean, kind: "phrase"|"regex"|"near", near?: { a: string, b: string, n: number }, error?: string }} */ export function parseAsk(raw) { const text = String(raw ?? "").trim(); if (!text) return { query: "", regex: false, kind: "phrase", error: "Type what to find" }; const re = /^\/(.+)\/[a-z]*$/s.exec(text); if (re) { try { new RegExp(re[1], "i"); } catch (e) { return { query: re[1], regex: true, kind: "regex", error: `Not a regex: ${e.message}` }; } return { query: re[1], regex: true, kind: "regex" }; } const near = /^(.+?)\s+near:(\d{1,2})\s+(.+)$/i.exec(text); if (near) { const a = unquote(near[1]); const b = unquote(near[3]); const n = Number(near[2]); if (!a || !b || n < 1) return { query: text, regex: false, kind: "near", error: "near:N takes a term on each side and N ≥ 1" }; return { query: nearRegex(a, b, n), regex: true, kind: "near", near: { a, b, n } }; } return { query: unquote(text), regex: false, kind: "phrase" }; } function unquote(s) { const t = s.trim(); const m = /^"(.*)"$/s.exec(t); return (m ? m[1] : t).trim(); } /** `a` and `b` within `n` words of each other, either order. */ export function nearRegex(a, b, n) { const gap = `\\W+(?:\\w+\\W+){0,${Math.max(0, n - 1)}}`; const A = escapeRegex(a); const B = escapeRegex(b); return `${A}${gap}${B}|${B}${gap}${A}`; } /** * Where a query matched in a snippet, as [start, end) ranges, merged. The * engine says THAT a cue matched (a boolean); this finds WHERE, the same way: * a plain query as a case-insensitive substring, a regex as one, plus every * alias the engine fired (`{ pattern, regex }`). * * @param {string} text * @param {{ query: string, regex: boolean, aliases?: { pattern: string, regex: boolean }[] }} q * @returns {[number, number][]} */ export function markRanges(text, q) { const out = []; const patterns = [{ pattern: q.query, regex: q.regex }, ...(q.aliases ?? [])]; for (const p of patterns) { if (!p.pattern) continue; let re; try { re = new RegExp(p.regex ? p.pattern : escapeRegex(p.pattern), "gi"); } catch { continue; } for (const m of text.matchAll(re)) { if (m[0].length === 0) continue; out.push([m.index, m.index + m[0].length]); } } out.sort((x, y) => x[0] - y[0] || x[1] - y[1]); const merged = []; for (const r of out) { const last = merged[merged.length - 1]; if (last && r[0] <= last[1]) last[1] = Math.max(last[1], r[1]); else merged.push([r[0], r[1]]); } return merged; }