diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/functions.ts | 7 | ||||
-rw-r--r-- | src/lib/i18n.ts | 9 | ||||
-rw-r--r-- | src/lib/rx.ts | 9 | ||||
-rw-r--r-- | src/lib/search.ts | 17 |
4 files changed, 39 insertions, 3 deletions
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<string | undefined>): 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, "") +} |