UNPKG

@visulima/string

Version:

Functions for manipulating strings.

26 lines (25 loc) 1.15 kB
declare const distance: (a: string, b: string) => number; declare const closest: (str: string, arr: readonly string[]) => string; declare const closestN: (string_: string, array: ReadonlyArray<string>, n: number) => (string | undefined)[]; /** * Computes a normalized similarity score between two strings in the range `[0, 1]`, * where `1` means the strings are identical and `0` means maximally different. * * The score is derived from the Levenshtein edit distance, normalized by the * length of the longer string: `1 - distance(a, b) / max(a.length, b.length)`. * Two empty strings are considered identical (`1`). * * This is convenient for threshold-based "did you mean?" UX, complementing the * raw {@link distance} and the sort-based helpers. * @example * ```typescript * similarity("kitten", "sitting"); // => ~0.571 * similarity("foo", "foo"); // => 1 * similarity("", ""); // => 1 * ``` * @param a The first string. * @param b The second string. * @returns A similarity score between 0 and 1 (inclusive). */ declare const similarity: (a: string, b: string) => number; export { closest, closestN, distance, similarity };