Files
30-seconds-of-code/node_modules/graphql/jsutils/suggestionList.mjs
2019-08-20 15:52:05 +02:00

98 lines
2.7 KiB
JavaScript

/**
* Given an invalid input string and a list of valid options, returns a filtered
* list of valid options sorted based on their similarity with the input.
*/
export default function suggestionList(input, options) {
var optionsByDistance = Object.create(null);
var inputThreshold = input.length / 2;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = options[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var option = _step.value;
var distance = lexicalDistance(input, option);
var threshold = Math.max(inputThreshold, option.length / 2, 1);
if (distance <= threshold) {
optionsByDistance[option] = distance;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return Object.keys(optionsByDistance).sort(function (a, b) {
return optionsByDistance[a] - optionsByDistance[b];
});
}
/**
* Computes the lexical distance between strings A and B.
*
* The "distance" between two strings is given by counting the minimum number
* of edits needed to transform string A into string B. An edit can be an
* insertion, deletion, or substitution of a single character, or a swap of two
* adjacent characters.
*
* Includes a custom alteration from Damerau-Levenshtein to treat case changes
* as a single edit which helps identify mis-cased values with an edit distance
* of 1.
*
* This distance can be useful for detecting typos in input or sorting
*
* @param {string} a
* @param {string} b
* @return {int} distance in number of edits
*/
function lexicalDistance(aStr, bStr) {
if (aStr === bStr) {
return 0;
}
var i;
var j;
var d = [];
var a = aStr.toLowerCase();
var b = bStr.toLowerCase();
var aLength = a.length;
var bLength = b.length; // Any case change counts as a single edit
if (a === b) {
return 1;
}
for (i = 0; i <= aLength; i++) {
d[i] = [i];
}
for (j = 1; j <= bLength; j++) {
d[0][j] = j;
}
for (i = 1; i <= aLength; i++) {
for (j = 1; j <= bLength; j++) {
var cost = a[i - 1] === b[j - 1] ? 0 : 1;
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}
return d[aLength][bLength];
}