allow custom functions in filters
This commit is contained in:
parent
dab2b9a852
commit
0dab4976ae
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
@ -5,7 +5,7 @@ pipeline {
|
||||
VERSION = VersionNumber([
|
||||
versionNumberString:
|
||||
'${BUILDS_ALL_TIME}',
|
||||
versionPrefix: '1.7.',
|
||||
versionPrefix: '1.8.',
|
||||
worstResultForIncrement: 'SUCCESS'
|
||||
])
|
||||
}
|
||||
|
12
README.md
12
README.md
@ -1,6 +1,6 @@
|
||||
# @sapphirecode/utilities
|
||||
|
||||
version: 1.7.x
|
||||
version: 1.8.x
|
||||
|
||||
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
|
||||
|
||||
MIT © Timo Hocker <timo@scode.ovh>
|
||||
|
3
index.js
3
index.js
@ -85,6 +85,9 @@ function get_element_field (element, carried_data, field) {
|
||||
}
|
||||
|
||||
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)
|
||||
? filter.field.map ((f) => get_element_field (e, carried_data, f))
|
||||
.filter ((v) => typeof v !== 'undefined')
|
||||
|
@ -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' } ]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user