diff --git a/README.md b/README.md index e7e544b..469dde7 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,44 @@ if (idxs != null && idxs.length > 0) { uFuzzy provides a `uf.search(haystack, needle, outOfOrder = 0, infoThresh = 1e3) => [idxs, info, order]` wrapper which combines the `filter`, `info`, `sort` steps above. This method also implements efficient logic for matching search terms out of order and support for multiple substring exclusions, e.g. `fruit -green -melon`. +Example of retrieving search results in order of ranking: + +```js +let haystack = [ + 'example', + 'exemple', + 'another example', + 'nonmatch' +]; + +let needle = 'example'; + +let u = new uFuzzy({ intraMode: 1 }); + +// idxs = array, haystack indexes with a match, unordered +// info = array, match details for highlighting and ranking +// order = array, rankings for ordering each item in idxs +let [idxs,info,order] = u.search(haystack, needle); + +let sortHaystackByOrder = (idxs, order) => { + // idxs may be null, for example if the search term + // was all whitespace. + if (idxs == null) + return []; + + // Order may be null when doing out of order searching + // when the # of search terms exceed the max allowed + if (order == null) + return idxs; + + return order.map(index => idxs[index]); +} + +// Results: +// ["example", "another example", "exemple"] +let orderedTerms = sortHaystackByOrder(idxs, order); +``` + --- ### Match Highlighting @@ -365,7 +403,7 @@ Options with an **inter** prefix apply to allowances _in between_ search terms,
intraMode: 1
only,intraMode
(either 0
or 1
)intraRules
(text) => { intraSlice, intraIns, intraSub, intraTrn, intraDel }