From 94d32062ca42d8149cd77d2b3e3ec5c8c4103084 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Tue, 6 May 2025 21:17:15 -0500 Subject: [PATCH] lib/strings: add toCaseWithSeparator Utility function accepting a separator for converting camelCase settings --- modules/lib/strings.nix | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/modules/lib/strings.nix b/modules/lib/strings.nix index eb6086ac6..14b54c86f 100644 --- a/modules/lib/strings.nix +++ b/modules/lib/strings.nix @@ -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 "-"; }