suggestionList.mjs 3.6 KB

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