fuzzy-array-filter
A simple fuzzy filter function for array.prototype.filter
using fuse.js.
Install
npm i -S fuzzy-array-filter
How to use it : Simple data structure
Basics
use this wrapper with simple array [val1, val2, ...]
il is really (really) easy :
ìmport fuzzyFilter from 'fuzzy-array-filter';
const array = ['There is a house in New Orleans',
'They call the Rising Sun',
'And \'s been the ruin of many a poor boy',
'And God, I know I\'m one',
'My mother was a tailor',
'She sewed my new blue jeans',
'My father was a gamblin man',
'Down in New Orleans',
'Now the only thing a gambler needs',
'Is a suitcase and trunk',
'And the only time he\'s satisfied',
'Is when he\'s on a drunk',
'Oh mother, tell your children',
'Not to do what I have done',
'Spend your lives in sin and misery',
'In the House of the Rising Sun' ];
console.log(array.filter(fuzzySearch('new orleans')));
Default fuzzy options are
{
"threshold": 0.4,
"location": 0,
"distance": 100,
"maxPatternLength": 32,
"keys": []
}
With custom options
ìmport fuzzyFilter from 'fuzzy-array-filter';
const array = theRisingSunArray;
const options = {
caseSensitive: true,
};
console.log(array.filter(fuzzySearch('new orleans', options)));
console.log(array.filter(fuzzySearch('man', options)));
For more details, please see the fuse.js options documentation
How to use it : More complex data structures
Basics
When you use this wrapper with more complex data structure, make sure you have provided some custom options.
You MUST fill the id
and the keys
options AND id
MUST be one of the keys
values.
In most of use cases keys = [id, [whatever]]
.
Simple complex
const tab = [
{names: 'Edwina' },
{names: 'Augusta' },
{names: 'Lina' },
{names: 'Ware' },
{names: 'Kim' },
{names: 'Nita' },
{names: 'Garrett' },
{names: 'Concepcion' },
{names: 'Laverne' },
{names: 'Alford' },
{names: 'Jill' },
{names: 'Reed' },
{names: 'Shaw' },
];
const options = {
keys: ['names'],
id: 'names'
}
const filteredTab = tab.filter(fuzzyFilter('in', options))
console.log(filteredTab)
Complex
const tab2 = [
{names: { first: 'Edwina' } },
{names: { first: 'Augusta' } },
{names: { first: 'Lina' } },
{names: { first: 'Ware' } },
{names: { first: 'Kim' } },
{names: { first: 'Nita' } },
{names: { first: 'Garrett' } },
{names: { first: 'Concepcion' } },
{names: { first: 'Laverne' } },
{names: { first: 'Alford' } },
{names: { first: 'Jill' } },
{names: { first: 'Reed' } },
{names: { first: 'Shaw' } },
];
const options2 = {
keys: ['names.first'],
id: 'names.first'
}
const filteredTab2 = tab2.filter(fuzzyFilter('in', options2))
console.log(filteredTab2)
More complex
const tab3 = [
{names: { first: ['Edwina'] } },
{names: { first: ['Augusta'] } },
{names: { first: ['Lina'] } },
{names: { first: ['Ware'] } },
{names: { first: ['Kim'] } },
{names: { first: ['Nita'] } },
{names: { first: ['Garrett'] } },
{names: { first: ['Concepcion'] } },
{names: { first: ['Laverne'] } },
{names: { first: ['Alford'] } },
{names: { first: ['Jill'] } },
{names: { first: ['Reed'] } },
{names: { first: ['Shaw'] } },
];
const options3 = {
keys: ['names.first'],
id: 'names.first'
}
const filteredTab3 = tab3.filter(fuzzyFilter('in', options3))
console.log(filteredTab2)
Dependencies
ToDo