UNPKG

search-plus-ts

Version:
25 lines (23 loc) 737 B
import Fuse from "fuse.js"; //#region src/lib/search.ts var SearchPlusTs = class { fuse; fuseOptions; constructor(getOptions) { this.fuseOptions = { keys: getOptions.keys, threshold: getOptions.threshold || .5, useExtendedSearch: true }; this.fuse = new Fuse(getOptions.data, this.fuseOptions); } search(search) { if (!search || typeof search !== "string") return []; const terms = search.toLowerCase().trim().split(/\s+/); const andOrConditions = terms.map((t) => ({ $or: this.fuseOptions.keys.map((key) => ({ [key]: `'${t}` })) })); return this.fuse.search({ $and: andOrConditions }).map((result) => result.item); } }; var search_default = SearchPlusTs; //#endregion export { search_default as default };