diff --git a/lib/utils.nix b/lib/utils.nix index 96c48322..4ca4a4b3 100644 --- a/lib/utils.nix +++ b/lib/utils.nix @@ -67,6 +67,31 @@ with lib; in 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: (mkIfNonNull' x x); diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index 8450d591..b8bbbf8b 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -113,6 +113,23 @@ let }; expected = ''{["c"] = { },["d"] = {["g"] = { }}}''; }; + + testUpperFirstChar = { + expr = map helpers.upperFirstChar [ + "foo" + " foo" + "foo bar" + "UPPER" + "mIxEd" + ]; + expected = [ + "Foo" + " foo" + "Foo bar" + "UPPER" + "MIxEd" + ]; + }; }; in if results == [ ] then