Function compareSimilarity

  • Takes a string and generates a comparator function to determine which of two strings is more similar to the given one.

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

    Parameters

    • givenWord: string

      The string to measure distance against.

    • Optionaloptions: CompareSimilarityOptions

      Options for the sort.

    Returns ((a: string, b: string) => number)

    The difference in distance. This will be a negative number if a is more similar to givenWord than b, a positive number if b is more similar, or 0 if they are equally similar.

      • (a, b): number
      • Parameters

        • a: string
        • b: string

        Returns number

    Most-similar words will be at the start of the array.

    import { compareSimilarity } from "@std/text/compare-similarity";
    import { assertEquals } from "@std/assert";

    const words = ["hi", "hello", "help"];
    const sortedWords = words.toSorted(compareSimilarity("hep"));

    assertEquals(sortedWords, ["help", "hi", "hello"]);