-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (31 loc) · 861 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Fuse from 'fuse.js';
export default (value = null, options = {}) => {
if (!value) {
return () => true;
}
const defaultOptions = {
distance: 100,
keys: [],
location: 0,
maxPatternLength: 32,
threshold: 0.4,
};
let fuse = null;
let result = null;
return (val, key, array) => {
if (!fuse || !result) {
fuse = new Fuse(array, Object.assign(defaultOptions, options))
result = fuse.search(value);
}
if (typeof val === 'object') {
let idValues = val;
options.id.split('.').forEach(key => (idValues = idValues[key]))
if (Array.isArray(idValues)) {
let temp = idValues.reduce((prev, idValue) => (result.indexOf(idValue) !== -1 || prev), false)
return temp;
}
return result.indexOf(idValues) !== -1;
}
return result.indexOf(key) !== -1;
};
};