saif-utilities / string_utilities / casing¶
A Terraform sub-module that converts strings between different casing formats (Pascal, Camel, Kebab, Snake). This module intelligently handles input strings in any existing casing format and converts them to all available casing types.
Features¶
- Universal Input Support: Handles strings in any format (spaces, kebab-case, snake_case, camelCase, PascalCase, mixed formats)
- Multiple Output Formats: Converts to Pascal, Camel, Kebab, and Snake casing
- Smart Word Detection: Advanced regex patterns to properly split words from any input format
Implementation¶
The module uses enhanced regex patterns and string manipulation to:
- Parse Input: Splits strings into individual words regardless of input format
- Word Detection: Uses multiple regex patterns to handle camelCase, PascalCase, and special characters
- Casing Conversion: Applies appropriate capitalization and joining logic for each casing type
Key Components¶
input_words: Splits the input string into individual wordscapitalize_word: Helper function to properly capitalize individual wordsconvert_to_casing: Converts words to each specific casing format
Usage¶
Single String → All Casings¶
Convert one string to all available casing formats:
module "resource_naming" {
source = "./modules/string_utilities/casing"
input_string = "my azure storage account"
}
output "all_formats" {
value = module.resource_naming.all_casings
}
# Output:
# {
# Pascal = "MyAzureStorageAccount"
# Camel = "myAzureStorageAccount"
# Kebab = "my-azure-storage-account"
# Snake = "my_azure_storage_account"
# }
Input Format Examples¶
The module handles any of these input formats correctly:
| Input Format | Example | Description |
|---|---|---|
| Spaces | "hello world example" |
Words separated by spaces |
| Kebab-case | "hello-world-example" |
Words separated by hyphens |
| Snake_case | "hello_world_example" |
Words separated by underscores |
| camelCase | "helloWorldExample" |
First word lowercase, others capitalized |
| PascalCase | "HelloWorldExample" |
All words capitalized |
| Mixed | "hello-World_Example test" |
Combination of formats |
| Special chars | "hello.world+example/test" |
Contains dots, plus, slashes |
All of these inputs will produce the same output in all_casings.
Real-World Azure Example¶
module "azure_naming" {
source = "./modules/string_utilities/casing"
input_string = "MyCompanyDataPlatform"
}
locals {
# Use different casings for different Azure resources
storage_account = "${module.azure_naming.all_casings["Kebab"]}sa" # "my-company-data-platformsa"
resource_group = "rg-${module.azure_naming.all_casings["Kebab"]}" # "rg-my-company-data-platform"
key_vault = "kv-${module.azure_naming.all_casings["Kebab"]}" # "kv-my-company-data-platform"
app_service = "app-${module.azure_naming.all_casings["Kebab"]}" # "app-my-company-data-platform"
# Environment variables
database_connection = "${upper(module.azure_naming.all_casings["Snake"])}_DB" # "MY_COMPANY_DATA_PLATFORM_DB"
# Code/configuration
class_name = "${module.azure_naming.all_casings["Pascal"]}Service" # "MyCompanyDataPlatformService"
variable_name = module.azure_naming.all_casings["Camel"] # "myCompanyDataPlatform"
}
Variables¶
| Variable | Type | Description | Required |
|---|---|---|---|
input_string |
string |
Input string to convert to all casings | Yes |
Outputs¶
| Output | Description |
|---|---|
all_casings |
Input string converted to all 4 casing types (Pascal, Camel, Kebab, Snake) |
Supported Casing Types¶
- Pascal:
HelloWorldExample- Each word capitalized, no separators - Camel:
helloWorldExample- First word lowercase, others capitalized, no separators - Kebab:
hello-world-example- All lowercase, hyphen separators - Snake:
hello_world_example- All lowercase, underscore separators