mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-06-04 13:08:33 +01:00
feat(settings): add accent-insensitive search filter with tests
This commit is contained in:
22
components/settings/index/searchFilter.test.ts
Normal file
22
components/settings/index/searchFilter.test.ts
Normal 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);
|
||||
});
|
||||
14
components/settings/index/searchFilter.ts
Normal file
14
components/settings/index/searchFilter.ts
Normal 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);
|
||||
};
|
||||
Reference in New Issue
Block a user