1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00
home-manager/modules/lib/strings.nix
Austin Horstman 94d32062ca lib/strings: add toCaseWithSeparator
Utility function accepting a separator for converting camelCase settings
2025-05-07 10:02:56 -05:00

65 lines
1.5 KiB
Nix

{ lib }:
let
inherit (lib)
genList
length
lowerChars
replaceStrings
stringToCharacters
upperChars
;
in
rec {
# Figures out a valid Nix store name for the given path.
storeFileName =
path:
let
# All characters that are considered safe. Note "-" is not
# included to avoid "-" followed by digit being interpreted as a
# version.
safeChars =
[
"+"
"."
"_"
"?"
"="
]
++ lowerChars
++ upperChars
++ stringToCharacters "0123456789";
empties = l: genList (x: "") (length l);
unsafeInName = stringToCharacters (replaceStrings safeChars (empties safeChars) path);
safeName = replaceStrings unsafeInName (empties unsafeInName) path;
in
"hm_" + safeName;
/*
Convert a string from camelCase to another case format using a separator
Type: string -> string -> string
*/
toCaseWithSeparator =
separator: string:
let
splitByWords = builtins.split "([A-Z])";
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 snake_case
Type: string -> string
*/
toSnakeCase = toCaseWithSeparator "_";
/*
Convert a string from camelCase to kebab-case
Type: string -> string
*/
toKebabCase = toCaseWithSeparator "-";
}