mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-07-29 13:52:07 +00:00
Share one fuzzy matcher across both dialogs
Plugins dialog and the Speed Dial popup each carried a private copy of the same fuzzy matcher and had already drifted. Move the pure functions into a shared module loaded by both pages, and add a unit test beside it.
This commit is contained in:
58
resources/web/js/fuzzy-search.js
Normal file
58
resources/web/js/fuzzy-search.js
Normal file
@@ -0,0 +1,58 @@
|
||||
// Shared fuzzy-search core for the webview dialogs (Plugins dialog, Speed Dial popup).
|
||||
// why: both pages carried their own copy of this matcher and had already drifted; one source of truth.
|
||||
// note: keep this DOM-free and plain global-scope (no export/module) - it is loaded by <script src> in
|
||||
// each page AND by a node vm.runInContext in the speed-dial logic test. It MUST be loaded before
|
||||
// the page script that calls it.
|
||||
|
||||
// Fold per-character so matched offsets stay in ORIGINAL string coordinates (highlighting slices the
|
||||
// original text; a separately-folded string would desync offsets).
|
||||
function FoldChar(ch) {
|
||||
return ch.normalize("NFD").replace(/\p{Diacritic}/gu, ""); // accents always folded
|
||||
}
|
||||
|
||||
function Norm(ch, caseSensitive) {
|
||||
const folded = FoldChar(ch);
|
||||
return caseSensitive ? folded : folded.toLowerCase(); // case-sensitivity is the only toggle
|
||||
}
|
||||
|
||||
function EscapeRegExp(value) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
// Fuzzy: ordered subsequence. Builds ranges in original coordinates, merging adjacent runs on the fly.
|
||||
function FuzzyRanges(text, query, caseSensitive) {
|
||||
const t = text || "";
|
||||
const needle = Array.from(query || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
if (!needle)
|
||||
return null;
|
||||
const ranges = [];
|
||||
let qi = 0;
|
||||
for (let i = 0; i < t.length && qi < needle.length; i++) {
|
||||
if (Norm(t[i], caseSensitive) === needle[qi]) {
|
||||
const last = ranges[ranges.length - 1];
|
||||
if (last && last[1] === i)
|
||||
last[1] = i + 1;
|
||||
else
|
||||
ranges.push([i, i + 1]);
|
||||
qi++;
|
||||
}
|
||||
}
|
||||
return qi === needle.length ? ranges : null;
|
||||
}
|
||||
|
||||
// Whole word: literal \b-bounded match that bypasses fuzzy. The per-char fold keeps the haystack
|
||||
// length-aligned to the original text, so regex indices map straight back to original offsets.
|
||||
// note: one-to-many folds (ligatures, eszett) shift offsets by a char; rare in names, cosmetic only.
|
||||
function WholeWordRanges(text, query, caseSensitive) {
|
||||
const haystack = Array.from(text || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
const needle = Array.from(query || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
if (!needle)
|
||||
return null;
|
||||
const re = new RegExp(`\\b${EscapeRegExp(needle)}\\b`, "g");
|
||||
const ranges = [];
|
||||
let match;
|
||||
// why: needle is non-empty, so \b-bounded matches are never zero-length - no empty-match guard needed.
|
||||
while ((match = re.exec(haystack)) !== null)
|
||||
ranges.push([match.index, match.index + match[0].length]);
|
||||
return ranges.length > 0 ? ranges : null;
|
||||
}
|
||||
37
resources/web/js/fuzzy-search.test.js
Normal file
37
resources/web/js/fuzzy-search.test.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// Unit test for the shared fuzzy matcher. Run: node resources/web/js/fuzzy-search.test.js
|
||||
// why: fuzzy-search.js is plain global-scope (no exports, so a browser <script src> works) - load it into
|
||||
// a vm context the same way the page and the speed-dial test do, then assert against the globals.
|
||||
const vm = require("vm"), assert = require("assert"), fs = require("fs");
|
||||
const ctx = {};
|
||||
vm.createContext(ctx);
|
||||
vm.runInContext(fs.readFileSync(__dirname + "/fuzzy-search.js", "utf8"), ctx);
|
||||
const { FoldChar, Norm, EscapeRegExp, FuzzyRanges, WholeWordRanges } = ctx;
|
||||
|
||||
// FoldChar / Norm: accents fold, case only folds when case-insensitive.
|
||||
assert.equal(FoldChar("é"), "e");
|
||||
assert.equal(Norm("É", false), "e");
|
||||
assert.equal(Norm("É", true), "E");
|
||||
|
||||
// FuzzyRanges: ordered subsequence, ranges in ORIGINAL coordinates, adjacent runs merged.
|
||||
assert.deepEqual(FuzzyRanges("Auto Arrange", "aa", false), [[0, 1], [5, 6]]);
|
||||
assert.deepEqual(FuzzyRanges("Measure", "eas", false), [[1, 4]]); // contiguous run merges to one range
|
||||
assert.equal(FuzzyRanges("Measure", "xyz", false), null); // no subsequence -> null
|
||||
assert.equal(FuzzyRanges("Measure", "", false), null); // empty query -> null
|
||||
|
||||
// Accent-insensitive matching, offsets stay in the original (accented) string.
|
||||
assert.deepEqual(FuzzyRanges("Café", "cafe", false), [[0, 4]]);
|
||||
|
||||
// Case sensitivity is the only toggle.
|
||||
assert.equal(FuzzyRanges("Measure", "MEAS", true), null); // case-sensitive: no match
|
||||
assert.deepEqual(FuzzyRanges("Measure", "Meas", true), [[0, 4]]); // case-sensitive: matches exact case
|
||||
|
||||
// WholeWordRanges: \b-bounded literal, bypasses fuzzy. Substring inside a word does NOT match.
|
||||
assert.deepEqual(WholeWordRanges("Auto Arrange", "arrange", false), [[5, 12]]);
|
||||
assert.equal(WholeWordRanges("Rearrange", "arrange", false), null); // not on a word boundary
|
||||
assert.deepEqual(WholeWordRanges("a.b", "a", false), [[0, 1]]); // '.' is a boundary
|
||||
|
||||
// EscapeRegExp: regex metachars in the query are treated literally by whole-word.
|
||||
assert.equal(EscapeRegExp("a.b*"), "a\\.b\\*");
|
||||
assert.deepEqual(WholeWordRanges("c++ tool", "c", false), [[0, 1]]); // '+' would be a regex error unescaped
|
||||
|
||||
console.log("ok");
|
||||
Reference in New Issue
Block a user