function nearestName
nearestName(
input: string,
candidates: Iterable<string>
): string | undefined

Picks the candidate closest to a misspelling, for a "did you mean" line.

Comparison is case-insensitive, so pngfile finds pngFile, and a candidate only counts as close when it is within roughly a third of the typed word in edits — otherwise nothing is suggested rather than something misleading. Ties go to the earliest candidate.

A word that is already among the candidates is not a misspelling of anything, and gets no suggestion. It reads as one otherwise: a listed package that would not load answered cannot read jsr:@binstruct/bencode … did you mean bencode?, correcting the user's spelling to the spelling they had used. The guard is exact-match on the word as typed rather than a zero-distance one, because a distance of zero is also what makes pngfile find pngFile, which is worth keeping.

Examples

Case and small typos still find the intended name

import { assertEquals } from "@std/assert";
import { nearestName } from "./guide.ts";

const coders = ["pngFile", "pngChunkUnknown", "pngFileChunks"];

assertEquals(nearestName("pngfile", coders), "pngFile");
assertEquals(nearestName("pngFiles", coders), "pngFile");
assertEquals(nearestName("totallyUnrelated", coders), undefined);

A name that is already a candidate suggests nothing

import { assertEquals } from "@std/assert";
import { nearestName } from "./guide.ts";

assertEquals(nearestName("pngFile", ["pngFile", "pngFileChunks"]), undefined);

Parameters

input: string

The word as typed

candidates: Iterable<string>

The names that would have been accepted

Return Type

string | undefined

The nearest candidate, or undefined when none is close enough