zenno: format

This commit is contained in:
2026-06-16 14:20:03 -04:00
parent 1d65a0ec14
commit 67377c4690
3 changed files with 71 additions and 59 deletions
+42 -42
View File
@@ -1,39 +1,39 @@
const WORD_BOUNDARY = " ,!?/-+();#○☆♡'=♪∀゚∴";
function score(s: string, tt: string): number {
let k: number | undefined;
let r = 0;
let run = 0;
for (const c of s) {
const j = tt.indexOf(c, k);
// If the character isn't in the string, there's a major penalty.
if (j < 0) {
// The penalty scales with run length, on the assumption that we're
// typing something else.
// Really this should scale with the longest current run among all
// search terms, but that's infeasible to implement.
r -= 6 + run*run;
run = 0;
continue;
}
run++;
// Characters at word boundaries get extra score.
if (j == 0 || WORD_BOUNDARY.includes(tt[j-1])) {
r += 2;
}
// As do characters that *are* word boundaries.
if (WORD_BOUNDARY.includes(c)) {
r += 2;
}
// And runs of matches scale with run length.
if (j === k) {
r += (run+1) * (run+1);
} else {
run = 0;
}
k = j + 1;
}
return r;
let k: number | undefined;
let r = 0;
let run = 0;
for (const c of s) {
const j = tt.indexOf(c, k);
// If the character isn't in the string, there's a major penalty.
if (j < 0) {
// The penalty scales with run length, on the assumption that we're
// typing something else.
// Really this should scale with the longest current run among all
// search terms, but that's infeasible to implement.
r -= 6 + run * run;
run = 0;
continue;
}
run++;
// Characters at word boundaries get extra score.
if (j == 0 || WORD_BOUNDARY.includes(tt[j - 1])) {
r += 2;
}
// As do characters that *are* word boundaries.
if (WORD_BOUNDARY.includes(c)) {
r += 2;
}
// And runs of matches scale with run length.
if (j === k) {
r += (run + 1) * (run + 1);
} else {
run = 0;
}
k = j + 1;
}
return r;
}
/**
@@ -44,13 +44,13 @@ function score(s: string, tt: string): number {
* @returns Matching terms in decreasing match quality order
*/
export function stringsearch<T>(sub: string, terms: Iterable<T>, map: (t: T) => string): T[] {
const s = sub.toLocaleLowerCase();
const scored: [T, number][] = [];
for (const t of terms) {
const sc = score(s, map(t).toLocaleLowerCase());
if (sc >= 0) {
scored.push([t, sc]);
}
}
return scored.sort(([, a], [, b]) => b - a).map(([t,]) => t);
const s = sub.toLocaleLowerCase();
const scored: [T, number][] = [];
for (const t of terms) {
const sc = score(s, map(t).toLocaleLowerCase());
if (sc >= 0) {
scored.push([t, sc]);
}
}
return scored.sort(([, a], [, b]) => b - a).map(([t]) => t);
}