From 89cac08ac76cffa6398a9d475edebdeae88dd275 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Wed, 31 Jan 2024 09:52:00 +0100 Subject: [PATCH] helpers: add toSnakeCase converter --- lib/utils.nix | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/utils.nix b/lib/utils.nix index 7777bc8e..b8c528df 100644 --- a/lib/utils.nix +++ b/lib/utils.nix @@ -6,6 +6,22 @@ with lib; { emptyTable = {"__empty" = null;}; + /* + Convert a string from camelCase to snake_case + Type: string -> string + */ + toSnakeCase = let + splitByWords = builtins.split "([A-Z])"; + processWord = s: + if isString s + then s + else "_" + toLower (elemAt s 0); + in + string: let + words = splitByWords string; + in + concatStrings (map processWord words); + mkIfNonNull' = x: y: (mkIf (x != null) y); mkIfNonNull = x: (mkIfNonNull' x x);