-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.ts
More file actions
66 lines (62 loc) · 1.88 KB
/
scoring.ts
File metadata and controls
66 lines (62 loc) · 1.88 KB
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
56
57
58
59
60
61
62
63
64
65
66
import Config from '@app/constants.ts'
/**
* Fuzzy match scoring utilities.
* @description Calculates score bonuses for match positions.
*/
export default class Scoring {
/**
* Check if character is a path boundary.
* @description Matches slash, dash, underscore, dot, or space.
* @param charCode - Unicode code point to check
* @returns True if character is a boundary
*/
static isBoundary(charCode: number): boolean {
return (
charCode === 47 ||
charCode === 92 ||
charCode === 45 ||
charCode === 95 ||
charCode === 46 ||
charCode === 32
)
}
/**
* Check if character is lowercase.
* @description Matches a-z ASCII range.
* @param charCode - Unicode code point to check
* @returns True if character is lowercase
*/
static isLower(charCode: number): boolean {
return charCode >= 97 && charCode <= 122
}
/**
* Check if character is uppercase.
* @description Matches A-Z ASCII range.
* @param charCode - Unicode code point to check
* @returns True if character is uppercase
*/
static isUpper(charCode: number): boolean {
return charCode >= 65 && charCode <= 90
}
/**
* Calculate bonus score for match position.
* @description Awards bonus for boundaries and camelCase.
* @param path - Full file path being searched
* @param position - Index of matched character
* @param isFirst - Whether this is first query character
* @returns Bonus score value
*/
static scoreBonusAt(path: string, position: number, isFirst: boolean): number {
if (position === 0) {
return isFirst ? Config.bonusFirst : 0
}
const prevChar = path.charCodeAt(position - 1)
if (Scoring.isBoundary(prevChar)) {
return Config.bonusBoundary
}
if (Scoring.isLower(prevChar) && Scoring.isUpper(path.charCodeAt(position))) {
return Config.bonusCamel
}
return 0
}
}