feat(settings): add accent-insensitive search filter with tests

This commit is contained in:
Gauvain
2026-06-03 23:41:40 +02:00
parent e783227ba6
commit e476e0b4d9
3 changed files with 39 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
import { expect, test } from "bun:test";
import { matchesQuery, normalize } from "./searchFilter";
test("normalize strips accents and lowercases", () => {
expect(normalize("Légèreté")).toBe("legerete");
expect(normalize(" AUDIO ")).toBe("audio");
});
test("matchesQuery matches title case/accent-insensitively", () => {
expect(matchesQuery({ title: "Apparence", keywords: [] }, "appar")).toBe(
true,
);
expect(
matchesQuery({ title: "Audio", keywords: ["sous-titres"] }, "SOUS"),
).toBe(true);
expect(matchesQuery({ title: "Music", keywords: [] }, "xyz")).toBe(false);
});
test("matchesQuery returns true for empty query", () => {
expect(matchesQuery({ title: "Anything" }, "")).toBe(true);
expect(matchesQuery({ title: "Anything" }, " ")).toBe(true);
});

View File

@@ -0,0 +1,14 @@
export const normalize = (s: string): string =>
s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().trim();
export interface Searchable {
title: string;
keywords?: string[];
}
export const matchesQuery = (item: Searchable, query: string): boolean => {
const q = normalize(query);
if (!q) return true;
const hay = normalize([item.title, ...(item.keywords ?? [])].join(" "));
return hay.includes(q);
};