compareSimilarity

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

function

function compareSimilarity(givenWord: string, options?: CompareSimilarityOptions): (a: string, b: string) => number

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.

givenWord: string

The string to measure distance against.

options?: CompareSimilarityOptions

Options for the sort.

Returns

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.

Example: Usage

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

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

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

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

Source