Usage
sx provides a simple and intuitive API for handling string case conversions. This guide covers common usage patterns and advanced configurations.
Case Conversion
The core functionality of sx is converting strings between different cases.
Camel Case
Converts strings to camelCase.
sx.CamelCase("hello-world") // helloWorld
sx.CamelCase("Hello World") // helloWorld
sx.CamelCase("hello_world") // helloWorld
Pascal Case
Converts strings to PascalCase.
sx.PascalCase("hello-world") // HelloWorld
sx.PascalCase("camelCase") // CamelCase
Kebab Case
Converts strings to kebab-case.
sx.KebabCase("HelloWorld") // hello-world
sx.KebabCase("camelCase") // camel-case
Snake Case
Converts strings to snake_case.
sx.SnakeCase("HelloWorld") // hello_world
sx.SnakeCase("kebab-case") // kebab_case
Train Case
Converts strings to Train-Case.
sx.TrainCase("hello_world") // Hello-World
Flat Case
Converts strings to flatcase (lowercase, no separators).
sx.FlatCase("Hello World") // helloworld
Advanced Options
Normalization
By default, sx preserves consecutive uppercase letters (e.g., XMLRequest -> XMLRequest). If you want strict normalization (e.g., XMLRequest -> XmlRequest), use the WithNormalize option.
// Default behavior
sx.PascalCase("XMLHttpRequest") // XMLHttpRequest
// With normalization
sx.PascalCase("XMLHttpRequest", sx.WithNormalize(true)) // XmlHttpRequest
Custom Separators
When splitting strings, sx uses a default set of separators. You can override this behavior using SplitByCase with WithSeparators.
// Custom separators for splitting
words := sx.SplitByCase("hello|world", sx.WithSeparators('|'))
// ["hello", "world"]
Custom Kebab Separator
KebabCase allows you to specify a custom separator string.
sx.KebabCase("hello world", "|") // hello|world
Utilities
sx also exports some utility functions that can be useful.
SplitByCase
Splits a string into words based on case changes and separators.
sx.SplitByCase("helloWorld") // ["hello", "World"]
UpperFirst
Capitalizes the first character of a string.
sx.UpperFirst("hello") // Hello
LowerFirst
Lowercases the first character of a string.
sx.LowerFirst("Hello") // hello