// Getting around the circuits: the way back shown in the top bar, folded // when it is long, and how to reach a circuit picked in the search box. import { matchScore } from './match' /** The names after Main: the last `keep` stay in the bar, the ones before fold into "…". */ export function foldCrumbs(path: T[], keep: number): { folded: T[]; shown: T[] } { const n = Math.max(1, keep) if (path.length <= n) return { folded: [], shown: path } return { folded: path.slice(0, path.length - n), shown: path.slice(-n) } } /** * How to get to circuit `id` from where you are: Main starts the way anew, * one on the way here goes back to it (its step in `nav`), any other opens * one step further in. */ export function wayTo(nav: string[], mainId: string, id: string): 'main' | 'open' | number { if (id === mainId) return 'main' const back = nav.indexOf(id) return back >= 0 ? back : 'open' } /** Circuits whose name matches what was typed, best first; nothing typed, none. */ export function circuitsNamed(defs: T[], query: string, limit = 5): T[] { const q = query.trim().toLowerCase() if (!q) return [] return defs .map(d => ({ d, score: matchScore(d.name, q) })) .filter(x => x.score > 0) .sort((a, b) => b.score - a.score || a.d.name.length - b.d.name.length) .slice(0, limit) .map(x => x.d) }