Search
One search for everything: the editor's palette, assets, content, outliner, Data and pickers, the tech and item trees, the shop window, and your own panels, shops and tools. It is the kit's (@v12-engine/client/kit): exact matches first, then prefixes, then word starts, then substrings, then a typo's nearest (flsh finds Flash, greves Greaves), by weighted keys, with the letters that matched for highlighting. Fuse.js runs behind it; the API is the kit's, so the library can change.
Quick start
import { createSearch } from '@v12-engine/client/kit'
interface Spell {
id: string
name: string
description: string
tags: string[]
}
const spells: Spell[] = [
{ id: 'flash', name: 'Flash', description: 'A short way towards the pointer', tags: ['movement'] },
{ id: 'brand', name: 'Brand', description: 'Burns for true damage', tags: ['damage', 'anti-heal'] }
]
const search = createSearch(spells, { keys: [{ name: 'name', weight: 3 }, 'id', { name: 'tags', weight: 2 }, { name: 'description', words: true }] })
const hits = search.search('flsh') // [{ item: Flash, rank: 4.x, matches: [{ key: 'name', value: 'Flash', ranges: [[0, 1], [2, 4]] }] }]
search.set(spells.slice(1)) // the list from now on; the index is kept between keystrokesCommon tasks
Search a list once
import { fuzzyBy, fuzzyRank, filterBy, rankBy } from '@v12-engine/client/kit'
fuzzyBy(items, query, ['name', 'id']) // hits, the best first
fuzzyRank('Greaves of Haste', 'greves') // { rank, ranges } or undefined
filterBy(items, query, (item) => item.name) // the list's order kept; a typo only when nothing else is found
rankBy(items, query, (item) => item.name) // ranked, as before; the fuzzy tier lastfuzzy: false on any of them takes the words as typed (codes, paths, where a near miss is another thing). threshold (0.4 when left out) is how far a typo may go, as a share of the word's letters.
Mark the letters that matched
<Highlight text={item.name} query={query()} /> // Solid
kit.highlight(item.name, hit.matches[0].ranges) // plain DOM: a span with the letters markedHighlight takes the query (it ranks the text itself) or the ranges a hit gave.
Search items with their stats and tags
import { searchItems, splitTags } from '@v12-engine/client/kit'
searchItems(registry, 'crit blade', { tier: ['basic', 'intermediate'] }) // names, ids, descriptions, the stats they give, tags, callings; a typo too
searchItems(registry, '#lifesteal', { calling: 'reaver' }) // #tag and tag:tag: tags every hit must have
splitTags('#crit blade') // { words: 'blade', tags: ['crit'] }registry is the game's (world.registry, or the client's session.registry on the screen). The shop window and the editor's Data use it (Tags).
Search in an editor panel
panel.filter({ placeholder }) is the field; filter.find(items, keys) the hits with the letters that matched; filter({ fuzzy: false }) takes the words as typed. A search source of an extension (search: [{ id, name, prefix, fuzzy, entries }]) and a panel's entries (searchFuzzy) are in the search of everything (Ctrl+K), a typo finding them unless they say fuzzy: false. splitPrefix(query) takes the first character off for a source searched alone. Make an editor panel, Make your own panel.
Use it in a field or a select
SearchField, SearchSelect, Select searchable and ListPanel search this way with no change for whoever uses them (The UI kit).
Keep a long list fast
createSearch keeps an index between keystrokes and makes it anew for what set changed; 5,000 entries rank under 5 ms a keystroke. Give it a limit for the few best.
Reference
createSearch(items, options) → Search
| Option | What it does |
|---|---|
keys | Property names, or SearchKeys: name, weight (1 when left out), get (what it is), words (a bag of words, never a name) |
threshold | How far a typo may go, 0..1; 0.4 when left out; words under four letters are taken as typed |
limit | How many hits at most |
fuzzy | Whether a typo finds; yes when left out |
SearchHit
| Field | What it is |
|---|---|
item | The item |
rank | 0 exact, 1 prefix, 2 word starts, 3 somewhere, 4 and up nearly; a lighter key's match between the whole numbers |
matches | By key: the text searched and the ranges of letters that matched (MatchRange), for a Highlight |
Where the search runs
| Where | What |
|---|---|
| The editor | Everything: palette (Ctrl+K), assets, content, outliner, Data (#tag, tag:x), pickers, cinematics, FX, AI, the tech and item trees |
| The shop window | searchItems over the wares, #tag the tag alone |
| Your panels | panel.filter, filter.find |
| Your shops, tools, screens | createSearch, fuzzyBy, searchItems, Highlight |
Tests: packages/client/tests/kitSearch.test.ts (ranking, typos, weights, ranges, 5,000 entries), packages/client/tests/searchItems.test.ts, packages/editor/tests/editorSearch.test.ts, packages/editor/tests/techTreeSearch.dom.test.tsx.