Revert "search index function"
This reverts commit 64f273a6ae63b8050853b83a3ed8e5036be727bf.
This commit is contained in:
parent
7c95a0758f
commit
482cebe73a
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
@ -5,7 +5,7 @@ pipeline {
|
||||
VERSION = VersionNumber([
|
||||
versionNumberString:
|
||||
'${BUILDS_ALL_TIME}',
|
||||
versionPrefix: '1.6.',
|
||||
versionPrefix: '1.5.',
|
||||
worstResultForIncrement: 'SUCCESS'
|
||||
])
|
||||
}
|
||||
|
42
README.md
42
README.md
@ -1,6 +1,6 @@
|
||||
# @sapphirecode/utilities
|
||||
|
||||
version: 1.6.x
|
||||
version: 1.5.x
|
||||
|
||||
small utility functions to make much needed features easier to work with
|
||||
|
||||
@ -118,51 +118,13 @@ const result = util.recursive_filter(
|
||||
[
|
||||
{
|
||||
filter: /foo bar/iu,
|
||||
field: ['name', 'name_2'] // fields will be joined with a space in between
|
||||
fields: ['name', 'name_2'] // fields will be joined with a space in between
|
||||
// {name: 'foo', name_2: 'bar'} will become 'foo bar'
|
||||
}
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
to improve the performance of recursive_filter you can generate a search index before running the function
|
||||
|
||||
```js
|
||||
const to_filter = [
|
||||
{name: 'include_foo'},
|
||||
{
|
||||
name: 'include_bar',
|
||||
children: [{name: 'foo'}, {name: 'bar'}],
|
||||
},
|
||||
{
|
||||
name: 'baz',
|
||||
children: [{name: 'include_foo'}, {name: 'bar'}],
|
||||
},
|
||||
{
|
||||
name: 'barbaz',
|
||||
children: [{name: 'foo'}, {name: 'bar'}],
|
||||
},
|
||||
];
|
||||
|
||||
util.filter_index(
|
||||
to_filter, // object to index
|
||||
'name', // fields to search on (array or string depending on the amount of fields)
|
||||
'children', // field where the children are stored (default: children)
|
||||
'search_index' // field where the index should be stored (default: search_index)
|
||||
)
|
||||
|
||||
const filter = {
|
||||
field: 'search_index', // filter based on the index field instead
|
||||
filter: /^include_.*/iu,
|
||||
};
|
||||
|
||||
const result = util.recursive_filter(
|
||||
to_filter,
|
||||
[filter],
|
||||
'children'
|
||||
);
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT © Timo Hocker <timo@scode.ovh>
|
||||
|
51
index.js
51
index.js
@ -78,20 +78,11 @@ function is_nil (obj) {
|
||||
|| (typeof obj === 'number' && isNaN (obj));
|
||||
}
|
||||
|
||||
function to_search_string (element, field) {
|
||||
return Array.isArray (field)
|
||||
? field.map ((f) => element[f])
|
||||
.filter ((v) => typeof v !== 'undefined')
|
||||
.join (' ')
|
||||
: element[field];
|
||||
}
|
||||
|
||||
/**
|
||||
* filter nested objects
|
||||
*
|
||||
* @param {Array<object>} input
|
||||
* @param {Array<{field: string|string[], filter: RegExp}>} filters
|
||||
* @param {string} children_key field where children are stored
|
||||
* @param {Array<{field: string, filter: RegExp}>} filters
|
||||
* @returns {Array<object>} filtered data
|
||||
*/
|
||||
function recursive_filter (input, filters, children_key = 'children') {
|
||||
@ -102,7 +93,14 @@ function recursive_filter (input, filters, children_key = 'children') {
|
||||
data[i] = e;
|
||||
let match = true;
|
||||
for (const filter of filters) {
|
||||
const search_str = to_search_string (e, filter.field);
|
||||
const is_multi_filter
|
||||
= Array.isArray (filter.fields);
|
||||
|
||||
const search_str = is_multi_filter
|
||||
? filter.fields.map ((f) => e[f])
|
||||
.filter ((v) => typeof v !== 'undefined')
|
||||
.join (' ')
|
||||
: e[filter.field];
|
||||
|
||||
if (!filter.filter.test (search_str)) {
|
||||
match = false;
|
||||
@ -127,40 +125,11 @@ function recursive_filter (input, filters, children_key = 'children') {
|
||||
return filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a search index on an object to speed up the recursive_filter function
|
||||
*
|
||||
* @param {Array<object>} input input object
|
||||
* @param {string|string[]} field field or fields to index
|
||||
* @param {string} children_key field where children are stored
|
||||
* @param {string} index_field field to save index in
|
||||
*/
|
||||
function filter_index (
|
||||
input,
|
||||
field,
|
||||
children_key = 'children',
|
||||
index_field = 'search_index'
|
||||
) {
|
||||
for (const e of input) {
|
||||
let search_str = '';
|
||||
if (Array.isArray (e[children_key])) {
|
||||
filter_index (e[children_key], field, children_key, index_field);
|
||||
search_str += e[children_key]
|
||||
.map ((v) => v[index_field])
|
||||
.filter ((v) => typeof v !== 'undefined')
|
||||
.join (' ');
|
||||
}
|
||||
search_str += to_search_string (e, field);
|
||||
e[index_field] = search_str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
truncate_decimal,
|
||||
try_parse_json,
|
||||
copy_object,
|
||||
run_regex,
|
||||
is_nil,
|
||||
recursive_filter,
|
||||
filter_index
|
||||
recursive_filter
|
||||
};
|
||||
|
@ -179,7 +179,7 @@ test ('recursive filter multifield', (t) => {
|
||||
];
|
||||
const filtered = [ { name: 'foo', f: 'include' } ];
|
||||
const filter = {
|
||||
field: [
|
||||
fields: [
|
||||
'name',
|
||||
'f'
|
||||
],
|
||||
@ -189,6 +189,15 @@ test ('recursive filter multifield', (t) => {
|
||||
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' } ],
|
||||
@ -204,50 +213,3 @@ test ('recursive filter undefined multifield', (t) => {
|
||||
);
|
||||
t.deepEqual (res, []);
|
||||
});
|
||||
|
||||
test ('recursive filter multifield index', (t) => {
|
||||
const raw = [
|
||||
{ name: 'foo', f: 'include' },
|
||||
{
|
||||
name: 'include_bar',
|
||||
children: [
|
||||
{ name: 'foo' },
|
||||
{ name: 'bar' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'baz',
|
||||
children: [
|
||||
{ name: 'include_foo' },
|
||||
{ name: 'bar' }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'barbaz',
|
||||
children: [
|
||||
{ name: 'foo' },
|
||||
{ name: 'bar' }
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
const filtered = [
|
||||
{
|
||||
name: 'foo',
|
||||
f: 'include',
|
||||
search_index: 'foo include'
|
||||
}
|
||||
];
|
||||
|
||||
util.filter_index (raw, [
|
||||
'name',
|
||||
'f'
|
||||
]);
|
||||
|
||||
const filter = {
|
||||
field: 'search_index',
|
||||
filter: /foo include/ui
|
||||
};
|
||||
const result = util.recursive_filter (raw, [ filter ]);
|
||||
t.deepEqual (filtered, result);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user