@visulima/string
Version:
Functions for manipulating strings.
39 lines (38 loc) • 1.5 kB
TypeScript
/** Options for {@linkcode closestString}. */
interface ClosestStringOptions {
/**
* Whether the distance should include case.
* @default {false}
*/
caseSensitive?: boolean;
/**
* A custom comparison function to use for comparing strings.
* @param a The first string for comparison.
* @param b The second string for comparison.
* @returns The distance between the two strings.
* @default {levenshteinDistance}
*/
compareFn?: (a: string, b: string) => number;
}
/**
* Finds the most similar string from an array of strings.
*
* By default, calculates the distance between words using the
* {@link https://en.wikipedia.org/wiki/Levenshtein_distance | Levenshtein distance}.
* @example Usage
* ```ts
* import { closestString } from "@visulima/string";
* import assert from "node:assert";
*
* const possibleWords = ["length", "size", "blah", "help"];
* const suggestion = closestString("hep", possibleWords);
*
* assert.deepStrictEqual(suggestion, "help");
* ```
* @param givenWord The string to measure distance against
* @param possibleWords The string-array to pick the closest string from
* @param options The options for the comparison.
* @returns The closest string from `possibleWords`, or `undefined` if `possibleWords` is empty (though the function throws in this case).
*/
declare const closestString: (givenWord: string, possibleWords: ReadonlyArray<string>, options?: ClosestStringOptions) => string | undefined;
export { ClosestStringOptions, closestString };