fuzzy-array-filter
Advanced tools
Comparing version 0.0.1-b to 0.1.0
14
index.js
var Fuse = require('fuse.js'); | ||
var fuzzyFilter = function(value, options = {}) { | ||
var fuzzyFilter = function(value, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
var defaultOptions = { | ||
@@ -20,2 +23,11 @@ threshold: 0.4, | ||
} | ||
if (typeof val === 'object') { | ||
var idValues = val; | ||
options.id.split('.').forEach(key => idValues = idValues[key]) | ||
if (Array.isArray(idValues)) { | ||
var temp = idValues.reduce((prev, idValue) => (result.indexOf(idValue) !== -1 || prev), false) | ||
return temp; | ||
} | ||
return result.indexOf(idValues) !== -1; | ||
} | ||
return result.indexOf(key) !== -1; | ||
@@ -22,0 +34,0 @@ }; |
{ | ||
"name": "fuzzy-array-filter", | ||
"version": "0.0.1b", | ||
"description": "A simple array fuzzy filtering", | ||
"version": "0.1.0", | ||
"description": "A simple fuzzy array prototype filter", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
109
README.md
@@ -5,7 +5,9 @@ # fuzzy-array-filter | ||
## How to use it | ||
## How to use it : Simple | ||
### Simple | ||
### Basics | ||
```javascript | ||
use this wrapper with simple array `[val1, val2, ...]` il really (really) easy : | ||
```JavaScript | ||
ìmport fuzzyFilter from 'fuzzy-array-filter'; | ||
@@ -48,3 +50,3 @@ | ||
```javascript | ||
```JavaScript | ||
ìmport fuzzyFilter from 'fuzzy-array-filter'; | ||
@@ -69,2 +71,99 @@ | ||
## How to use it : More complex | ||
### Basics | ||
When you use this wrapper with more complex data, make sure you have 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 | ||
```JavaScript | ||
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 | ||
```JavaScript | ||
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 | ||
```JavaScript | ||
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 | ||
@@ -76,2 +175,2 @@ | ||
* It must filter more complex arrays | ||
* An idea ? |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
17291
31
172
0