textmachine/frontend/src/showcase/terms.ts

21 lines
1.1 KiB
TypeScript

import type { BankTerm } from '../api';
/**
* Whether a term matched the query. One predicate for the bank and for the jump palette: were they
* to diverge, the palette would find what the bank it leads to does not show.
*
* Case is folded on both sides: hanzi have none, but the source side of a pair is sometimes Latin,
* and a one-sided filter would behave differently in the two columns.
*/
export const matchesTerm = (query: string) => {
// The query is normalized ONCE, not per term: the predicate is called over a thousand rows per
// repaint, and a `trim().toLowerCase()` inside would cost a thousand needless strings.
const needle = query.trim().toLowerCase();
// ⚠ There is no search through the description here and there cannot be: the description is
// covered by the spoiler, and a field that is searched through works as an oracle — the covered
// text is computed through it while revealing nothing.
return (term: BankTerm) =>
needle === '' ||
term.src.toLowerCase().includes(needle) ||
term.dst.toLowerCase().includes(needle);
};