1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-12 20:11:06 +01:00

lib/strings: add toCaseWithSeparator

Utility function accepting a separator for converting camelCase settings
This commit is contained in:
Austin Horstman 2025-05-06 21:17:15 -05:00
parent 9cebc4cb8a
commit 94d32062ca

View file

@ -10,7 +10,7 @@ let
upperChars
;
in
{
rec {
# Figures out a valid Nix store name for the given path.
storeFileName =
path:
@ -39,32 +39,27 @@ in
"hm_" + safeName;
/*
Convert a string from camelCase to snake_case
Type: string -> string
Convert a string from camelCase to another case format using a separator
Type: string -> string -> string
*/
toSnakeCase =
toCaseWithSeparator =
separator: string:
let
splitByWords = builtins.split "([A-Z])";
processWord = s: if lib.isString s then s else "_" + lib.toLower (lib.elemAt s 0);
in
string:
let
processWord = s: if lib.isString s then s else separator + lib.toLower (lib.elemAt s 0);
words = splitByWords string;
in
lib.concatStrings (map processWord words);
/*
Convert a string from camelCase to kebab-case
Convert a string from camelCase to snake_case
Type: string -> string
*/
toKebabCase =
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);
toSnakeCase = toCaseWithSeparator "_";
/*
Convert a string from camelCase to kebab-case
Type: string -> string
*/
toKebabCase = toCaseWithSeparator "-";
}