wordSimilaritySort

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

function

function wordSimilaritySort(givenWord: string, possibleWords: ReadonlyArray<string>, options?: WordSimilaritySortOptions): string[]

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

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

options?: WordSimilaritySortOptions

Options for the sort.

Returns

A sorted copy of possibleWords.

Example: Basic usage

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

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

assertEquals(suggestions, ["help", "size", "blah", "length"]);

Example: Case-sensitive sorting

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

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

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

Source