mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
lib/strings: add extra string matching predicates
Centralize logic to avoid needing to rewrite it elsewhere. Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
parent
19f0ba9c52
commit
f62e9a8114
1 changed files with 25 additions and 0 deletions
|
|
@ -63,4 +63,29 @@ rec {
|
||||||
Type: string -> string
|
Type: string -> string
|
||||||
*/
|
*/
|
||||||
toKebabCase = toCaseWithSeparator "-";
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue