A comprehensive MoonBit library for converting between different case styles including camelCase, snake_case, kebab-case, PascalCase, SHOUTY_SNAKE_CASE, Title Case, and more. Features bidirectional conversion, type-safe enums, and zero dependencies.
| Style | Example | Description |
|---|---|---|
| UpperCamel | HelloWorldTest | PascalCase - each word capitalized |
| LowerCamel | helloWorldTest | camelCase - first word lowercase, others capitalized |
| Snake | hello_world_test | snake_case - words separated by underscores |
| Kebab | hello-world-test | kebab-case - words separated by hyphens |
| ShoutySnake | HELLO_WORLD_TEST | SHOUTY_SNAKE_CASE - uppercase with underscores |
| Sentence | Hello world test | Sentence case - first word capitalized |
| Title | Hello World Test | Title Case - each word capitalized |
| ShoutyKebab | HELLO-WORLD-TEST | SHOUTY-KEBAB-CASE - uppercase with hyphens |
///|
test "basic conversion examples" {
// Parse from camelCase and convert to snake_case
let words = @case_conv.Words::from_lower_camel("helloWorldTest")
let snake_result = words.into_snake()
inspect(snake_result, content="hello_world_test")
// Parse from kebab-case and convert to PascalCase
let words2 = @case_conv.Words::from_kebab("hello-world-test")
let pascal_result = words2.into_upper_camel()
inspect(pascal_result, content="HelloWorldTest")
// Parse from snake_case and convert to Title Case
let words3 = @case_conv.Words::from_snake("my_awesome_function")
let title_result = words3.into_title()
inspect(title_result, content="My Awesome Function")
}///|
test "generic conversion with CaseStyle enum" {
let words = @case_conv.Words::from_lower_camel("getUserById")
// Convert to different styles using the generic method
let snake = words.into(@case_conv.CaseStyle::Snake)
inspect(snake, content="get_user_by_id")
let kebab = words.into(@case_conv.CaseStyle::Kebab)
inspect(kebab, content="get-user-by-id")
let shouty = words.into(@case_conv.CaseStyle::ShoutySnake)
inspect(shouty, content="GET_USER_BY_ID")
}///|
test "parsing from various case styles" {
// From camelCase variations
let camel_words = @case_conv.Words::from_lower_camel("httpResponseCode")
inspect(camel_words, content="Words([\"http\", \"response\", \"code\"])")
let pascal_words = @case_conv.Words::from_upper_camel("HttpResponseCode")
inspect(pascal_words, content="Words([\"http\", \"response\", \"code\"])")
// From separator-based styles
let snake_words = @case_conv.Words::from_snake("http_response_code")
inspect(snake_words, content="Words([\"http\", \"response\", \"code\"])")
let kebab_words = @case_conv.Words::from_kebab("http-response-code")
inspect(kebab_words, content="Words([\"http\", \"response\", \"code\"])")
// From space-separated styles
let sentence_words = @case_conv.Words::from_sentence("Http response code")
inspect(sentence_words, content="Words([\"http\", \"response\", \"code\"])")
let title_words = @case_conv.Words::from_title("Http Response Code")
inspect(title_words, content="Words([\"http\", \"response\", \"code\"])")
}///|
test "edge cases and special handling" {
// Empty strings
let empty_words = @case_conv.Words::from_snake("")
inspect(empty_words, content="Words([])")
let empty_result = empty_words.into_upper_camel()
inspect(empty_result, content="")
// Single word
let single_words = @case_conv.Words::from_snake("hello")
let single_result = single_words.into_upper_camel()
inspect(single_result, content="Hello")
// Already lowercase words
let lower_words = @case_conv.Words::from_sentence("hello world")
let camel_result = lower_words.into_lower_camel()
inspect(camel_result, content="helloWorld")
}///|
test "common programming case conversions" {
// API endpoint naming
let api_endpoint = @case_conv.Words::from_snake("get_user_profile")
let url_path = api_endpoint.into_kebab()
inspect(url_path, content="get-user-profile")
// Database column to JavaScript property
let db_column = @case_conv.Words::from_snake("created_at_timestamp")
let js_property = db_column.into_lower_camel()
inspect(js_property, content="createdAtTimestamp")
// Environment variable naming
let env_var = @case_conv.Words::from_kebab("database-connection-url")
let env_name = env_var.into_shouty_snake()
inspect(env_name, content="DATABASE_CONNECTION_URL")
// Class name generation
let class_words = @case_conv.Words::from_kebab("user-profile-service")
let class_name = class_words.into_upper_camel()
inspect(class_name, content="UserProfileService")
}///|
test "configuration and documentation examples" {
// Configuration key transformations
let config_key = @case_conv.Words::from_snake("max_retry_attempts")
let yaml_key = config_key.into_kebab()
inspect(yaml_key, content="max-retry-attempts")
// Documentation title generation
let func_name = @case_conv.Words::from_lower_camel("validateUserInput")
let doc_title = func_name.into_title()
inspect(doc_title, content="Validate User Input")
// Sentence description
let sentence = func_name.into_sentence()
inspect(sentence, content="Validate user input")
}{
"import": [
"illusory0x0/case_conv"
]
}pub(all) enum CaseStyle {
UpperCamel
LowerCamel
Snake
Kebab
ShoutySnake
Sentence
Title
ShoutyKebab
}pub(all) type Words FixedArray[String]Install
Download zipA comprehensive MoonBit library for converting between different case styles including camelCase, snake_case, kebab-case, PascalCase, SHOUTY_SNAKE_CASE, Title Case, and more. Features bidirectional conversion, type-safe enums, and zero dependencies.