|
| 1 | +# strings2 |
| 2 | + |
| 3 | +[](https://github.com/arran4/strings2/actions/workflows/test.yml) |
| 4 | +[](https://github.com/arran4/strings2/actions/workflows/vet.yml) |
| 5 | +[](https://github.com/arran4/strings2/actions/workflows/golangci-lint.yml) |
| 6 | +[](https://github.com/arran4/strings2/actions/workflows/fmt.yml) |
| 7 | +[](https://pkg.go.dev/github.com/arran4/strings2) |
| 8 | + |
| 9 | +strings2 provides utilities for converting slices of words into various casing conventions. It is intended to supplement Go's standard library `strings` package with helpers for creating formats such as `camelCase`, `PascalCase`, `snake_case` and `kebab-case`. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +``` |
| 14 | +go get github.com/arran4/strings2 |
| 15 | +``` |
| 16 | + |
| 17 | +Add the module to your project and import it: |
| 18 | + |
| 19 | +```go |
| 20 | +import "github.com/arran4/strings2" |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +Words must implement `fmt.Stringer`. The package defines several helper types which satisfy this interface: |
| 26 | + |
| 27 | +```go |
| 28 | +words := []strings2.Word{ |
| 29 | + strings2.SingleCaseWord("hello"), |
| 30 | + strings2.SingleCaseWord("world"), |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Parsing |
| 35 | + |
| 36 | +The library includes a robust parser to convert strings into typed `Word` objects, distinguishing between acronyms, casing, and delimiters. |
| 37 | + |
| 38 | +```go |
| 39 | +// Auto-detect format and parse |
| 40 | +words, err := strings2.Parse("helloWorld") |
| 41 | +// Result: [SingleCaseWord("hello"), FirstUpperCaseWord("World")] |
| 42 | + |
| 43 | +// Parse specific format |
| 44 | +words = strings2.ParseSnakeCase("hello_world") |
| 45 | + |
| 46 | +// Configure parser |
| 47 | +words, err = strings2.Parse("N.E.W. World", strings2.ParserSmartAcronyms(true)) |
| 48 | +``` |
| 49 | + |
| 50 | +### Case Conversion Functions |
| 51 | + |
| 52 | +```go |
| 53 | +strings2.ToCamelCase(words) // "helloWorld" |
| 54 | +strings2.ToPascalCase(words) // "HelloWorld" |
| 55 | +strings2.ToKebabCase(words) // "hello-world" |
| 56 | +strings2.ToSnakeCase(words) // "hello_world" |
| 57 | +``` |
| 58 | + |
| 59 | +### Customising Formatting |
| 60 | + |
| 61 | +Behaviour can be tuned with options passed to each function. Some commonly used options include: |
| 62 | + |
| 63 | +- `OptionDelimiter(string)` – change the delimiter used between words. |
| 64 | +- `OptionCaseMode(CaseMode)` – set the case transformation mode. Modes include: |
| 65 | + - `CMVerbatim` |
| 66 | + - `CMFirstTitle` |
| 67 | + - `CMAllTitle` |
| 68 | + - `CMFirstLower` |
| 69 | + - `CMWhispering` |
| 70 | + - `CMScreaming` |
| 71 | +- `OptionFirstUpper()` – force the result to start with an uppercase letter. |
| 72 | +- `OptionFirstLower()` – force the result to start with a lowercase letter. |
| 73 | + |
| 74 | +Examples: |
| 75 | + |
| 76 | +```go |
| 77 | +// Custom delimiter |
| 78 | +fmt.Println(strings2.ToKebabCase(words, strings2.OptionDelimiter("|"))) |
| 79 | + |
| 80 | +// Screaming snake case |
| 81 | +fmt.Println(strings2.ToSnakeCase(words, strings2.OptionCaseMode(strings2.CMScreaming))) |
| 82 | +``` |
| 83 | + |
| 84 | +### CLI Mode |
| 85 | + |
| 86 | +The library also provides a command-line interface that exposes all these options, ensuring that the CLI mode has as much flexibility as the code (without being obligated to use smart defaults). |
| 87 | + |
| 88 | +```bash |
| 89 | +strings2 camel "hello world" |
| 90 | +# Result: helloWorld |
| 91 | + |
| 92 | +strings2 snake --screaming "hello world" |
| 93 | +# Result: HELLO_WORLD |
| 94 | + |
| 95 | +strings2 kebab --first-upper "hello world" |
| 96 | +# Result: Hello-world |
| 97 | +``` |
| 98 | + |
| 99 | +You can pipe input into the CLI as well: |
| 100 | +```bash |
| 101 | +echo "hello world" | strings2 pascal |
| 102 | +# Result: HelloWorld |
| 103 | +``` |
| 104 | + |
| 105 | +Available flags across commands: |
| 106 | +- `--delimiter`, `-d` (string): Override the delimiter |
| 107 | +- `--screaming`, `-S`: Enforce uppercase formatting |
| 108 | +- `--whispering`, `-w`: Enforce lowercase formatting |
| 109 | +- `--first-upper`, `-U`: Capitalize the first letter |
| 110 | +- `--first-lower`, `-l`: Lowercase the first letter |
| 111 | +- `--mix-case-support`, `-m`: Enable splitting of mixed case words |
| 112 | +- `--no-smart-acronyms`: Disable acronym preservation |
| 113 | +- `--number-splitting`: Enable letter-digit boundary splitting |
| 114 | + |
| 115 | +# Screaming snake case |
| 116 | +strings2 snake --screaming "hello world" |
| 117 | +``` |
| 118 | + |
| 119 | +Options are composable so multiple behaviours can be applied at once. See the documentation in `types.go` for details on further options. |
| 120 | + |
| 121 | +## TODO |
| 122 | + |
| 123 | +- Support slices for flags when the gosubc version supports it. |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details. |
0 commit comments