UNPKG

@visulima/string

Version:

Functions for manipulating strings.

41 lines (40 loc) 1.57 kB
/** Options for {@linkcode wordSimilaritySort}. */ interface WordSimilaritySortOptions { caseSensitive?: boolean; compareFunction?: (a: string, b: string) => number; } /** * Sorts a string-array by similarity to a given string. * * By default, calculates the distance between words using the * {@link https://en.wikipedia.org/wiki/Levenshtein_distance | Levenshtein distance}. * @example Basic usage * * ```ts * import { wordSimilaritySort } from "@visulima/string"; * import assert from "node:assert"; * * const possibleWords = ["length", "size", "blah", "help"]; * const suggestions = wordSimilaritySort("hep", possibleWords); * * assert.deepStrictEqual(suggestions, ["help", "size", "blah", "length"]); * ``` * @example Case-sensitive sorting * * ```ts * import { wordSimilaritySort } from "@visulima/string"; * import assert from "node:assert"; * * const possibleWords = ["length", "Size", "blah", "HELP"]; * const suggestions = wordSimilaritySort("hep", possibleWords, { caseSensitive: true }); * * assert.deepStrictEqual(suggestions, ["Size", "blah", "HELP", "length"]); * ``` * @param givenWord The string to measure distance against. * @param possibleWords The string-array that will be sorted. This array will * not be mutated, but the sorted copy will be returned. * @param options Options for the sort. * @returns A sorted copy of `possibleWords`. */ declare const wordSimilaritySort: (givenWord: string, possibleWords: ReadonlyArray<string>, options?: WordSimilaritySortOptions) => string[]; export { WordSimilaritySortOptions, wordSimilaritySort };