MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / fuzzySearch

Function fuzzySearch

src/core/diff/strategies/multi-search-replace.ts:37–73  ·  view source on GitHub ↗

* Performs a "middle-out" search of `lines` (between [startIndex, endIndex]) to find * the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text.

(lines: string[], searchChunk: string, startIndex: number, endIndex: number)

Source from the content-addressed store, hash-verified

35 * the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text.
36 */
37function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, endIndex: number) {
38 let bestScore = 0
39 let bestMatchIndex = -1
40 let bestMatchContent = ""
41 const searchLen = searchChunk.split(/\r?\n/).length
42
43 // Middle-out from the midpoint
44 const midPoint = Math.floor((startIndex + endIndex) / 2)
45 let leftIndex = midPoint
46 let rightIndex = midPoint + 1
47
48 while (leftIndex >= startIndex || rightIndex <= endIndex - searchLen) {
49 if (leftIndex >= startIndex) {
50 const originalChunk = lines.slice(leftIndex, leftIndex + searchLen).join("\n")
51 const similarity = getSimilarity(originalChunk, searchChunk)
52 if (similarity > bestScore) {
53 bestScore = similarity
54 bestMatchIndex = leftIndex
55 bestMatchContent = originalChunk
56 }
57 leftIndex--
58 }
59
60 if (rightIndex <= endIndex - searchLen) {
61 const originalChunk = lines.slice(rightIndex, rightIndex + searchLen).join("\n")
62 const similarity = getSimilarity(originalChunk, searchChunk)
63 if (similarity > bestScore) {
64 bestScore = similarity
65 bestMatchIndex = rightIndex
66 bestMatchContent = originalChunk
67 }
68 rightIndex++
69 }
70 }
71
72 return { bestScore, bestMatchIndex, bestMatchContent }
73}
74
75export class MultiSearchReplaceDiffStrategy implements DiffStrategy {
76 private fuzzyThreshold: number

Callers 1

applyDiffMethod · 0.85

Calls 1

getSimilarityFunction · 0.85

Tested by

no test coverage detected