This returns the tree that results from applying the filter
tree to the source
tree, preserving only keys that exist in filter
and have a truthy value.
Suppose filter.ori
contains:
// filter.ori
filter(
// Source tree
{
a: 1
b: 2
c: {
d: 3
e: 4
}
}
// Filter tree
{
a: true
c: {
d: true
}
}
)
Invoking this returns the filtered result:
$ ori filter.ori/
a: 1
c:
d: 3
If the filter
tree does not specify a value for a given key, the result of asking for that value will be undefined
, which is a falsy value. This means it is not normally necessary to specify false
values in the filter tree.
Above, b
and c/e
have been filtered from the source
tree because those do paths do not lead to truthy values in the filter
tree.
Default value
You can influence the result of a filter operation by defining a default value for the filter tree using a shorthand function or tree:constant.
// filterDefault.ori
filter(
{
a: 1
b: 2
c: 3
d: 4
}
{
...constant(true) // Default is to let values through the filter
b: false
}
)
The above defines a filter where the default value is true
, so all keys and values in the source
tree will come through the filter unless specifically overridden with a falsy value.
$ ori filterDefault.ori/
a: 1
c: 3
d: 4
This flips the logic of filter
: instead of only allowing truthy values in, this filter
excludes falsy values.
Filter with globs and regular expressions
You can use filter
in conjunction with tree:globKeys
and tree:regExpKeys
.