This lib provides a fuzzy search. It's inspired from the work of Matt York on the repository fuzzy
npm install @nexucis/fuzzy
- Filter a simple list of string
import Fuzzy from '@nexucis/fuzzy'
const fuz = new Fuzzy()
const list = ['lion', 'goat', 'mouse', 'dragon']
console.log(fuz.filter('li', list))
// [
// {rendered: 'lion', index: 0, score: 4, original: 'lion'},
// ]
//
- Wrap matching characters in each string for highlighting
import Fuzzy from '@nexucis/fuzzy'
const fuz = new Fuzzy({pre:'<b>', post:'</b>'})
const list = ['lion', 'goat', 'mouse', 'dragon']
console.log(fuz.filter('li', list))
// [
// {rendered: '<b>li</b>on', index: 0, score: 4, original: 'lion'},
// ]
//
- Include the list of indices of the matched characters to make your own highlight
import Fuzzy from '@nexucis/fuzzy'
const fuz = new Fuzzy({includeMatches: true})
const list = ['lion', 'goat', 'mouse', 'dragon']
console.log(fuz.filter('li', list))
// [
// {rendered: 'lion', index: 0, score: 4, original: 'lion', intervales:[{from:0, to:2}]},
// ]
//
- Override locally the global configuration
import Fuzzy from '@nexucis/fuzzy'
const fuz = new Fuzzy({includeMatches: true})
const list = ['lion', 'goat', 'mouse', 'dragon']
console.log(fuz.filter('li', list), {includeMatches: false})
// [
// {rendered: 'lion', index: 0, score: 4, original: 'lion'},
// ]
//
Note: each option can be passed to the constructor or/and in each method exposed. The options passed in the method take precedence over the one passed in the contructor.
- Type:
boolean
- Default:
false
Indicates whether comparisons should be case-sensitive.
- Type:
array of string
- Default:
[]
List of characters that should be ignored in the pattern or in the word used for matching
- Type:
boolean
- Default:
false
Whether the matches should be included in the result. When true, each record in the result set will include the indices of the matched characters. These can consequently be used for highlighting purposes.
- Type:
boolean
- Default:
false
Whether the result should be sorted
- Type:
boolean
- Default:
true
If true, then the strings matched will be automatically rendered using the config pre/post and escapeHTML.
In case you want to render it yourself, set it to false, and set includeMatches
to true.
You will need the intervals to call the method render.
- Type:
boolean
- Default:
false
Whether the filtering should escape the HTML characters that can be found in each record in the result
- Type:
string
- Default:
''
Should be used to prefix each matched characters. Can be useful for the highlighting.
- Type:
string
- Default:
''
Should be used to suffix each matched characters. Can be useful for the highlighting.
Any contribution or suggestion would be really appreciated. Feel free to file an issue or send a pull request.