suggestionList.js.flow 3.9 KB

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