Function closestString

  • Finds the most similar string from an array of strings.

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

    Parameters

    • givenWord: string

      The string to measure distance against

    • possibleWords: readonly string[]

      The string-array to pick the closest string from

    • Optionaloptions: ClosestStringOptions

      The options for the comparison.

    Returns string

    The closest string

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

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

    assertEquals(suggestion, "help");