suggestionList.js 4.0 KB

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