Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e237fbcb7 |
6 changed files with 294 additions and 0 deletions
|
|
@ -11,6 +11,7 @@ let
|
|||
"lists"
|
||||
"math"
|
||||
"modules"
|
||||
"nixpkgs"
|
||||
"numbers"
|
||||
"options"
|
||||
"packages"
|
||||
|
|
|
|||
|
|
@ -360,4 +360,15 @@ lib: {
|
|||
*/
|
||||
flatten =
|
||||
value: if builtins.isList value then builtins.concatMap lib.lists.flatten value else [ value ];
|
||||
|
||||
/**
|
||||
Creates a list of length `count` where every item is set to `item`.
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
repeat :: Int -> Any -> [Any]
|
||||
```
|
||||
*/
|
||||
repeat = count: item: builtins.genList (i: item) count;
|
||||
}
|
||||
|
|
|
|||
5
src/nixpkgs/default.nix
Normal file
5
src/nixpkgs/default.nix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
lib: {
|
||||
strings = (import ./strings.nix lib);
|
||||
}
|
||||
# TODO: I think this ^ might be wrong, let's study src/default.nix to see if we
|
||||
# need to replicate that
|
||||
4
src/nixpkgs/default.test.nix
Normal file
4
src/nixpkgs/default.test.nix
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
let strings_test = import ./strings.test.nix;
|
||||
in {
|
||||
inherit strings_test;
|
||||
}
|
||||
144
src/nixpkgs/strings.nix
Normal file
144
src/nixpkgs/strings.nix
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
lib: {
|
||||
/**
|
||||
Concatenate a list of strings.
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
concatStrings :: [string] -> string
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.strings.concatStrings` usage example
|
||||
|
||||
```nix
|
||||
concatStrings ["foo" "bar"]
|
||||
=> "foobar"
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
concatStrings = lib.strings.concat;
|
||||
|
||||
/**
|
||||
Map a function over a list and concatenate the resulting strings.
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
: 1\. Function argument
|
||||
|
||||
`list`
|
||||
: 2\. Function argument
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
concatMapStrings :: (a -> string) -> [a] -> string
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.strings.concatMapStrings` usage example
|
||||
|
||||
```nix
|
||||
concatMapStrings (x: "a" + x) ["foo" "bar"]
|
||||
=> "afooabar"
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
concatMapStrings = lib.strings.concatMap;
|
||||
|
||||
/**
|
||||
Like `concatMapStrings` except that the f functions also gets the
|
||||
position as a parameter.
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
: 1\. Function argument
|
||||
|
||||
`list`
|
||||
: 2\. Function argument
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
concatImapStrings :: (int -> a -> string) -> [a] -> string
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.strings.concatImapStrings` usage example
|
||||
|
||||
```nix
|
||||
concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
|
||||
=> "1-foo2-bar"
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
concatImapStrings = f: list: lib.strings.concat (lib.lists.mapWithIndex1 f list);
|
||||
|
||||
/**
|
||||
Place an element between each element of a list
|
||||
|
||||
# Inputs
|
||||
|
||||
`separator`
|
||||
: Separator to add between elements
|
||||
|
||||
`list`
|
||||
: Input list
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
intersperse :: a -> [a] -> [a]
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.strings.intersperse` usage example
|
||||
|
||||
```nix
|
||||
intersperse "/" ["usr" "local" "bin"]
|
||||
=> ["usr" "/" "local" "/" "bin"].
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
intersperse = lib.lists.intersperse;
|
||||
|
||||
/**
|
||||
Concatenate a list of strings with a separator between each element
|
||||
|
||||
# Inputs
|
||||
|
||||
`sep`
|
||||
: Separator to add between elements
|
||||
|
||||
`list`
|
||||
: List of input strings
|
||||
|
||||
# Type
|
||||
|
||||
```
|
||||
concatStringsSep :: string -> [string] -> string
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.strings.concatStringsSep` usage example
|
||||
|
||||
```nix
|
||||
concatStringsSep "/" ["usr" "local" "bin"]
|
||||
=> "usr/local/bin"
|
||||
```
|
||||
|
||||
:::
|
||||
*/
|
||||
concatStringsSep = sep: list: lib.strings.concat (lib.lists.intersperse sep list);
|
||||
}#
|
||||
129
src/nixpkgs/strings.test.nix
Normal file
129
src/nixpkgs/strings.test.nix
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
let
|
||||
aux_lib = import ./../default.nix;
|
||||
nixpkgs_lib_path = (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/25.05.tar.gz") + "/lib";
|
||||
nixpkgs_lib = import nixpkgs_lib_path;
|
||||
aux_nixpkgs_lib = aux_lib.nixpkgs;
|
||||
tests = lib: {
|
||||
"concatStrings" = {
|
||||
"concatenate zero strings" =
|
||||
let
|
||||
expected = "";
|
||||
actual = lib.strings.concatStrings [];
|
||||
in
|
||||
actual == expected;
|
||||
"concatenate one string" =
|
||||
let
|
||||
expected = "foo";
|
||||
actual = lib.strings.concatStrings ["foo"];
|
||||
in
|
||||
actual == expected;
|
||||
"concatenate two strings" =
|
||||
let
|
||||
expected = "foobar";
|
||||
actual = lib.strings.concatStrings ["foo" "bar"];
|
||||
in
|
||||
actual == expected;
|
||||
};
|
||||
"concatMapStrings" = {
|
||||
"idenity map zero strings" =
|
||||
let
|
||||
expected = "";
|
||||
actual = lib.strings.concatMapStrings (x: x) [];
|
||||
in
|
||||
actual == expected;
|
||||
"idenity map one string" =
|
||||
let
|
||||
expected = "foo";
|
||||
actual = lib.strings.concatMapStrings (x: x) ["foo"];
|
||||
in
|
||||
actual == expected;
|
||||
"idenity map two strings" =
|
||||
let
|
||||
expected = "foobar";
|
||||
actual = lib.strings.concatMapStrings (x: x) ["foo" "bar"];
|
||||
in
|
||||
actual == expected;
|
||||
"map zero strings" =
|
||||
let
|
||||
expected = "";
|
||||
actual = lib.strings.concatMapStrings (x: "hi " + x) [];
|
||||
in
|
||||
actual == expected;
|
||||
"map one string" =
|
||||
let
|
||||
expected = "hi foo";
|
||||
actual = lib.strings.concatMapStrings (x: "hi " + x) ["foo"];
|
||||
in
|
||||
actual == expected;
|
||||
"map two strings" =
|
||||
let
|
||||
expected = "hi foohi bar";
|
||||
actual = lib.strings.concatMapStrings (x: "hi " + x) ["foo" "bar"];
|
||||
in
|
||||
actual == expected;
|
||||
};
|
||||
"concatImapStrings" = {
|
||||
"map zero strings" =
|
||||
let
|
||||
expected = "";
|
||||
actual = lib.strings.concatImapStrings (i: x: x) [];
|
||||
in
|
||||
actual == expected;
|
||||
"map one string" =
|
||||
let
|
||||
expected = "1foo";
|
||||
actual = lib.strings.concatImapStrings (i: x: (builtins.toString i) + x) ["foo"];
|
||||
in
|
||||
actual == expected;
|
||||
"map two strings" =
|
||||
let
|
||||
expected = "1foo2bar";
|
||||
actual = lib.strings.concatImapStrings (i: x: (builtins.toString i) + x) ["foo" "bar"];
|
||||
in
|
||||
actual == expected;
|
||||
};
|
||||
"intersperse" = {
|
||||
"zero length list" =
|
||||
let
|
||||
expected = [];
|
||||
actual = lib.strings.intersperse "foo" [];
|
||||
in
|
||||
actual == expected;
|
||||
"one length list" =
|
||||
let
|
||||
expected = ["bar"];
|
||||
actual = lib.strings.intersperse "foo" ["bar"];
|
||||
in
|
||||
actual == expected;
|
||||
"two length list" =
|
||||
let
|
||||
expected = ["bar" "foo" "baz"];
|
||||
actual = lib.strings.intersperse "foo" ["bar" "baz"];
|
||||
in
|
||||
actual == expected;
|
||||
};
|
||||
"concatStringsSep" = {
|
||||
"zero length list" =
|
||||
let
|
||||
expected = "";
|
||||
actual = lib.strings.concatStringsSep "foo" [];
|
||||
in
|
||||
actual == expected;
|
||||
"one length list" =
|
||||
let
|
||||
expected = "bar";
|
||||
actual = lib.strings.concatStringsSep "foo" ["bar"];
|
||||
in
|
||||
actual == expected;
|
||||
"two length list" =
|
||||
let
|
||||
expected = "barfoobaz";
|
||||
actual = lib.strings.concatStringsSep "foo" ["bar" "baz"];
|
||||
in
|
||||
actual == expected;
|
||||
};
|
||||
};
|
||||
in {
|
||||
"nixpkgs_tests" = (tests nixpkgs_lib);
|
||||
"aux_nixkpgs_tests" = (tests aux_nixpkgs_lib);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue