lib/strings: add toSnakeCase
Utility function for converting camelCase strings to snake_case
This commit is contained in:
parent
35535345be
commit
e121442b65
2 changed files with 18 additions and 5 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue