diff --git a/modules/lib/strings.nix b/modules/lib/strings.nix index 0449409c8..2e2179a0c 100644 --- a/modules/lib/strings.nix +++ b/modules/lib/strings.nix @@ -63,4 +63,29 @@ rec { Type: string -> string */ toKebabCase = toCaseWithSeparator "-"; + + # A predicate that returns true only for camelCase. + isCamelCase = + str: + # This regex enforces the entire structure: + # - Must start with one or more lowercase letters. + # - Must be followed by one or more "humps" of an uppercase letter + # and then subsequent lowercase letters/numbers. + builtins.match "^[a-z]+([A-Z][a-z0-9]*)+$" str != null; + + # Returns true for strings like `PascalCase`, `Application`, `URLShortener`. + # Must start with an uppercase letter. + isPascalCase = str: builtins.match "^[A-Z][a-z0-9]*([A-Z][a-z0-9]*)*$" str != null; + + # Returns true for strings like `snake_case`, `a_longer_variable`, `var1`. + # Must be all lowercase letters/numbers, with words separated by single underscores. + isSnakeCase = str: builtins.match "^[a-z0-9]+(_[a-z0-9]+)*$" str != null; + + # Returns true for strings like `kebab-case`, `a-css-class-name`. + # Must be all lowercase letters/numbers, with words separated by single hyphens. + isKebabCase = str: builtins.match "^[a-z0-9]+(-[a-z0-9]+)*$" str != null; + + # Returns true for strings like `SCREAMING_SNAKE_CASE`, `SOME_CONSTANT`. + # Must be all uppercase letters/numbers, with words separated by single underscores. + isScreamingSnakeCase = str: builtins.match "^[A-Z0-9]+(_[A-Z0-9]+)*$" str != null; }