refactor(books): shared search normalisation and debounce helpers + migrate command palette to server-side queries (#2232)

This commit is contained in:
Alex
2026-09-21 13:01:56 +01:00
committed by GitHub
parent 7ed1c35ca0
commit 04467d7d68
9 changed files with 339 additions and 83 deletions
@@ -0,0 +1,56 @@
import {effect, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {debouncedSignal} from './debounced-signal';
describe('debouncedSignal', () => {
beforeEach(() => {
vi.useFakeTimers();
TestBed.configureTestingModule({});
});
afterEach(() => {
TestBed.resetTestingModule();
vi.useRealTimers();
});
it('exposes the initial source value immediately', () => {
const source = signal('initial');
const debounced = TestBed.runInInjectionContext(() => debouncedSignal(source, 200));
expect(debounced()).toBe('initial');
});
it('updates only after the debounce window', () => {
const source = signal('initial');
const debounced = TestBed.runInInjectionContext(() => debouncedSignal(source, 200));
source.set('changed');
TestBed.flushEffects();
vi.advanceTimersByTime(200 - 1);
expect(debounced()).toBe('initial');
vi.advanceTimersByTime(1);
expect(debounced()).toBe('changed');
});
it('does not emit when a change reverts to the settled value', () => {
const source = signal('settled');
const debounced = TestBed.runInInjectionContext(() => debouncedSignal(source, 200));
const values: string[] = [];
TestBed.runInInjectionContext(() => {
effect(() => values.push(debounced()));
});
TestBed.flushEffects();
source.set('temporary');
TestBed.flushEffects();
vi.advanceTimersByTime(200 / 2);
source.set('settled');
TestBed.flushEffects();
vi.advanceTimersByTime(200);
expect(values).toEqual(['settled']);
});
});
@@ -0,0 +1,11 @@
import {Injector, Signal} from '@angular/core';
import {toObservable, toSignal} from '@angular/core/rxjs-interop';
import {debounceTime, distinctUntilChanged} from 'rxjs';
export function debouncedSignal<T>(source: Signal<T>, ms: number, injector?: Injector): Signal<T> {
return toSignal(
toObservable(source, {injector}).pipe(debounceTime(ms), distinctUntilChanged()),
{initialValue: source(), injector},
);
}
@@ -0,0 +1,32 @@
import {describe, expect, it} from 'vitest';
import {normalizeLocalSearchTerm, normalizeRemoteSearchTerm} from './search-terms';
describe('normalizeRemoteSearchTerm', () => {
it('strips punctuation and collapses whitespace without changing letters', () => {
expect(normalizeRemoteSearchTerm(' Dune!\t/ Messiah? ')).toBe('Dune Messiah');
expect(normalizeRemoteSearchTerm(' Łódź Ærø STRAẞE ')).toBe('Łódź Ærø STRAẞE');
});
it('returns an empty string for empty or whitespace-only input', () => {
expect(normalizeRemoteSearchTerm('')).toBe('');
expect(normalizeRemoteSearchTerm(' \t ')).toBe('');
});
});
describe('normalizeLocalSearchTerm', () => {
it('normalizes representative text for local matching', () => {
expect(normalizeLocalSearchTerm(' Ætna Łódź / Straße! ')).toBe('aetna lodz strasse');
});
it('strips punctuation and collapses whitespace', () => {
expect(normalizeLocalSearchTerm(' Dune!\t/ Messiah? ')).toBe('dune messiah');
});
it('returns an empty string for empty or whitespace-only input', () => {
expect(normalizeLocalSearchTerm('')).toBe('');
expect(normalizeLocalSearchTerm(' \t ')).toBe('');
});
});
@@ -0,0 +1,18 @@
export const SEARCH_DEBOUNCE_MS = 200;
export function normalizeRemoteSearchTerm(str: string): string {
return str
.replace(/[!@$%^&*_=|~`<>?/";']/g, '')
.replace(/\s+/g, ' ')
.trim();
}
export function normalizeLocalSearchTerm(str: string): string {
const folded = str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
.replace(/ø/gi, 'o')
.replace(/ł/gi, 'l')
.replace(/æ/gi, 'ae')
.replace(/œ/gi, 'oe')
.replace(/ß/g, 'ss');
return normalizeRemoteSearchTerm(folded).toLowerCase();
}