mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-12-19 04:03:56 +00:00
lib.strings.concatMapAttrsStringSep: init (#330010)
This commit is contained in:
commit
2936a2ab9b
@ -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
|
||||
|
@ -269,6 +269,43 @@ rec {
|
||||
f:
|
||||
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")`.
|
||||
|
@ -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";
|
||||
|
@ -102,10 +102,6 @@ in
|
||||
}@args:
|
||||
|
||||
let
|
||||
concatMapStringAttrsSep =
|
||||
sep: f: attrs:
|
||||
lib.concatMapStringsSep sep (name: f name attrs.${name}) (lib.attrNames attrs);
|
||||
|
||||
addShellDoubleQuotes = s: lib.escapeShellArg ''"'' + s + lib.escapeShellArg ''"'';
|
||||
in
|
||||
(buildGoModule {
|
||||
@ -210,7 +206,7 @@ in
|
||||
patchShebangs --build "$configureScript" makeit e2e scripts mlocal/scripts
|
||||
|
||||
# Patching the hard-coded defaultPath by prefixing the packages in defaultPathInputs
|
||||
${concatMapStringAttrsSep "\n" (fileName: originalDefaultPaths: ''
|
||||
${lib.concatMapAttrsStringSep "\n" (fileName: originalDefaultPaths: ''
|
||||
substituteInPlace ${lib.escapeShellArg fileName} \
|
||||
${
|
||||
lib.concatMapStringsSep " \\\n " (
|
||||
|
@ -158,5 +158,5 @@ stdenvNoCC.mkDerivation {
|
||||
passthru = { inherit tests; };
|
||||
buildCommand = ''
|
||||
touch $out
|
||||
'' + lib.concatStringsSep "\n" (lib.attrValues (lib.mapAttrs (name: t: "([[ ${lib.boolToString t.expr} == ${lib.boolToString t.expected} ]] && echo '${name} success') || (echo '${name} fail' && exit 1)") tests));
|
||||
'' + lib.concatMapAttrsStringSep "\n" (name: t: "([[ ${lib.boolToString t.expr} == ${lib.boolToString t.expected} ]] && echo '${name} success') || (echo '${name} fail' && exit 1)") tests;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user