Skip to Content
Client Lib

Search

Ranked in-app search with query chips, typo tolerance and usage boosts. No UI, no dependencies.

Usage

import { amounts, createSearch, dates, source } from "@dapplelight/signet-lib/search"
import { db } from "./db"

const search = createSearch({
  sources: [
    source({
      kind: "page",
      title: "Pages",
      items: [{ id: "invoices", label: "Invoices" }, { id: "team", label: "Team members" }],
      key: (p) => p.id,
      name: (p) => p.label,
      words: (p) => (p.id === "team" ? ["people", "staff"] : []),
    }),
    source({
      kind: "payment",
      title: "Payments",
      find: (q) => db.payments.search(q.text, q.filters), // large lists query your own store
      key: (p) => p.id,
      name: (p) => p.payee,
      usesFilters: true,
      keepOrder: true,
    }),
  ],
  readers: [dates(), amounts()],
  words: { bijli: "electricity" }, // what people type -> what the data says
})

const result = search.search("invoce over 5k last month")
// result.best, result.sections, result.fixed ("typo corrected to"), result.query.chips

search.pick("invoce", result.best ?? result.sections[0].hits[0]) // call when a result is opened
search.recent(5) // last searches that led somewhere

Pass store to keep picks on the device (memoryStore() by default).

API

ExportWhat it does
createSearch(config)Returns { search, pick, recent, parse }
source(s)Erases a source's item type so sources share one list
Config, Source, Query, Hit, Section, ResultTypes
memoryStore(start?), Store, PickPicks storage; implement load/save for MMKV, localStorage or a file
boost, addPick, recent, BOOST_CAPThe usage-boost helpers
norm(s), lev(a, b), sound(w), typo(q, word), quality(name, words, q)Text matching primitives

createSearch(config)

OptionTypeDefault
sourcesSource[]required
readersReader[]noneRun in order over the query
wordsRecord<string, string>{}Word list applied to the query
storeStorememoryStore()
now() => numberDate.now

Source

FieldType
kind, titlestringSection id and heading
key(item), name(item)stringIdentity and display/match name
itemslist or () => listSmall lists the engine matches
find(q)(q: Query) => listBig lists: query your own store
words(item)string[]Extra names people search by
weight(item)number0 to 6 points for how much the item is used
priornumberMultiplier on the match score, default 1
usesFiltersbooleanSection leads whenever the query has a chip
keepOrderbooleanKeep find's order instead of sorting

Readers

Readers pull structured parts out of the query before text matching.

ExportWhat it reads
dates(labels?)today, yesterday, this/last week, last N days, month names, year (English and Hindi words)
amounts(format?, labels?)500, 3k, 1.2L, over 5k, 500-1000, kam/zyada
people(names)Names or @name from your own list
commands(verbs)A leading verb: add 250 chai gives { verb: "add", amount: 250 }
amount(n, unit?)Converts 3 and k to 3000
Reader, Chip, ReaderContext, DateRange, AmountRange, Command, DateLabels, AmountLabelsTypes. Write your own Reader as { name, read(s, ctx) }

result.best is set only when one result clearly wins and the query has no chips.