-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdefaultStrategy.ts
38 lines (32 loc) · 1014 Bytes
/
defaultStrategy.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
import { ScoreContext } from "../match";
/**
* Increments a context's score based on the context's values
* This default strategy is based on
* https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
* A fuzzy matching scoring function should most of the time push a big score
* when matching a leading letter (ie. a letter that is capital or comes
* after a separator).
*
* @param previousContext The last context given to pushScore. undefined if first match
* @param context The actual context
* @returns The new score
*/
export function pushScore(
previousContext: ScoreContext | undefined,
context: ScoreContext
): number {
if (!context) {
throw new TypeError("Expecting context to be defined");
}
if (!context.match) {
return context.currentScore - 1;
}
let increment = 0;
if (previousContext && previousContext.match) {
increment += 5;
}
if (context.leading) {
increment += 10;
}
return context.currentScore + increment;
}