MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / closestMatch

Function closestMatch

curriculum/src/filter.ts:166–197  ·  view source on GitHub ↗
(
  target: string,
  xs: string[]
)

Source from the content-addressed store, hash-verified

164}
165
166export function closestMatch(
167 target: string,
168 xs: string[]
169): { closest: string; score: number } {
170 const [firstCandidate, ...rest] = xs;
171
172 if (!firstCandidate) {
173 return { closest: target, score: 0 };
174 }
175
176 const normalizedTarget = normalizeForComparison(target);
177
178 let closest = firstCandidate;
179 let closestScore = getSimilarityScore(
180 normalizedTarget,
181 normalizeForComparison(closest)
182 );
183
184 for (const candidate of rest) {
185 const score = getSimilarityScore(
186 normalizedTarget,
187 normalizeForComparison(candidate)
188 );
189
190 if (score > closestScore) {
191 closest = candidate;
192 closestScore = score;
193 }
194 }
195
196 return { closest, score: closestScore };
197}
198
199export function closestFilters(
200 superblocks: Filterable[],

Callers 2

filter.test.jsFile · 0.90
closestFiltersFunction · 0.85

Calls 2

normalizeForComparisonFunction · 0.85
getSimilarityScoreFunction · 0.85

Tested by

no test coverage detected