suggestionList.js.flow 3.8 KB

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