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/functions.ts | 7 +++++++ src/lib/i18n.ts | 9 +++++++++ src/lib/rx.ts | 9 ++++++--- src/lib/search.ts | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/lib/functions.ts create mode 100644 src/lib/i18n.ts create mode 100644 src/lib/search.ts (limited to 'src/lib') diff --git a/src/lib/functions.ts b/src/lib/functions.ts new file mode 100644 index 0000000..0ec9f00 --- /dev/null +++ b/src/lib/functions.ts @@ -0,0 +1,7 @@ +export function debounce(func: Function, timeout: number) { + let timer: any + return (...args: any) => { + clearTimeout(timer) + timer = setTimeout(() => { func.apply(this, args) }, timeout) + } +} diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts new file mode 100644 index 0000000..cd5b3de --- /dev/null +++ b/src/lib/i18n.ts @@ -0,0 +1,9 @@ +export function unit(n: number, singular: string, plural: string, f: (n: number, unit: string) => string = format): string { + return n > 1 + ? f(n, plural) + : f(n, singular) +} + +function format(n: number, unit: string): string { + return `${n} ${unit}` +} diff --git a/src/lib/rx.ts b/src/lib/rx.ts index dbdd3ad..1b41c27 100644 --- a/src/lib/rx.ts +++ b/src/lib/rx.ts @@ -328,8 +328,8 @@ function appendChild(state: State, element: Element, child: Child, lastAdded?: N lastAdded: appendRes.lastAdded } } else if (isRx(child)) { - const rxBase = document.createTextNode("") - element.append(rxBase) + const rxBase = document.createTextNode('') + appendNode(element, rxBase, lastAdded) let appendRes: AppendResult = { cancel: voidCancel, remove: voidRemove, @@ -345,7 +345,10 @@ function appendChild(state: State, element: Element, child: Child, lastAdded?: N appendRes.cancel() cancelRx() }, - remove: appendRes.remove, + remove: () => { + appendRes.remove() + element.removeChild(rxBase) + }, lastAdded: appendRes.lastAdded, } } else if (child === undefined || child === false) { 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