lib: add upperFirstChar

A util function to capitalize the first character of a string.
This commit is contained in:
Matt Sturgeon 2024-03-17 15:40:10 +00:00
parent 36f2e51b28
commit c16533b3f7
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
2 changed files with 42 additions and 0 deletions

View file

@ -67,6 +67,31 @@ with lib;
in in
concatStrings (map processWord words); concatStrings (map processWord words);
/**
Capitalize a string by making the first character uppercase.
# Example
```nix
upperFirstChar "hello, world!"
=> "Hello, world!"
```
# Type
```
upperFirstChar :: String -> String
```
*/
upperFirstChar =
s:
let
first = substring 0 1 s;
rest = substring 1 (stringLength s) s;
result = (toUpper first) + rest;
in
optionalString (s != "") result;
mkIfNonNull' = x: y: (mkIf (x != null) y); mkIfNonNull' = x: y: (mkIf (x != null) y);
mkIfNonNull = x: (mkIfNonNull' x x); mkIfNonNull = x: (mkIfNonNull' x x);

View file

@ -113,6 +113,23 @@ let
}; };
expected = ''{["c"] = { },["d"] = {["g"] = { }}}''; expected = ''{["c"] = { },["d"] = {["g"] = { }}}'';
}; };
testUpperFirstChar = {
expr = map helpers.upperFirstChar [
"foo"
" foo"
"foo bar"
"UPPER"
"mIxEd"
];
expected = [
"Foo"
" foo"
"Foo bar"
"UPPER"
"MIxEd"
];
};
}; };
in in
if results == [ ] then if results == [ ] then