filter out undefined fields
This commit is contained in:
parent
8eb807714c
commit
556645844d
17
index.js
17
index.js
@ -93,15 +93,14 @@ function recursive_filter (input, filters, children_key = 'children') {
|
||||
data[i] = e;
|
||||
let match = true;
|
||||
for (const filter of filters) {
|
||||
let search_str = '';
|
||||
if (
|
||||
typeof filter.fields !== 'undefined'
|
||||
&& Array.isArray (filter.fields)
|
||||
) {
|
||||
search_str = filter.fields.map ((f) => e[f])
|
||||
.join (' ');
|
||||
}
|
||||
else { search_str = e[filter.field]; }
|
||||
const is_multi_filter
|
||||
= Array.isArray (filter.fields);
|
||||
|
||||
const search_str = is_multi_filter
|
||||
? filter.fields.map ((f) => e[f])
|
||||
.filter ((v) => typeof v !== 'undefined')
|
||||
.join (' ')
|
||||
: e[filter.field];
|
||||
|
||||
if (!filter.filter.test (search_str)) {
|
||||
match = false;
|
||||
|
@ -188,3 +188,28 @@ test ('recursive filter multifield', (t) => {
|
||||
const result = util.recursive_filter (raw, [ filter ]);
|
||||
t.deepEqual (filtered, result);
|
||||
});
|
||||
|
||||
test ('recursive filter multifield input error', (t) => {
|
||||
t.notThrows (() => {
|
||||
util.recursive_filter (
|
||||
[ { foo: 'bar' } ],
|
||||
[ { fields: '', field: 'foo', filter: /a/u } ]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test ('recursive filter undefined multifield', (t) => {
|
||||
const res = util.recursive_filter (
|
||||
[ { foo: 'bar' } ],
|
||||
[
|
||||
{
|
||||
fields: [
|
||||
'foo',
|
||||
'bar'
|
||||
],
|
||||
filter: /\s/u
|
||||
}
|
||||
]
|
||||
);
|
||||
t.deepEqual (res, []);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user