lib/versions: add pad
Pad a version string with zeros to match a given number of components.
This commit is contained in:
parent
3ff39f984f
commit
0355479715
2 changed files with 30 additions and 0 deletions
|
@ -212,6 +212,21 @@ runTests {
|
|||
expected = [ "1" "2" "3" ];
|
||||
};
|
||||
|
||||
testPadVersionLess = {
|
||||
expr = versions.pad 3 "1.2";
|
||||
expected = "1.2.0";
|
||||
};
|
||||
|
||||
testPadVersionLessExtra = {
|
||||
expr = versions.pad 3 "1.3-rc1";
|
||||
expected = "1.3.0-rc1";
|
||||
};
|
||||
|
||||
testPadVersionMore = {
|
||||
expr = versions.pad 3 "1.2.3.4";
|
||||
expected = "1.2.3";
|
||||
};
|
||||
|
||||
testIsStorePath = {
|
||||
expr =
|
||||
let goodPath =
|
||||
|
|
|
@ -46,4 +46,19 @@ rec {
|
|||
builtins.concatStringsSep "."
|
||||
(lib.take 2 (splitVersion v));
|
||||
|
||||
/* Pad a version string with zeros to match the given number of components.
|
||||
|
||||
Example:
|
||||
pad 3 "1.2"
|
||||
=> "1.2.0"
|
||||
pad 3 "1.3-rc1"
|
||||
=> "1.3.0-rc1"
|
||||
pad 3 "1.2.3.4"
|
||||
=> "1.2.3"
|
||||
*/
|
||||
pad = n: version: let
|
||||
numericVersion = lib.head (lib.splitString "-" version);
|
||||
versionSuffix = lib.removePrefix numericVersion version;
|
||||
in lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n)) + versionSuffix;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue