Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,33 @@ rec {

:::
*/
filterAttrsRecursive =
pred: set:
filterAttrsRecursive = filterAttrsRecursiveCond (_: true);

/**
Like `filterAttrsRecursive', but it takes an additional predicate that tells it whether to recurse into an attribute set.
If the predicate returns false, `filterAttrsRecursiveCond' does not recurse, but instead applies the filtering function.
If the predicate returns true, it does recurse, and does not apply the filtering function.

:::{#filter-attrs-recursive-cond-example .example}
# Filter over an leaf attributes defined by a condition

Filter derivations according to their `name` attribute.
Derivations are identified as attribute sets that contain `{ type = "derivation"; }`.
```nix
filterAttrsRecursiveCond
(as: !(as ? "type" && as.type == "derivation"))
(x: x.name != "foo")
attrs
```
:::

# Type:
```
filterAttrsRecursiveCond :: (AttrSet -> Bool) -> (String -> a -> Bool) -> AttrSet -> AttrSet
```
*/
filterAttrsRecursiveCond =
cond: pred: set:
listToAttrs (
concatMap (
name:
Expand All @@ -707,7 +732,7 @@ rec {
in
if pred name v then
[
(nameValuePair name (if isAttrs v then filterAttrsRecursive pred v else v))
(nameValuePair name (if isAttrs v && cond v then filterAttrsRecursiveCond cond pred v else v))
]
else
[ ]
Expand Down
1 change: 1 addition & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ let
catAttrs
filterAttrs
filterAttrsRecursive
filterAttrsRecursiveCond
foldlAttrs
foldAttrs
collect
Expand Down
Loading