From 632e1b1d8c8a1b8ae492ea71dbb24dd2958ec1e1 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 8 Aug 2024 11:19:03 +0800 Subject: [PATCH] lib: implement filterAttrs' Uhh... I forgot that this is a thing. --- lib/trivial.nix | 15 ++++++++++++--- tests/lib/trivial.nix | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index c7b4c38d..eb69b726 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -14,11 +14,20 @@ lib.count (attr: pred attr.name attr.value) (lib.mapAttrsToList lib.nameValuePair attrs); - /* Filters and groups the attribute set into two separate attribute. + /* Filters and groups the attribute set into two separate attribute where it + either accepted or denied from a given predicate function. Example: filterAttrs' (n: v: v == 4) { a = 4; b = 2; c = 6; } - => { ok = { a = 4; }; reject = { b = 2; c = 6; }; } + => { ok = { a = 4; }; notOk = { b = 2; c = 6; }; } */ - filterAttrs' = f: attrs: { }; + filterAttrs' = f: attrs: + lib.foldlAttrs (acc: name: value: let + isOk = f name value; + in { + ok = acc.ok // lib.optionalAttrs isOk { ${name} = value; }; + notOk = acc.notOk // lib.optionalAttrs (!isOk) { ${name} = value; }; + }) + { ok = { }; notOk = { }; } + attrs; } diff --git a/tests/lib/trivial.nix b/tests/lib/trivial.nix index bf62c6c7..ccf8bfdd 100644 --- a/tests/lib/trivial.nix +++ b/tests/lib/trivial.nix @@ -12,4 +12,16 @@ lib.runTests { }; expected = 2; }; + + testFilterAttrs' = { + expr = self.trivial.filterAttrs' (n: v: v == 4) { + e = 5; + f = 7; + a = 4; + }; + expected = { + ok = { a = 4; }; + notOk = { e = 5; f = 7; }; + }; + }; }