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;
|
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;
|
||||||
|
@ -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, []);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user