From 25ecb1cb75b7b5b584ad223e12d74c3e3ee7da89 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 12 Feb 2023 13:05:25 +0100 Subject: Add textual search on title, subtitle and authors --- src/lib/search.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/lib/search.ts (limited to 'src/lib/search.ts') diff --git a/src/lib/search.ts b/src/lib/search.ts new file mode 100644 index 0000000..026cb94 --- /dev/null +++ b/src/lib/search.ts @@ -0,0 +1,17 @@ +export function match(search: string, ...targets: Array): boolean { + const formattedTargets = targets + .filter(t => t !== undefined) + .map(format) + + return search.split(/\s+/).every(subSearch => + formattedTargets.some(target => target.includes(format(subSearch))) + ) +} + +export function format(str: string): string { + return unaccent(str.toLowerCase()) +} + +function unaccent(str: string): string { + return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "") +} -- cgit v1.2.3