summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYueh-Shun Li <shamrocklee@posteo.net>2024-07-26 03:00:09 +0800
committerYueh-Shun Li <shamrocklee@posteo.net>2024-12-06 11:02:26 +0800
commitb1371135b5db3fcf728114d92d5bd0218109598a (patch)
tree3ad2cb621e27cb52bded4110c0b30ae25023ee80
parentpython3Packages.pywikibot: init at 9.5.0 (#333068) (diff)
downloadnixpkgs-b1371135b5db3fcf728114d92d5bd0218109598a.tar.gz
lib.strings.concatMapAttrsStringSep: init
-rw-r--r--lib/default.nix2
-rw-r--r--lib/strings.nix37
-rw-r--r--lib/tests/misc.nix6
3 files changed, 44 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 5b742b195d34..83ce7a6d7ea9 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -100,7 +100,7 @@ let
length head tail elem elemAt isList;
inherit (self.strings) concatStrings concatMapStrings concatImapStrings
stringLength substring isString replaceStrings
- intersperse concatStringsSep concatMapStringsSep
+ intersperse concatStringsSep concatMapStringsSep concatMapAttrsStringSep
concatImapStringsSep concatLines makeSearchPath makeSearchPathOutput
makeLibraryPath makeIncludePath makeBinPath optionalString
hasInfix hasPrefix hasSuffix stringToCharacters stringAsChars escape
diff --git a/lib/strings.nix b/lib/strings.nix
index d9a2d786ed28..5ea86e0d7e07 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -270,6 +270,43 @@ rec {
list: concatStringsSep sep (lib.imap1 f list);
/**
+ Like [`concatMapStringsSep`](#function-library-lib.strings.concatMapStringsSep)
+ but takes an attribute set instead of a list.
+
+ # Inputs
+
+ `sep`
+ : Separator to add between item strings
+
+ `f`
+ : Function that takes each key and value and return a string
+
+ `attrs`
+ : Attribute set to map from
+
+ # Type
+
+ ```
+ concatMapAttrsStringSep :: String -> (String -> Any -> String) -> AttrSet -> String
+ ```
+
+ # Examples
+
+ :::{.example}
+ ## `lib.strings.concatMapAttrsStringSep` usage example
+
+ ```nix
+ concatMapAttrsStringSep "\n" (name: value: "${name}: foo-${value}") { a = "0.1.0"; b = "0.2.0"; }
+ => "a: foo-0.1.0\nb: foo-0.2.0"
+ ```
+
+ :::
+ */
+ concatMapAttrsStringSep =
+ sep: f: attrs:
+ concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs));
+
+ /**
Concatenate a list of strings, adding a newline at the end of each one.
Defined as `concatMapStrings (s: s + "\n")`.
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 72b3d630b8da..6357cc3ec1cf 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -39,6 +39,7 @@ let
composeManyExtensions
concatLines
concatMapAttrs
+ concatMapAttrsStringSep
concatMapStrings
concatStrings
concatStringsSep
@@ -328,6 +329,11 @@ runTests {
expected = "a,b,c";
};
+ testConcatMapAttrsStringSepExamples = {
+ expr = concatMapAttrsStringSep "\n" (name: value: "${name}: foo-${value}") { a = "0.1.0"; b = "0.2.0"; };
+ expected = "a: foo-0.1.0\nb: foo-0.2.0";
+ };
+
testConcatLines = {
expr = concatLines ["a" "b" "c"];
expected = "a\nb\nc\n";