filter out undefined fields

This commit is contained in:
Timo Hocker 2020-06-29 12:37:28 +02:00
parent 8eb807714c
commit 556645844d
2 changed files with 33 additions and 9 deletions

View File

@ -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;

View File

@ -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, []);
});