Revert "fix indexing"

This reverts commit 01be6cede85cf1dc6ab81211adad82283d3d3392.
This commit is contained in:
Timo Hocker 2020-06-29 13:57:49 +02:00
parent 01be6cede8
commit 7c95a0758f
3 changed files with 8 additions and 35 deletions

View File

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

View File

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

View File

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