lib/strings: add toSnakeCase

Utility function for converting camelCase strings to snake_case
This commit is contained in:
Austin Horstman 2025-05-06 21:13:52 -05:00
parent 35535345be
commit e121442b65
2 changed files with 18 additions and 5 deletions

View file

@ -37,4 +37,19 @@ in
safeName = replaceStrings unsafeInName (empties unsafeInName) path;
in
"hm_" + safeName;
/*
Convert a string from camelCase to snake_case
Type: string -> string
*/
toSnakeCase =
let
splitByWords = builtins.split "([A-Z])";
processWord = s: if lib.isString s then s else "_" + lib.toLower (lib.elemAt s 0);
in
string:
let
words = splitByWords string;
in
lib.concatStrings (map processWord words);
}