suggestionList.mjs 3.7 KB

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