You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.4 KiB

2 years ago
  1. didYouMean.js - A simple JavaScript matching engine
  2. ===================================================
  3. [Available on GitHub](https://github.com/dcporter/didyoumean.js).
  4. A super-simple, highly optimized JS library for matching human-quality input to a list of potential
  5. matches. You can use it to suggest a misspelled command-line utility option to a user, or to offer
  6. links to nearby valid URLs on your 404 page. (The examples below are taken from a personal project,
  7. my [HTML5 business card](http://dcporter.aws.af.cm/me), which uses didYouMean.js to suggest correct
  8. URLs from misspelled ones, such as [dcporter.aws.af.cm/me/instagarm](http://dcporter.aws.af.cm/me/instagarm).)
  9. Uses the [Levenshtein distance algorithm](https://en.wikipedia.org/wiki/Levenshtein_distance).
  10. didYouMean.js works in the browser as well as in node.js. To install it for use in node:
  11. ```
  12. npm install didyoumean
  13. ```
  14. Examples
  15. --------
  16. Matching against a list of strings:
  17. ```
  18. var input = 'insargrm'
  19. var list = ['facebook', 'twitter', 'instagram', 'linkedin'];
  20. console.log(didYouMean(input, list));
  21. > 'instagram'
  22. // The method matches 'insargrm' to 'instagram'.
  23. input = 'google plus';
  24. console.log(didYouMean(input, list));
  25. > null
  26. // The method was unable to find 'google plus' in the list of options.
  27. ```
  28. Matching against a list of objects:
  29. ```
  30. var input = 'insargrm';
  31. var list = [ { id: 'facebook' }, { id: 'twitter' }, { id: 'instagram' }, { id: 'linkedin' } ];
  32. var key = 'id';
  33. console.log(didYouMean(input, list, key));
  34. > 'instagram'
  35. // The method returns the matching value.
  36. didYouMean.returnWinningObject = true;
  37. console.log(didYouMean(input, list, key));
  38. > { id: 'instagram' }
  39. // The method returns the matching object.
  40. ```
  41. didYouMean(str, list, [key])
  42. ----------------------------
  43. - str: The string input to match.
  44. - list: An array of strings or objects to match against.
  45. - key (OPTIONAL): If your list array contains objects, you must specify the key which contains the string
  46. to match against.
  47. Returns: the closest matching string, or null if no strings exceed the threshold.
  48. Options
  49. -------
  50. Options are set on the didYouMean function object. You may change them at any time.
  51. ### threshold
  52. By default, the method will only return strings whose edit distance is less than 40% (0.4x) of their length.
  53. For example, if a ten-letter string is five edits away from its nearest match, the method will return null.
  54. You can control this by setting the "threshold" value on the didYouMean function. For example, to set the
  55. edit distance threshold to 50% of the input string's length:
  56. ```
  57. didYouMean.threshold = 0.5;
  58. ```
  59. To return the nearest match no matter the threshold, set this value to null.
  60. ### thresholdAbsolute
  61. This option behaves the same as threshold, but instead takes an integer number of edit steps. For example,
  62. if thresholdAbsolute is set to 20 (the default), then the method will only return strings whose edit distance
  63. is less than 20. Both options apply.
  64. ### caseSensitive
  65. By default, the method will perform case-insensitive comparisons. If you wish to force case sensitivity, set
  66. the "caseSensitive" value to true:
  67. ```
  68. didYouMean.caseSensitive = true;
  69. ```
  70. ### nullResultValue
  71. By default, the method will return null if there is no sufficiently close match. You can change this value here.
  72. ### returnWinningObject
  73. By default, the method will return the winning string value (if any). If your list contains objects rather
  74. than strings, you may set returnWinningObject to true.
  75. ```
  76. didYouMean.returnWinningObject = true;
  77. ```
  78. This option has no effect on lists of strings.
  79. ### returnFirstMatch
  80. By default, the method will search all values and return the closest match. If you're simply looking for a "good-
  81. enough" match, you can set your thresholds appropriately and set returnFirstMatch to true to substantially speed
  82. things up.
  83. License
  84. -------
  85. didYouMean copyright (c) 2013-2014 Dave Porter.
  86. Licensed under the Apache License, Version 2.0 (the "License");
  87. you may not use this file except in compliance with the License.
  88. You may obtain a copy of the License
  89. [here](http://www.apache.org/licenses/LICENSE-2.0).
  90. Unless required by applicable law or agreed to in writing, software
  91. distributed under the License is distributed on an "AS IS" BASIS,
  92. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  93. See the License for the specific language governing permissions and
  94. limitations under the License.