1
0
silverbullet/web/components/fuse_search.ts
Zef Hemel a2dbf7b3db
PlugOS refactor and other tweaks (#631)
* Prep for in-process plug loading (e.g. for CF workers, Deno Deploy)
* Prototype of fixed in-process loading plugs
* Fix: buttons not to scroll with content
* Better positioning of modal especially on mobile
* Move query caching outside query
* Fix annoying mouse behavior when filter box appears
* Page navigator search tweaks
2024-01-15 16:43:12 +01:00

60 lines
1.4 KiB
TypeScript

// @deno-types="https://deno.land/x/fuse@v6.4.1/dist/fuse.d.ts"
import Fuse from "https://deno.land/x/fuse@v6.4.1/dist/fuse.esm.min.js";
import { FilterOption } from "../types.ts";
type FuseOption = FilterOption & {
baseName: string;
};
export const fuzzySearchAndSort = (
arr: FilterOption[],
searchPhrase: string,
): FilterOption[] => {
if (!searchPhrase) {
return arr.sort((a, b) => (a.orderId || 0) - (b.orderId || 0));
}
const enrichedArr: FuseOption[] = arr.map((item) => {
return {
...item,
baseName: item.name.split("/").pop()!,
tags: item.tags?.join(" "),
aliases: item.aliases?.join(" "),
};
});
const fuse = new Fuse(enrichedArr, {
keys: [{
name: "name",
weight: 0.3,
}, {
name: "baseName",
weight: 1,
}, {
name: "displayName",
weight: 0.7,
}, {
name: "aliases",
weight: 0.5,
}],
includeScore: true,
shouldSort: true,
isCaseSensitive: false,
ignoreLocation: true,
threshold: 0.6,
sortFn: (a, b): number => {
if (a.score === b.score) {
const aOrder = enrichedArr[a.idx].orderId || 0;
const bOrder = enrichedArr[b.idx].orderId || 0;
if (aOrder !== bOrder) {
return aOrder - bOrder;
}
}
return a.score - b.score;
},
});
const results = fuse.search(searchPhrase);
// console.log("results", results);
return results.map((r) => r.item);
};