Function wordSimilaritySort

  • Sorts a string-array by similarity to a given string.

    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 that will be sorted. This array will not be mutated, but the sorted copy will be returned.

    • Optionaloptions: WordSimilaritySortOptions

      Options for the sort.

    Returns string[]

    A sorted copy of possibleWords.

    import { wordSimilaritySort } from "@std/text/word-similarity-sort";
    import { assertEquals } from "@std/assert";

    const possibleWords = ["length", "size", "blah", "help"];
    const suggestions = wordSimilaritySort("hep", possibleWords);

    assertEquals(suggestions, ["help", "size", "blah", "length"]);
    import { wordSimilaritySort } from "@std/text/word-similarity-sort";
    import { assertEquals } from "@std/assert";

    const possibleWords = ["length", "Size", "blah", "HELP"];
    const suggestions = wordSimilaritySort("hep", possibleWords, { caseSensitive: true });

    assertEquals(suggestions, ["Size", "blah", "HELP", "length"]);