(items: T[], query: string, search: (item: T) => string)
| 1 | import { commandScore } from '@campsite/ui' |
| 2 | |
| 3 | export function commandScoreSort<T>(items: T[], query: string, search: (item: T) => string) { |
| 4 | return items |
| 5 | .map((item) => { |
| 6 | const score = commandScore(search(item), query) |
| 7 | |
| 8 | return { score, item } |
| 9 | }) |
| 10 | .filter(({ score }) => score > 0) |
| 11 | .sort((a, b) => { |
| 12 | if (a.score === b.score) { |
| 13 | return search(a.item).localeCompare(search(b.item)) |
| 14 | } |
| 15 | return b.score - a.score |
| 16 | }) |
| 17 | .map(({ item }) => item) |
| 18 | } |
no test coverage detected