-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
fuzzy-find.ts
55 lines (47 loc) · 1.4 KB
/
fuzzy-find.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as fuzzAldrin from 'fuzzaldrin-plus'
import { compareDescending } from './compare'
function score(str: string, query: string, maxScore: number) {
return fuzzAldrin.score(str, query) / maxScore
}
export interface IMatches {
readonly title: ReadonlyArray<number>
readonly subtitle: ReadonlyArray<number>
}
export interface IMatch<T> {
/** `0 <= score <= 1` */
score: number
item: T
matches: IMatches
}
export type KeyFunction<T> = (item: T) => ReadonlyArray<string>
export function match<T>(
query: string,
items: ReadonlyArray<T>,
getKey: KeyFunction<T>
): ReadonlyArray<IMatch<T>> {
// matching `query` against itself is a perfect match.
const maxScore = score(query, query, 1)
const result = items
.map(
(item): IMatch<T> => {
const matches: Array<ReadonlyArray<number>> = []
const itemTextArray = getKey(item)
itemTextArray.forEach(text => {
matches.push(fuzzAldrin.match(text, query))
})
return {
score: score(itemTextArray.join(''), query, maxScore),
item,
matches: {
title: matches[0],
subtitle: matches.length > 1 ? matches[1] : [],
},
}
}
)
.filter(
({ matches }) => matches.title.length > 0 || matches.subtitle.length > 0
)
.sort(({ score: left }, { score: right }) => compareDescending(left, right))
return result
}