|
1 | 1 | # sx |
2 | 2 |
|
3 | | -[](https://pkg.go.dev/github.com/gomantics/sx) |
| 3 | +[](https://pkg.go.dev/github.com/gomantics/sx) [](https://github.com/gomantics/sx/actions/workflows/ci.yml) |
4 | 4 |
|
5 | | -TODO Description |
| 5 | +A simple Go library for string case conversion. Converts between camelCase, PascalCase, kebab-case, snake_case, and more. |
6 | 6 |
|
7 | 7 | ## Installation |
8 | 8 |
|
9 | 9 | ```bash |
10 | 10 | go get github.com/gomantics/sx |
11 | 11 | ``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```go |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/gomantics/sx" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + // Convert to different cases |
| 26 | + fmt.Println(sx.CamelCase("hello-world")) // helloWorld |
| 27 | + fmt.Println(sx.PascalCase("hello_world")) // HelloWorld |
| 28 | + fmt.Println(sx.KebabCase("HelloWorld")) // hello-world |
| 29 | + fmt.Println(sx.SnakeCase("HelloWorld")) // hello_world |
| 30 | + fmt.Println(sx.TrainCase("hello-world")) // Hello-World |
| 31 | + fmt.Println(sx.FlatCase("hello-world")) // helloworld |
| 32 | + |
| 33 | + // Works with mixed separators and cases |
| 34 | + fmt.Println(sx.CamelCase("mixed_caseWith-different.separators")) // mixedCaseWithDifferentSeparators |
| 35 | + |
| 36 | + // Handle complex acronyms |
| 37 | + fmt.Println(sx.KebabCase("XMLHttpRequest")) // xml-http-request |
| 38 | + fmt.Println(sx.CamelCase("HTML5Parser")) // html5Parser |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Case Functions |
| 43 | + |
| 44 | +- `CamelCase()` - converts to camelCase |
| 45 | +- `PascalCase()` - converts to PascalCase |
| 46 | +- `KebabCase()` - converts to kebab-case |
| 47 | +- `SnakeCase()` - converts to snake_case |
| 48 | +- `TrainCase()` - converts to Train-Case |
| 49 | +- `FlatCase()` - converts to flatcase (no separators) |
| 50 | + |
| 51 | +## Utilities |
| 52 | + |
| 53 | +- `SplitByCase()` - splits strings into words by case changes and separators |
| 54 | +- `UpperFirst()` - capitalizes first character |
| 55 | +- `LowerFirst()` - lowercases first character |
| 56 | + |
| 57 | +## Options |
| 58 | + |
| 59 | +Some functions support options for customization: |
| 60 | + |
| 61 | +```go |
| 62 | +// Normalize case for strict PascalCase/camelCase |
| 63 | +sx.PascalCase("XMLHttpRequest", sx.WithNormalize(true)) // XmlHttpRequest |
| 64 | + |
| 65 | +// Custom separators for splitting |
| 66 | +sx.SplitByCase("hello.world", sx.WithSeparators('.')) // ["hello", "world"] |
| 67 | + |
| 68 | +// Custom separator for kebab case |
| 69 | +sx.KebabCase("hello world", "|") // hello|world |
| 70 | +``` |
| 71 | + |
| 72 | +## Contributing |
| 73 | + |
| 74 | +Contributors are always welcome! Feel free to raise a PR or create an issue first. |
| 75 | + |
| 76 | +## License |
| 77 | + |
| 78 | +MIT |
0 commit comments