lib.strings: init toSentenceCase

This commit is contained in:
isabel 2025-02-13 15:28:35 +00:00
parent ddd8141b21
commit b109863c25
3 changed files with 46 additions and 1 deletions

View file

@ -1424,6 +1424,42 @@ rec {
*/
toUpper = replaceStrings lowerChars upperChars;
/**
Converts the first character of a string `s` to upper-case.
# Inputs
`str`
: The string to convert to sentence case.
# Type
```
toSentenceCase :: string -> string
```
# Examples
:::{.example}
## `lib.strings.toSentenceCase` usage example
```nix
toSentenceCase "home"
=> "Home"
```
:::
*/
toSentenceCase = str:
lib.throwIfNot (isString str)
"toSentenceCase does only accepts string values, but got ${typeOf str}"
(
let
firstChar = substring 0 1 str;
rest = substring 1 (stringLength str) str;
in
addContextFrom str (toUpper firstChar + toLower rest)
);
/**
Appends string context from string like object `src` to `target`.