lib/strings: add PascalCase support for toCaseWithSeperator (#7282)

This commit is contained in:
Different 2025-06-17 04:49:41 +10:00 committed by GitHub
parent 66523b0efe
commit 1db3cb415d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -39,7 +39,7 @@ rec {
"hm_" + safeName;
/*
Convert a string from camelCase to another case format using a separator
Convert a string from camelCase or PascalCase to another case format using a separator
Type: string -> string -> string
*/
toCaseWithSeparator =
@ -48,17 +48,18 @@ rec {
splitByWords = builtins.split "([A-Z])";
processWord = s: if lib.isString s then s else separator + lib.toLower (lib.elemAt s 0);
words = splitByWords string;
converted = lib.concatStrings (map processWord words);
in
lib.concatStrings (map processWord words);
lib.removePrefix separator converted;
/*
Convert a string from camelCase to snake_case
Convert a string from camelCase or PascalCase to snake_case
Type: string -> string
*/
toSnakeCase = toCaseWithSeparator "_";
/*
Convert a string from camelCase to kebab-case
Convert a string from camelCase or PascalCase to kebab-case
Type: string -> string
*/
toKebabCase = toCaseWithSeparator "-";