Skip to content

Commit b3a7f7a

Browse files
committed
Implement path scoring using dynamic programming to prioritize the best fuzzy search matches
1 parent 3a9dcd5 commit b3a7f7a

2 files changed

Lines changed: 62 additions & 32 deletions

File tree

‎packages/shared/src/utils/search-paths/search-paths.spec.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe('search_paths', () => {
55
'src/index.ts',
66
'src/routes/HomePage.tsx',
77
'src/test.spec.ts',
8+
'src/test.ts',
89
'src/utils.py'
910
]
1011

@@ -30,6 +31,12 @@ describe('search_paths', () => {
3031
expect(result[0]).toBe('src/test.spec.ts')
3132
})
3233

34+
it('should find "src/test.ts" with query "testts"', () => {
35+
const result = search_paths({ paths, search_value: 'testts' })
36+
expect(result).toHaveLength(1)
37+
expect(result[0]).toBe('src/test.ts')
38+
})
39+
3340
it('should find "src/index.ts" with query "srcindex"', () => {
3441
const result = search_paths({ paths, search_value: 'srcindex' })
3542
expect(result).toHaveLength(1)

‎packages/shared/src/utils/search-paths/search-paths.ts‎

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,68 @@ const get_path_parts = (path: string): string[] => {
1111
.filter((p) => p.length > 0)
1212
}
1313

14-
const is_prefix_match_recursive = (params: {
14+
const get_match_score = (params: {
1515
query: string
1616
parts: string[]
1717
part_idx: number
18-
memo: Record<string, boolean>
19-
}): boolean => {
18+
memo: Record<string, number>
19+
}): number => {
2020
const key = `${params.part_idx}:${params.query}`
2121
if (params.memo[key] !== undefined) {
2222
return params.memo[key]
2323
}
2424

25-
if (params.query.length === 0) {
26-
return true
25+
if (params.query.length == 0) {
26+
return 0
2727
}
2828

2929
if (params.part_idx >= params.parts.length) {
30-
return false
30+
return -1
3131
}
3232

33-
// Option 1: Skip current part and try to match the query with the rest of the parts.
34-
if (
35-
is_prefix_match_recursive({
36-
query: params.query,
37-
parts: params.parts,
38-
part_idx: params.part_idx + 1,
39-
memo: params.memo
40-
})
41-
) {
42-
return (params.memo[key] = true)
43-
}
33+
// Option 1: Skip current part.
34+
const score_skipping = get_match_score({
35+
query: params.query,
36+
parts: params.parts,
37+
part_idx: params.part_idx + 1,
38+
memo: params.memo
39+
})
4440

4541
// Option 2: Match a prefix of the query with a prefix of the current part.
4642
const part = params.parts[params.part_idx]
43+
let best_score_matching = -1
44+
4745
for (let i = 1; i <= params.query.length; i++) {
4846
const query_prefix = params.query.substring(0, i)
4947
if (part.startsWith(query_prefix)) {
50-
if (
51-
is_prefix_match_recursive({
52-
query: params.query.substring(i),
53-
parts: params.parts,
54-
part_idx: params.part_idx + 1,
55-
memo: params.memo
56-
})
57-
) {
58-
return (params.memo[key] = true)
48+
const score = get_match_score({
49+
query: params.query.substring(i),
50+
parts: params.parts,
51+
part_idx: params.part_idx + 1,
52+
memo: params.memo
53+
})
54+
if (score !== -1) {
55+
if (best_score_matching === -1 || score < best_score_matching) {
56+
best_score_matching = score
57+
}
5958
}
6059
} else {
6160
break
6261
}
6362
}
6463

65-
return (params.memo[key] = false)
64+
let result = -1
65+
if (score_skipping !== -1) {
66+
result = score_skipping + 1 // +1 for skipping current part
67+
}
68+
69+
if (best_score_matching !== -1) {
70+
if (result === -1 || best_score_matching < result) {
71+
result = best_score_matching
72+
}
73+
}
74+
75+
return (params.memo[key] = result)
6676
}
6777

6878
export const search_paths = (params: {
@@ -75,24 +85,37 @@ export const search_paths = (params: {
7585
if (/[/\\\.]$/.test(params.search_value)) {
7686
return []
7787
}
78-
return params.paths.filter((path_item) => {
79-
const search_lower = params.search_value.toLowerCase()
88+
89+
const search_lower = params.search_value.toLowerCase()
90+
91+
const scored_paths = params.paths.map((path_item) => {
8092
const path_to_search_lower = path_item.toLowerCase()
8193

8294
if (path_to_search_lower.includes(search_lower)) {
83-
return true
95+
return { path: path_item, score: 0 }
8496
}
8597

8698
if (/[^a-z0-9]/.test(search_lower)) {
87-
return false
99+
return { path: path_item, score: -1 }
88100
}
89101

90102
const parts = get_path_parts(path_item)
91-
return is_prefix_match_recursive({
103+
const score = get_match_score({
92104
query: search_lower,
93105
parts,
94106
part_idx: 0,
95107
memo: {}
96108
})
109+
return { path: path_item, score }
97110
})
111+
112+
const matched_paths = scored_paths.filter((item) => item.score !== -1)
113+
114+
if (matched_paths.length === 0) return []
115+
116+
const min_score = Math.min(...matched_paths.map((item) => item.score))
117+
118+
return matched_paths
119+
.filter((item) => item.score === min_score)
120+
.map((item) => item.path)
98121
}

0 commit comments

Comments
 (0)