closestString

import { closestString } from "jsr:@ende/text";

function

function closestString(givenWord: string, possibleWords: ReadonlyArray<string>, options?: ClosestStringOptions): string

Finds the most similar string from an array of strings.

By default, calculates the distance between words using the Levenshtein distance.

givenWord: string

The string to measure distance against

possibleWords: ReadonlyArray<string>

The string-array to pick the closest string from

options?: ClosestStringOptions

The options for the comparison.

Returns

The closest string

Example: Usage

import { closestString } from "@ende/text/closest-string";
import { assertEquals } from "@ende/assert";

const possibleWords = ["length", "size", "blah", "help"];
const suggestion = closestString("hep", possibleWords);

assertEquals(suggestion, "help");

Source