Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation on intraRules, search example #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +215 to +216
Copy link
Owner

@leeoniya leeoniya Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, any of them can be null.

iirc 🤣 ...

  • idxs can be null if the search string ends up empty after trimming whitespace and removing non-alphanumeric chars (except those in quotes, for exact matches)
  • info will be null if the number of results exceeds infoThresh

Copy link
Owner

@leeoniya leeoniya Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and they're null in sequence.

if order is not null, then idxs and info is not null.
if info is not null then idxs is not null
if idxs is null, everything is null

if (order == null)
return idxs;

return order.map(index => idxs[index]);
}

// Results:
// ["example", "another example", "exemple"]
let orderedTerms = sortHaystackByOrder(idxs, order);
```

---
### Match Highlighting

Expand Down Expand Up @@ -365,7 +403,7 @@ Options with an **inter** prefix apply to allowances _in between_ search terms,
</td>
<td>
For <code>intraMode: 1</code> only,<br>
Error types to tolerate within terms
Error types to tolerate within terms - substitution (replacement), transposition (swap), and deletion (omission)
</td>
<td>Matches the value of <code>intraMode</code> (either <code>0</code> or <code>1</code>)</td>
<td>
Expand Down Expand Up @@ -433,6 +471,14 @@ Options with an **inter** prefix apply to allowances _in between_ search terms,
Demo: <a href="https://github.com/leeoniya/uFuzzy/blob/bba02537334ae9d02440b86262fbfa40d86daa54/demos/compare.html#L264-L288">Typeahead sort</a>, prioritizes start offset and match length<br>
</td>
</tr>
<tr>
<td><code>intraRules</code></td>
<td>Custom matching rules per string</td>
<td><code>(text) => { intraSlice, intraIns, intraSub, intraTrn, intraDel }</code></td>
<td>
<a href="https://github.com/leeoniya/uFuzzy/blob/main/src/uFuzzy.js#L156-L197">Default Rules</a> apply stricter matching for numbers, and terms less than 5 characters
</td>
</tr>
</tbody>
</table>

Expand Down