allow custom functions in filters

This commit is contained in:
Timo Hocker 2020-07-03 14:36:39 +02:00
parent dab2b9a852
commit 0dab4976ae
4 changed files with 42 additions and 2 deletions

2
Jenkinsfile vendored
View File

@ -5,7 +5,7 @@ pipeline {
VERSION = VersionNumber([ VERSION = VersionNumber([
versionNumberString: versionNumberString:
'${BUILDS_ALL_TIME}', '${BUILDS_ALL_TIME}',
versionPrefix: '1.7.', versionPrefix: '1.8.',
worstResultForIncrement: 'SUCCESS' worstResultForIncrement: 'SUCCESS'
]) ])
} }

View File

@ -1,6 +1,6 @@
# @sapphirecode/utilities # @sapphirecode/utilities
version: 1.7.x version: 1.8.x
small utility functions to make much needed features easier to work with small utility functions to make much needed features easier to work with
@ -178,6 +178,16 @@ field values can be carried to filter runs on children to filter over the entire
*/ */
``` ```
custom functions to match items can also be used
```js
const filter = {
function: (element) => {
return element.should_match;
}
}
```
## License ## License
MIT © Timo Hocker <timo@scode.ovh> MIT © Timo Hocker <timo@scode.ovh>

View File

@ -85,6 +85,9 @@ function get_element_field (element, carried_data, field) {
} }
function check_filter (filter, e, carried_data) { function check_filter (filter, e, carried_data) {
if (typeof filter.function !== 'undefined')
return filter.function ({ ...e, ...carried_data });
const search_str = Array.isArray (filter.field) const search_str = Array.isArray (filter.field)
? filter.field.map ((f) => get_element_field (e, carried_data, f)) ? filter.field.map ((f) => get_element_field (e, carried_data, f))
.filter ((v) => typeof v !== 'undefined') .filter ((v) => typeof v !== 'undefined')

View File

@ -252,3 +252,30 @@ test ('recursive filter carry field', (t) => {
} }
]); ]);
}); });
test ('recursive filter custom function', (t) => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { function: (elem) => elem.name === 'foo bar' } ],
'children',
[ 'name' ]
);
t.deepEqual (res, [
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});