Function compareSimilarity
- compare
Similarity (givenWord, options?): ((a: string, b: string) => number) Parameters
- givenWord: string
The string to measure distance against.
Optionaloptions: CompareSimilarityOptionsOptions for the sort.
Returns ((a: string, b: string) => number)
The difference in distance. This will be a negative number if
ais more similar togivenWordthanb, a positive number ifbis more similar, or0if they are equally similar.- (a, b): number
Parameters
- a: string
- b: string
Returns number
Example: Usage
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"]);- givenWord: string
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.