suggestionList.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = suggestionList;
  6. /**
  7. * Given an invalid input string and a list of valid options, returns a filtered
  8. * list of valid options sorted based on their similarity with the input.
  9. */
  10. function suggestionList(input, options) {
  11. var optionsByDistance = Object.create(null);
  12. var lexicalDistance = new LexicalDistance(input);
  13. var threshold = Math.floor(input.length * 0.4) + 1;
  14. for (var _i2 = 0; _i2 < options.length; _i2++) {
  15. var option = options[_i2];
  16. var distance = lexicalDistance.measure(option, threshold);
  17. if (distance !== undefined) {
  18. optionsByDistance[option] = distance;
  19. }
  20. }
  21. return Object.keys(optionsByDistance).sort(function (a, b) {
  22. var distanceDiff = optionsByDistance[a] - optionsByDistance[b];
  23. return distanceDiff !== 0 ? distanceDiff : a.localeCompare(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. var LexicalDistance = /*#__PURE__*/function () {
  41. function LexicalDistance(input) {
  42. this._input = input;
  43. this._inputLowerCase = input.toLowerCase();
  44. this._inputArray = stringToArray(this._inputLowerCase);
  45. this._rows = [new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0)];
  46. }
  47. var _proto = LexicalDistance.prototype;
  48. _proto.measure = function measure(option, threshold) {
  49. if (this._input === option) {
  50. return 0;
  51. }
  52. var optionLowerCase = option.toLowerCase(); // Any case change counts as a single edit
  53. if (this._inputLowerCase === optionLowerCase) {
  54. return 1;
  55. }
  56. var a = stringToArray(optionLowerCase);
  57. var b = this._inputArray;
  58. if (a.length < b.length) {
  59. var tmp = a;
  60. a = b;
  61. b = tmp;
  62. }
  63. var aLength = a.length;
  64. var bLength = b.length;
  65. if (aLength - bLength > threshold) {
  66. return undefined;
  67. }
  68. var rows = this._rows;
  69. for (var j = 0; j <= bLength; j++) {
  70. rows[0][j] = j;
  71. }
  72. for (var i = 1; i <= aLength; i++) {
  73. var upRow = rows[(i - 1) % 3];
  74. var currentRow = rows[i % 3];
  75. var smallestCell = currentRow[0] = i;
  76. for (var _j = 1; _j <= bLength; _j++) {
  77. var cost = a[i - 1] === b[_j - 1] ? 0 : 1;
  78. var currentCell = Math.min(upRow[_j] + 1, // delete
  79. currentRow[_j - 1] + 1, // insert
  80. upRow[_j - 1] + cost // substitute
  81. );
  82. if (i > 1 && _j > 1 && a[i - 1] === b[_j - 2] && a[i - 2] === b[_j - 1]) {
  83. // transposition
  84. var doubleDiagonalCell = rows[(i - 2) % 3][_j - 2];
  85. currentCell = Math.min(currentCell, doubleDiagonalCell + 1);
  86. }
  87. if (currentCell < smallestCell) {
  88. smallestCell = currentCell;
  89. }
  90. currentRow[_j] = currentCell;
  91. } // Early exit, since distance can't go smaller than smallest element of the previous row.
  92. if (smallestCell > threshold) {
  93. return undefined;
  94. }
  95. }
  96. var distance = rows[aLength % 3][bLength];
  97. return distance <= threshold ? distance : undefined;
  98. };
  99. return LexicalDistance;
  100. }();
  101. function stringToArray(str) {
  102. var strLength = str.length;
  103. var array = new Array(strLength);
  104. for (var i = 0; i < strLength; ++i) {
  105. array[i] = str.charCodeAt(i);
  106. }
  107. return array;
  108. }