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; data[i] = e;
let match = true; let match = true;
for (const filter of filters) { for (const filter of filters) {
let search_str = ''; const is_multi_filter
if ( = Array.isArray (filter.fields);
typeof filter.fields !== 'undefined'
&& Array.isArray (filter.fields) const search_str = is_multi_filter
) { ? filter.fields.map ((f) => e[f])
search_str = filter.fields.map ((f) => e[f]) .filter ((v) => typeof v !== 'undefined')
.join (' '); .join (' ')
} : e[filter.field];
else { search_str = e[filter.field]; }
if (!filter.filter.test (search_str)) { if (!filter.filter.test (search_str)) {
match = false; match = false;

View File

@ -188,3 +188,28 @@ test ('recursive filter multifield', (t) => {
const result = util.recursive_filter (raw, [ filter ]); const result = util.recursive_filter (raw, [ filter ]);
t.deepEqual (filtered, result); 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, []);
});