suggestionList.js 3.7 KB

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