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