From 382784cb8463e0711a85e75b406b78b59664751c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Wed, 18 Dec 2024 14:51:29 +0200 Subject: [PATCH] Flatten declarations --- src/utils/history.ts | 2 +- src/utils/state.ts | 76 ++++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/utils/history.ts b/src/utils/history.ts index b0fcd19..2014ed8 100644 --- a/src/utils/history.ts +++ b/src/utils/history.ts @@ -4,7 +4,7 @@ import { DefaultState } from "./state" type Items = NonNullable export function createHistory(size: number) { - const localStorageKey = "nostoAutocomplete:history" + const localStorageKey = "nosto:autocomplete:history" let items = get() function get(): Items { diff --git a/src/utils/state.ts b/src/utils/state.ts index 07f3058..c589f4d 100644 --- a/src/utils/state.ts +++ b/src/utils/state.ts @@ -31,7 +31,7 @@ export type StateActions = { clearHistory(): PromiseLike } -export const getStateActions = ({ +export function getStateActions({ config, history, input, @@ -39,7 +39,7 @@ export const getStateActions = ({ config: Required> history?: History input: HTMLInputElement -}): StateActions => { +}): StateActions { let cancellable: Cancellable | undefined const fetchState = ( @@ -75,39 +75,47 @@ export const getStateActions = ({ }) } - return { - updateState: (inputValue?: string, options?: SearchAutocompleteOptions): PromiseLike => { - cancellable?.cancel() + function updateState(inputValue?: string, options?: SearchAutocompleteOptions): PromiseLike { + cancellable?.cancel() - if (inputValue && inputValue.length >= config.minQueryLength) { - cancellable = makeCancellable(fetchState(inputValue, config, options)) - return cancellable.promise - } else if (history) { - return getHistoryState(inputValue ?? "") - } + if (inputValue && inputValue.length >= config.minQueryLength) { + cancellable = makeCancellable(fetchState(inputValue, config, options)) + return cancellable.promise + } else if (history) { + return getHistoryState(inputValue ?? "") + } - return ( - // @ts-expect-error type mismatch - cancellable?.promise ?? Promise.resolve({}) - ) - }, - addHistoryItem: (item: string) => { - if (history) { - history.add(item) - } - return getHistoryState(input.value) - }, - removeHistoryItem: (item: string) => { - if (history) { - history.remove(item) - } - return getHistoryState(input.value) - }, - clearHistory: () => { - if (history) { - history.clear() - } - return getHistoryState(input.value) - }, + return ( + // @ts-expect-error type mismatch + cancellable?.promise ?? Promise.resolve({}) + ) + } + + function addHistoryItem(item: string): PromiseLike { + if (history) { + history.add(item) + } + return getHistoryState(input.value) + } + + function removeHistoryItem(item: string): PromiseLike { + if (history) { + history.remove(item) + } + return getHistoryState(input.value) + } + + function clearHistory(): PromiseLike { + if (history) { + history.clear() + } + return getHistoryState(input.value) + } + + return { + updateState, + addHistoryItem, + removeHistoryItem, + clearHistory, } }