UNPKG

3.81 kBJavaScriptView Raw
1import naturalCompare from "./naturalCompare.mjs";
2/**
3 * Given an invalid input string and a list of valid options, returns a filtered
4 * list of valid options sorted based on their similarity with the input.
5 */
6
7export default function suggestionList(input, options) {
8 var optionsByDistance = Object.create(null);
9 var lexicalDistance = new LexicalDistance(input);
10 var threshold = Math.floor(input.length * 0.4) + 1;
11
12 for (var _i2 = 0; _i2 < options.length; _i2++) {
13 var option = options[_i2];
14 var distance = lexicalDistance.measure(option, threshold);
15
16 if (distance !== undefined) {
17 optionsByDistance[option] = distance;
18 }
19 }
20
21 return Object.keys(optionsByDistance).sort(function (a, b) {
22 var distanceDiff = optionsByDistance[a] - optionsByDistance[b];
23 return distanceDiff !== 0 ? distanceDiff : naturalCompare(a, b);
24 });
25}
26/**
27 * Computes the lexical distance between strings A and B.
28 *
29 * The "distance" between two strings is given by counting the minimum number
30 * of edits needed to transform string A into string B. An edit can be an
31 * insertion, deletion, or substitution of a single character, or a swap of two
32 * adjacent characters.
33 *
34 * Includes a custom alteration from Damerau-Levenshtein to treat case changes
35 * as a single edit which helps identify mis-cased values with an edit distance
36 * of 1.
37 *
38 * This distance can be useful for detecting typos in input or sorting
39 */
40
41var LexicalDistance = /*#__PURE__*/function () {
42 function LexicalDistance(input) {
43 this._input = input;
44 this._inputLowerCase = input.toLowerCase();
45 this._inputArray = stringToArray(this._inputLowerCase);
46 this._rows = [new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0)];
47 }
48
49 var _proto = LexicalDistance.prototype;
50
51 _proto.measure = function measure(option, threshold) {
52 if (this._input === option) {
53 return 0;
54 }
55
56 var optionLowerCase = option.toLowerCase(); // Any case change counts as a single edit
57
58 if (this._inputLowerCase === optionLowerCase) {
59 return 1;
60 }
61
62 var a = stringToArray(optionLowerCase);
63 var b = this._inputArray;
64
65 if (a.length < b.length) {
66 var tmp = a;
67 a = b;
68 b = tmp;
69 }
70
71 var aLength = a.length;
72 var bLength = b.length;
73
74 if (aLength - bLength > threshold) {
75 return undefined;
76 }
77
78 var rows = this._rows;
79
80 for (var j = 0; j <= bLength; j++) {
81 rows[0][j] = j;
82 }
83
84 for (var i = 1; i <= aLength; i++) {
85 var upRow = rows[(i - 1) % 3];
86 var currentRow = rows[i % 3];
87 var smallestCell = currentRow[0] = i;
88
89 for (var _j = 1; _j <= bLength; _j++) {
90 var cost = a[i - 1] === b[_j - 1] ? 0 : 1;
91 var currentCell = Math.min(upRow[_j] + 1, // delete
92 currentRow[_j - 1] + 1, // insert
93 upRow[_j - 1] + cost // substitute
94 );
95
96 if (i > 1 && _j > 1 && a[i - 1] === b[_j - 2] && a[i - 2] === b[_j - 1]) {
97 // transposition
98 var doubleDiagonalCell = rows[(i - 2) % 3][_j - 2];
99 currentCell = Math.min(currentCell, doubleDiagonalCell + 1);
100 }
101
102 if (currentCell < smallestCell) {
103 smallestCell = currentCell;
104 }
105
106 currentRow[_j] = currentCell;
107 } // Early exit, since distance can't go smaller than smallest element of the previous row.
108
109
110 if (smallestCell > threshold) {
111 return undefined;
112 }
113 }
114
115 var distance = rows[aLength % 3][bLength];
116 return distance <= threshold ? distance : undefined;
117 };
118
119 return LexicalDistance;
120}();
121
122function stringToArray(str) {
123 var strLength = str.length;
124 var array = new Array(strLength);
125
126 for (var i = 0; i < strLength; ++i) {
127 array[i] = str.charCodeAt(i);
128 }
129
130 return array;
131}