Add discover page
This commit is contained in:
+32
-1
@@ -1,6 +1,9 @@
|
||||
import Fuse from "fuse.js"
|
||||
import type {IFuseOptions, FuseResult} from 'fuse.js'
|
||||
import {throttle} from 'throttle-debounce'
|
||||
import {browser} from '$app/environment'
|
||||
import {writable} from 'svelte/store'
|
||||
import {sortBy} from '@welshman/lib'
|
||||
import {browser} from '$app/environment'
|
||||
|
||||
export const parseJson = (json: string) => {
|
||||
if (!json) return null
|
||||
@@ -29,3 +32,31 @@ export const synced = <T>(key: string, defaultValue: T, delay = 300) => {
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
export type SearchOptions<V, T> = {
|
||||
getValue: (item: T) => V
|
||||
fuseOptions?: IFuseOptions<T>
|
||||
sortFn?: (items: FuseResult<T>) => any
|
||||
}
|
||||
|
||||
export const createSearch = <V, T>(data: T[], opts: SearchOptions<V, T>) => {
|
||||
const fuse = new Fuse(data, {...opts.fuseOptions, includeScore: true})
|
||||
const map = new Map<V, T>(data.map(item => [opts.getValue(item), item]))
|
||||
|
||||
const search = (term: string) => {
|
||||
let results = term ? fuse.search(term) : data.map(item => ({item, score: 1}) as FuseResult<T>)
|
||||
|
||||
if (opts.sortFn) {
|
||||
results = sortBy(opts.sortFn, results)
|
||||
}
|
||||
|
||||
return results.map(result => result.item)
|
||||
}
|
||||
|
||||
return {
|
||||
getValue: opts.getValue,
|
||||
getOption: (value: V) => map.get(value),
|
||||
searchOptions: (term: string) => search(term),
|
||||
searchValues: (term: string) => search(term).map(opts.getValue),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user