diff --git a/README.md b/README.md index df72118..7285303 100644 --- a/README.md +++ b/README.md @@ -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' ); ``` diff --git a/index.js b/index.js index 583080c..21c47d5 100644 --- a/index.js +++ b/index.js @@ -92,15 +92,9 @@ function to_search_string (element, field) { * @param {Array} 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} 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; } } diff --git a/test/index.js b/test/index.js index 2696f72..ba5ae97 100644 --- a/test/index.js +++ b/test/index.js @@ -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); });