Revert "fix indexing"
This reverts commit 01be6cede85cf1dc6ab81211adad82283d3d3392.
This commit is contained in:
parent
01be6cede8
commit
7c95a0758f
@ -159,8 +159,7 @@ 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
|
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
23
index.js
23
index.js
@ -92,15 +92,9 @@ 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 (
|
function recursive_filter (input, filters, children_key = 'children') {
|
||||||
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++) {
|
||||||
@ -115,20 +109,16 @@ function recursive_filter (
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (match && !indexed) {
|
if (match) {
|
||||||
filtered.push (e);
|
filtered.push (e);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!Array.isArray (e[children_key])) {
|
if (typeof e[children_key] === 'undefined')
|
||||||
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);
|
||||||
@ -160,10 +150,7 @@ function filter_index (
|
|||||||
.filter ((v) => typeof v !== 'undefined')
|
.filter ((v) => typeof v !== 'undefined')
|
||||||
.join (' ');
|
.join (' ');
|
||||||
}
|
}
|
||||||
const self_str = to_search_string (e, field);
|
search_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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,6 @@ 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' },
|
||||||
@ -219,7 +218,7 @@ test ('recursive filter multifield index', (t) => {
|
|||||||
{
|
{
|
||||||
name: 'baz',
|
name: 'baz',
|
||||||
children: [
|
children: [
|
||||||
{ name: 'include_foo', f: 'include' },
|
{ name: 'include_foo' },
|
||||||
{ name: 'bar' }
|
{ name: 'bar' }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -237,19 +236,7 @@ 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, [
|
||||||
@ -261,6 +248,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 ], 'children', true);
|
const result = util.recursive_filter (raw, [ filter ]);
|
||||||
t.deepEqual (filtered, result);
|
t.deepEqual (filtered, result);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user