mirror of
https://github.com/NixOS/nixpkgs.git
synced 2026-07-06 17:13:24 -05:00
lib.strings: remove throwIfNot usage
throwIfNot sends our error message through a function call, even if the error condition doesn't trigger. This requires a lot of thunk allocation that can be easily avoided.
This commit is contained in:
parent
bb50241c60
commit
ed29e1b031
1 changed files with 12 additions and 12 deletions
|
|
@ -1589,15 +1589,14 @@ rec {
|
|||
*/
|
||||
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 (-1) str; # -1 takes till the end of the string
|
||||
in
|
||||
toUpper firstChar + toLower rest
|
||||
);
|
||||
if !isString str then
|
||||
throw "toSentenceCase does only accepts string values, but got ${typeOf str}"
|
||||
else
|
||||
let
|
||||
firstChar = substring 0 1 str;
|
||||
rest = substring 1 (-1) str; # -1 takes till the end of the string
|
||||
in
|
||||
toUpper firstChar + toLower rest;
|
||||
|
||||
/**
|
||||
Converts a string to camelCase. Handles snake_case, PascalCase,
|
||||
|
|
@ -1633,7 +1632,9 @@ rec {
|
|||
*/
|
||||
toCamelCase =
|
||||
str:
|
||||
lib.throwIfNot (isString str) "toCamelCase does only accepts string values, but got ${typeOf str}" (
|
||||
if !isString str then
|
||||
throw "toCamelCase does only accepts string values, but got ${typeOf str}"
|
||||
else
|
||||
let
|
||||
separators = splitStringBy (
|
||||
prev: curr:
|
||||
|
|
@ -1653,8 +1654,7 @@ rec {
|
|||
first = if length parts > 0 then toLower (head parts) else "";
|
||||
rest = if length parts > 1 then map toSentenceCase (tail parts) else [ ];
|
||||
in
|
||||
concatStrings ([ first ] ++ rest)
|
||||
);
|
||||
concatStrings ([ first ] ++ rest);
|
||||
|
||||
/**
|
||||
Appends string context from string like object `src` to `target`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue