You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
651 B
TypeScript
23 lines
651 B
TypeScript
/** Pure matcher for the command palette — every query term must appear. */
|
|
|
|
export interface PaletteItem {
|
|
id: string
|
|
label: string
|
|
group: string
|
|
hint?: string
|
|
}
|
|
|
|
export function matchItems(items: PaletteItem[], q: string, limit = 12): PaletteItem[] {
|
|
const terms = q.trim().toLowerCase().split(/\s+/).filter((t) => t !== '')
|
|
if (terms.length === 0) return items.slice(0, limit)
|
|
const out: PaletteItem[] = []
|
|
for (const item of items) {
|
|
const hay = `${item.label} ${item.hint ?? ''}`.toLowerCase()
|
|
if (terms.every((t) => hay.includes(t))) {
|
|
out.push(item)
|
|
if (out.length >= limit) break
|
|
}
|
|
}
|
|
return out
|
|
}
|