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