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
7 changes: 4 additions & 3 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ rec {

/**
Given a set of attribute names, return the set of the corresponding
attributes from the given set.
attributes from the given set. If the attributes does not exist,
they will be ignored.


# Inputs
Expand All @@ -565,15 +566,15 @@ rec {
## `lib.attrsets.getAttrs` usage example

```nix
getAttrs [ "a" "b" ] { a = 1; b = 2; c = 3; }
getAttrs [ "a" "b" "d" ] { a = 1; b = 2; c = 3; }
=> { a = 1; b = 2; }
```

:::
*/
getAttrs =
names:
attrs: genAttrs names (name: attrs.${name});
attrs: intersectAttrs (genAttrs names (_: null)) attrs;

/**
Collect each attribute named `attr` from a list of attribute
Expand Down
13 changes: 13 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let
functionArgs
generators
genList
getAttrs
getExe
getExe'
getLicenseFromSpdxIdOr
Expand Down Expand Up @@ -2094,6 +2095,18 @@ runTests {
};
};

testGetAttrs = {
expr = getAttrs [ "a" "b" "nonexistent" ] {
a = 1;
b = true;
c = "hello";
};
expected = {
a = 1;
b = true;
};
};

## Levenshtein distance functions and co.
testCommonPrefixLengthEmpty = {
expr = strings.commonPrefixLength "" "hello";
Expand Down