| 114 | } |
| 115 | |
| 116 | function fuzzySequenceScore(haystack: string, needle: string) { |
| 117 | let score = 0; |
| 118 | let previousIndex = -1; |
| 119 | let searchFrom = 0; |
| 120 | |
| 121 | for (const character of needle) { |
| 122 | const index = haystack.indexOf(character, searchFrom); |
| 123 | if (index === -1) return 0; |
| 124 | |
| 125 | if (index === 0 || haystack[index - 1] === " ") { |
| 126 | score += 8; |
| 127 | } else if (index === previousIndex + 1) { |
| 128 | score += 6; |
| 129 | } else { |
| 130 | score += 2; |
| 131 | } |
| 132 | |
| 133 | previousIndex = index; |
| 134 | searchFrom = index + 1; |
| 135 | } |
| 136 | |
| 137 | return score + Math.max(0, 12 - haystack.length / 8); |
| 138 | } |
| 139 | |
| 140 | function scoreTerm(field: string, term: string) { |
| 141 | const normalized = normalizeSearchText(field); |