deno.land / std@0.224.0 / text / word_similarity_sort.ts

word_similarity_sort.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import { compareSimilarity } from "./compare_similarity.ts";
/** * Sorts a string-array by similarity to a given string * * @example * ```ts * import { wordSimilaritySort } from "https://deno.land/std@$STD_VERSION/text/word_similarity_sort.ts"; * * const possibleWords = ["length", "size", "blah", "help"]; * * // case-insensitive by default * const suggestions = wordSimilaritySort("hep", possibleWords).join(", "); * * // force case sensitive * wordSimilaritySort("hep", possibleWords, { caseSensitive: true }); * ``` * * @param givenWord - The string to measure distance against * @param possibleWords - The string-array that will be sorted * @param options.caseSensitive - Flag indicating whether the distance should include case. Default is false. * @returns {string[]} A sorted copy of possibleWords */export function wordSimilaritySort( givenWord: string, possibleWords: string[], options?: { caseSensitive?: boolean; },): string[] { const { caseSensitive } = { ...options };
// this distance metric could be swapped/improved in the future return [...possibleWords].sort( compareSimilarity(givenWord, { caseSensitive }), );}
std

Version Info

Tagged at
3 weeks ago