fix indexing

This commit is contained in:
Timo Hocker 2020-06-29 13:41:07 +02:00
parent 64f273a6ae
commit 01be6cede8
3 changed files with 35 additions and 8 deletions

View File

@ -159,7 +159,8 @@ const filter = {
const result = util.recursive_filter( const result = util.recursive_filter(
to_filter, to_filter,
[filter], [filter],
'children' 'children',
true // let the function know, that it is running on an indexed object
); );
``` ```

View File

@ -92,9 +92,15 @@ function to_search_string (element, field) {
* @param {Array<object>} input * @param {Array<object>} input
* @param {Array<{field: string|string[], filter: RegExp}>} filters * @param {Array<{field: string|string[], filter: RegExp}>} filters
* @param {string} children_key field where children are stored * @param {string} children_key field where children are stored
* @param {boolean} indexed set to true if the object was indexed
* @returns {Array<object>} filtered data * @returns {Array<object>} filtered data
*/ */
function recursive_filter (input, filters, children_key = 'children') { function recursive_filter (
input,
filters,
children_key = 'children',
indexed = false
) {
const data = [ ...input ]; const data = [ ...input ];
const filtered = []; const filtered = [];
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
@ -109,16 +115,20 @@ function recursive_filter (input, filters, children_key = 'children') {
break; break;
} }
} }
if (match) { if (match && !indexed) {
filtered.push (e); filtered.push (e);
} }
else { else {
if (typeof e[children_key] === 'undefined') if (!Array.isArray (e[children_key])) {
if (match && indexed)
filtered.push (e);
continue; continue;
}
e[children_key] = recursive_filter ( e[children_key] = recursive_filter (
e[children_key], e[children_key],
filters, filters,
children_key children_key,
indexed
); );
if (e[children_key].length > 0) if (e[children_key].length > 0)
filtered.push (e); filtered.push (e);
@ -150,7 +160,10 @@ function filter_index (
.filter ((v) => typeof v !== 'undefined') .filter ((v) => typeof v !== 'undefined')
.join (' '); .join (' ');
} }
search_str += to_search_string (e, field); const self_str = to_search_string (e, field);
if (search_str.length > 0 && self_str.length > 0)
search_str += ' ';
search_str += self_str;
e[index_field] = search_str; e[index_field] = search_str;
} }
} }

View File

@ -205,6 +205,7 @@ test ('recursive filter undefined multifield', (t) => {
t.deepEqual (res, []); t.deepEqual (res, []);
}); });
// eslint-disable-next-line max-lines-per-function
test ('recursive filter multifield index', (t) => { test ('recursive filter multifield index', (t) => {
const raw = [ const raw = [
{ name: 'foo', f: 'include' }, { name: 'foo', f: 'include' },
@ -218,7 +219,7 @@ test ('recursive filter multifield index', (t) => {
{ {
name: 'baz', name: 'baz',
children: [ children: [
{ name: 'include_foo' }, { name: 'include_foo', f: 'include' },
{ name: 'bar' } { name: 'bar' }
] ]
}, },
@ -236,7 +237,19 @@ test ('recursive filter multifield index', (t) => {
name: 'foo', name: 'foo',
f: 'include', f: 'include',
search_index: 'foo include' search_index: 'foo include'
},
{
name: 'baz',
children: [
{
name: 'include_foo',
f: 'include',
search_index: 'include_foo include'
} }
],
search_index: 'include_foo include bar baz'
}
]; ];
util.filter_index (raw, [ util.filter_index (raw, [
@ -248,6 +261,6 @@ test ('recursive filter multifield index', (t) => {
field: 'search_index', field: 'search_index',
filter: /foo include/ui filter: /foo include/ui
}; };
const result = util.recursive_filter (raw, [ filter ]); const result = util.recursive_filter (raw, [ filter ], 'children', true);
t.deepEqual (filtered, result); t.deepEqual (filtered, result);
}); });