Skip to content

Commit 9015124

Browse files
committed
changes Delimiter to a string, updates README
1 parent 4da4e95 commit 9015124

File tree

5 files changed

+88
-57
lines changed

5 files changed

+88
-57
lines changed

README.md

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,27 @@ import (
4444
"github.com/chanced/caps"
4545
)
4646
func main() {
47-
c := caps.New()
48-
fmt.Println(c.ToCamel("http request"))
47+
fmt.Println(caps.ToCamel("http request"))
4948
// Output:
5049
// HTTPRequest
51-
fmt.Println(c.ToCamel("http request"))
50+
fmt.Println(caps.ToCamel("http request"))
5251
// Output:
5352
// HTTPRequest
54-
fmt.Println(c.ToLowerCamel("some_id"))
53+
fmt.Println(caps.ToLowerCamel("some_id"))
5554
// Output:
5655
// someID
57-
fmt.Println(c.ToLowerCamel("SomeID", caps.WithReplaceStyleCamel()))
56+
fmt.Println(caps.ToLowerCamel("SomeID", caps.WithReplaceStyleCamel()))
5857
// Output:
5958
// someId
6059

6160
// Alternatively:
62-
63-
fmt.Println(caps.ToCamel("http request"))
61+
capsJS := caps.New(caps.CapsOpts{
62+
AllowedSymbols: "$",
63+
ReplaceStyle: caps.ReplaceStyleCamel
64+
})
65+
fmt.Println(capsJS.ToCamel("SomeID"))
6466
// Output:
65-
// HTTPRequest
66-
67-
67+
// someId
6868
}
6969
```
7070

@@ -154,6 +154,30 @@ options. See below.
154154

155155
## Customizing the `Converter`
156156

157+
### Using caps.Caps
158+
159+
This is likely your best option. You can create and utilize an instance of
160+
`caps.Caps` which as all of the conversions as methods:
161+
162+
```go
163+
package main
164+
import (
165+
"fmt"
166+
"github.com/chanced/caps"
167+
"github.com/chanced/caps/token"
168+
)
169+
170+
c := caps.New(caps.CapsOpts{
171+
Replacements: []caps.Replacement{
172+
{"Ex", "EX" },
173+
// ... your replacements
174+
},
175+
})
176+
c.ToLower("some ex")
177+
// Output:
178+
// SomeEX
179+
```
180+
157181
### Creating isolated `caps.StdConverter` instances
158182

159183
You can pass a new instance of `caps.StdConverter` with a new set of
@@ -278,50 +302,57 @@ func main() {
278302

279303
input: `"Example Uuid Test Case."`
280304

305+
```
306+
goos: darwin
307+
goarch: arm64
308+
pkg: github.com/chanced/caps
309+
BenchmarkToTitle
310+
```
311+
281312
Using a `caps.Caps` instance:
282313

283314
```
284315
BenchmarkCapsToTitle
285-
BenchmarkCapsToTitle-10 1000000 1007 ns/op 288 B/op 19 allocs/op
316+
BenchmarkCapsToTitle-10 1000000 1005 ns/op 288 B/op 19 allocs/op
286317
BenchmarkCapsToCamel
287-
BenchmarkCapsToCamel-10 1217474 984.5 ns/op 288 B/op 19 allocs/op
318+
BenchmarkCapsToCamel-10 1214407 995.0 ns/op 288 B/op 19 allocs/op
288319
BenchmarkCapsToLowerCamel
289-
BenchmarkCapsToLowerCamel-10 1223340 979.1 ns/op 288 B/op 19 allocs/op
320+
BenchmarkCapsToLowerCamel-10 1219000 980.1 ns/op 288 B/op 19 allocs/op
290321
BenchmarkCapsToSnake
291-
BenchmarkCapsToSnake-10 1205770 996.5 ns/op 288 B/op 20 allocs/op
322+
BenchmarkCapsToSnake-10 1212786 989.1 ns/op 288 B/op 19 allocs/op
292323
BenchmarkCapsToScreamingSnake
293-
BenchmarkCapsToScreamingSnake-10 1000000 1037 ns/op 336 B/op 21 allocs/op
324+
BenchmarkCapsToScreamingSnake-10 1000000 1027 ns/op 336 B/op 20 allocs/op
294325
BenchmarkCapsToKebab
295-
BenchmarkCapsToKebab-10 1000000 1021 ns/op 336 B/op 21 allocs/op
326+
BenchmarkCapsToKebab-10 1000000 1007 ns/op 336 B/op 20 allocs/op
296327
BenchmarkCapsToScreamingKebab
297-
BenchmarkCapsToScreamingKebab-10 1000000 1052 ns/op 336 B/op 21 allocs/op
328+
BenchmarkCapsToScreamingKebab-10 1000000 1018 ns/op 336 B/op 20 allocs/op
298329
BenchmarkCapsToDotNotation
299-
BenchmarkCapsToDotNotation-10 1000000 1019 ns/op 336 B/op 21 allocs/op
330+
BenchmarkCapsToDotNotation-10 1000000 1017 ns/op 336 B/op 20 allocs/op
300331
BenchmarkCapsToScreamingDotNotation
301-
BenchmarkCapsToScreamingDotNotation-10 1000000 1032 ns/op 336 B/op 21 allocs/op
332+
BenchmarkCapsToScreamingDotNotation-10 1000000 1042 ns/op 336 B/op 20 allocs/op
302333
```
303334

304335
Using top-level functions:
305336

306337
```
307338
BenchmarkToTitle
308-
BenchmarkToTitle-10 1128897 1051 ns/op 336 B/op 20 allocs/op
339+
BenchmarkToTitle-10 1138011 1046 ns/op 336 B/op 20 allocs/op
309340
BenchmarkToCamel
310-
BenchmarkToCamel-10 1000000 1030 ns/op 336 B/op 20 allocs/op
341+
BenchmarkToCamel-10 1000000 1023 ns/op 336 B/op 20 allocs/op
311342
BenchmarkToLowerCamel
312-
BenchmarkToLowerCamel-10 1000000 1014 ns/op 336 B/op 20 allocs/op
343+
BenchmarkToLowerCamel-10 1000000 1020 ns/op 336 B/op 20 allocs/op
313344
BenchmarkToSnake
314-
BenchmarkToSnake-10 1000000 1026 ns/op 336 B/op 21 allocs/op
345+
BenchmarkToSnake-10 1000000 1024 ns/op 336 B/op 20 allocs/op
315346
BenchmarkToScreamingSnake
316-
BenchmarkToScreamingSnake-10 1000000 1042 ns/op 336 B/op 21 allocs/op
347+
BenchmarkToScreamingSnake-10 1000000 1040 ns/op 336 B/op 20 allocs/op
317348
BenchmarkToKebab
318-
BenchmarkToKebab-10 1000000 1021 ns/op 336 B/op 21 allocs/op
349+
BenchmarkToKebab-10 1000000 1024 ns/op 336 B/op 20 allocs/op
319350
BenchmarkToScreamingKebab
320-
BenchmarkToScreamingKebab-10 1000000 1032 ns/op 336 B/op 21 allocs/op
351+
BenchmarkToScreamingKebab-10 1000000 1032 ns/op 336 B/op 20 allocs/op
321352
BenchmarkToDotNotation
322-
BenchmarkToDotNotation-10 1000000 1020 ns/op 336 B/op 21 allocs/op
353+
BenchmarkToDotNotation-10 1000000 1020 ns/op 336 B/op 20 allocs/op
323354
BenchmarkToScreamingDotNotation
324-
BenchmarkToScreamingDotNotation-10 1000000 1033 ns/op 336 B/op 21 allocs/op
355+
BenchmarkToScreamingDotNotation-10 1000000 1028 ns/op 336 B/op 20 allocs/op
325356
```
326357

327358
## License

caps.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Caps struct {
2121
type CapsOpts struct {
2222
// Any characters within this string will be allowed in the output.
2323
//
24-
// This does not affect delimiters (e.g. '_', '-', '.') as they are added
24+
// This does not affect delimiters (e.g. "_", "-", ".") as they are added
2525
// post-tokenization.
2626
//
2727
// Default:
@@ -30,7 +30,7 @@ type CapsOpts struct {
3030
// The Converter to use.
3131
//
3232
// Default:
33-
// DefaultConverter
33+
// A StdConverter with the Replacements, Caser, and Tokenizer.
3434
Converter Converter
3535

3636
// If not set, this will be DefaultReplacements.
@@ -59,7 +59,7 @@ type CapsOpts struct {
5959
// functions)
6060
Caser token.Caser
6161

62-
// If not set, uses DefaultTokenizer
62+
// If not set, uses StdTokenizer with the provided delimiters and token.Caser.
6363
Tokenizer Tokenizer
6464
}
6565

@@ -154,7 +154,7 @@ func (c Caps) LowerFirst(str string) string {
154154

155155
// Without numbers returns the string with all numeric runes removed.
156156
//
157-
// It does not currently use any logic to determine if a rune (e.g. '.')
157+
// It does not currently use any logic to determine if a rune (e.g. ".")
158158
// is part of a number. This may change in the future.
159159
func (c Caps) WithoutNumbers(s string) string {
160160
return strings.Map(func(r rune) rune {
@@ -211,7 +211,7 @@ func (c Caps) ToLowerCamel(str string) string {
211211
//
212212
// caps.ToSnake("This is [an] {example}${id32}.") // this_is_an_example_id_32
213213
func (c Caps) ToSnake(str string) string {
214-
return c.ToDelimited(str, '_', true)
214+
return c.ToDelimited(str, "_", true)
215215
}
216216

217217
// ToScreamingSnake transforms the case of str into Screaming Snake Case (e.g.
@@ -220,15 +220,15 @@ func (c Caps) ToSnake(str string) string {
220220
//
221221
// caps.ToScreamingSnake("This is [an] {example}${id32}.") // THIS_IS_AN_EXAMPLE_ID_32
222222
func (c Caps) ToScreamingSnake(str string) string {
223-
return ToDelimited(str, '_', false)
223+
return ToDelimited(str, "_", false)
224224
}
225225

226226
// ToKebab transforms the case of str into Lower Kebab Case (e.g. an-example-string) using
227227
// either the provided Converter or the DefaultConverter otherwise.
228228
//
229229
// caps.ToKebab("This is [an] {example}${id32}.") // this-is-an-example-id-32
230230
func (c Caps) ToKebab(str string) string {
231-
return ToDelimited(str, '-', true)
231+
return ToDelimited(str, "-", true)
232232
}
233233

234234
// ToScreamingKebab transforms the case of str into Screaming Kebab Snake (e.g.
@@ -237,15 +237,15 @@ func (c Caps) ToKebab(str string) string {
237237
//
238238
// caps.ToScreamingKebab("This is [an] {example}${id32}.") // THIS-IS-AN-EXAMPLE-ID-32
239239
func (c Caps) ToScreamingKebab(str string) string {
240-
return ToDelimited(str, '-', false)
240+
return ToDelimited(str, "-", false)
241241
}
242242

243243
// ToDotNotation transforms the case of str into Lower Dot Notation Case (e.g. an.example.string) using
244244
// either the provided Converter or the DefaultConverter otherwise.
245245
//
246246
// caps.ToDotNotation("This is [an] {example}${id32}.") // this.is.an.example.id.32
247247
func (c Caps) ToDotNotation(str string) string {
248-
return ToDelimited(str, '.', true)
248+
return ToDelimited(str, ".", true)
249249
}
250250

251251
// ToScreamingDotNotation transforms the case of str into Screaming Kebab Case (e.g.
@@ -254,7 +254,7 @@ func (c Caps) ToDotNotation(str string) string {
254254
//
255255
// caps.ToScreamingDotNotation("This is [an] {example}${id32}.") // THIS.IS.AN.EXAMPLE.ID.32
256256
func (c Caps) ToScreamingDotNotation(str string) string {
257-
return ToDelimited(str, '.', false)
257+
return ToDelimited(str, ".", false)
258258
}
259259

260260
// ToTitle transforms the case of str into Title Case (e.g. An Example String) using
@@ -281,10 +281,10 @@ func (c Caps) ToTitle(str string) string {
281281
//
282282
// # Example
283283
//
284-
// caps.ToDelimited("This is [an] {example}${id}.#32", '.', true) // this.is.an.example.id.32
285-
// caps.ToDelimited("This is [an] {example}${id}.break32", '.', false) // THIS.IS.AN.EXAMPLE.ID.BREAK.32
286-
// caps.ToDelimited("This is [an] {example}${id}.v32", '.', true, caps.Opts{AllowedSymbols: "$"}) // this.is.an.example.$.id.v32
287-
func (c Caps) ToDelimited(str string, delimiter rune, lowercase bool) string {
284+
// caps.ToDelimited("This is [an] {example}${id}.#32", ".", true) // this.is.an.example.id.32
285+
// caps.ToDelimited("This is [an] {example}${id}.break32", ".", false) // THIS.IS.AN.EXAMPLE.ID.BREAK.32
286+
// caps.ToDelimited("This is [an] {example}${id}.v32", ".", true, caps.Opts{AllowedSymbols: "$"}) // this.is.an.example.$.id.v32
287+
func (c Caps) ToDelimited(str string, delimiter string, lowercase bool) string {
288288
var style Style
289289
var replacementStyle ReplaceStyle
290290
if lowercase {
@@ -298,7 +298,7 @@ func (c Caps) ToDelimited(str string, delimiter rune, lowercase bool) string {
298298
Style: style,
299299
ReplaceStyle: replacementStyle,
300300
Input: str,
301-
Join: string(delimiter),
301+
Join: delimiter,
302302
AllowedSymbols: c.allowedSymbols,
303303
NumberRules: c.numberRules,
304304
})

conversions.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func ToLowerCamel[T ~string](str T, options ...Opts) T {
9393
//
9494
// caps.ToSnake("This is [an] {example}${id32}.") // this_is_an_example_id_32
9595
func ToSnake[T ~string](str T, options ...Opts) T {
96-
return ToDelimited(str, '_', true, options...)
96+
return ToDelimited(str, "_", true, options...)
9797
}
9898

9999
// ToScreamingSnake transforms the case of str into Screaming Snake Case (e.g.
@@ -102,15 +102,15 @@ func ToSnake[T ~string](str T, options ...Opts) T {
102102
//
103103
// caps.ToScreamingSnake("This is [an] {example}${id32}.") // THIS_IS_AN_EXAMPLE_ID_32
104104
func ToScreamingSnake[T ~string](str T, options ...Opts) T {
105-
return ToDelimited(str, '_', false, options...)
105+
return ToDelimited(str, "_", false, options...)
106106
}
107107

108108
// ToKebab transforms the case of str into Lower Kebab Case (e.g. an-example-string) using
109109
// either the provided Converter or the DefaultConverter otherwise.
110110
//
111111
// caps.ToKebab("This is [an] {example}${id32}.") // this-is-an-example-id-32
112112
func ToKebab[T ~string](str T, options ...Opts) T {
113-
return ToDelimited(str, '-', true, options...)
113+
return ToDelimited(str, "-", true, options...)
114114
}
115115

116116
// ToScreamingKebab transforms the case of str into Screaming Kebab Snake (e.g.
@@ -119,15 +119,15 @@ func ToKebab[T ~string](str T, options ...Opts) T {
119119
//
120120
// caps.ToScreamingKebab("This is [an] {example}${id32}.") // THIS-IS-AN-EXAMPLE-ID-32
121121
func ToScreamingKebab[T ~string](str T, options ...Opts) T {
122-
return ToDelimited(str, '-', false, options...)
122+
return ToDelimited(str, "-", false, options...)
123123
}
124124

125125
// ToDotNotation transforms the case of str into Lower Dot Notation Case (e.g. an.example.string) using
126126
// either the provided Converter or the DefaultConverter otherwise.
127127
//
128128
// caps.ToDotNotation("This is [an] {example}${id32}.") // this.is.an.example.id.32
129129
func ToDotNotation[T ~string](str T, options ...Opts) T {
130-
return ToDelimited(str, '.', true, options...)
130+
return ToDelimited(str, ".", true, options...)
131131
}
132132

133133
// ToScreamingDotNotation transforms the case of str into Screaming Kebab Case (e.g.
@@ -136,7 +136,7 @@ func ToDotNotation[T ~string](str T, options ...Opts) T {
136136
//
137137
// caps.ToScreamingDotNotation("This is [an] {example}${id32}.") // THIS.IS.AN.EXAMPLE.ID.32
138138
func ToScreamingDotNotation[T ~string](str T, options ...Opts) T {
139-
return ToDelimited(str, '.', false, options...)
139+
return ToDelimited(str, ".", false, options...)
140140
}
141141

142142
// ToTitle transforms the case of str into Title Case (e.g. An Example String) using
@@ -164,10 +164,10 @@ func ToTitle[T ~string](str T, options ...Opts) T {
164164
//
165165
// # Example
166166
//
167-
// caps.ToDelimited("This is [an] {example}${id}.#32", '.', true) // this.is.an.example.id.32
168-
// caps.ToDelimited("This is [an] {example}${id}.break32", '.', false) // THIS.IS.AN.EXAMPLE.ID.BREAK.32
169-
// caps.ToDelimited("This is [an] {example}${id}.v32", '.', true, caps.Opts{AllowedSymbols: "$"}) // this.is.an.example.$.id.v32
170-
func ToDelimited[T ~string](str T, delimiter rune, lowercase bool, options ...Opts) T {
167+
// caps.ToDelimited("This is [an] {example}${id}.#32", ".", true) // this.is.an.example.id.32
168+
// caps.ToDelimited("This is [an] {example}${id}.break32", ".", false) // THIS.IS.AN.EXAMPLE.ID.BREAK.32
169+
// caps.ToDelimited("This is [an] {example}${id}.v32", ".", true, caps.Opts{AllowedSymbols: "$"}) // this.is.an.example.$.id.v32
170+
func ToDelimited[T ~string](str T, delimiter string, lowercase bool, options ...Opts) T {
171171
opts := loadOpts(options)
172172
var style Style
173173
var replacementStyle ReplaceStyle

examples_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func ExampleToScreamingDotNotation() {
6363
}
6464

6565
func ExampleToDelimited() {
66-
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.#32", '.', true))
67-
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.break32", '.', false))
68-
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.v32", '.', true, caps.Opts{AllowedSymbols: "$"}))
66+
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.#32", ".", true))
67+
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.break32", ".", false))
68+
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.v32", ".", true, caps.Opts{AllowedSymbols: "$"}))
6969

7070
// Output:
7171
// this.is.an.example.id.32

text/text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (t Text) ToTitle(opts ...caps.Opts) Text {
112112
// using either the provided Converter or the DefaultConverter otherwise.
113113
//
114114
// If lowercase is false, the output will be all uppercase.
115-
func (t Text) ToDelimited(delimiter rune, lowercase bool, opts ...caps.Opts) Text {
115+
func (t Text) ToDelimited(delimiter string, lowercase bool, opts ...caps.Opts) Text {
116116
return caps.ToDelimited(t, delimiter, lowercase, opts...)
117117
}
118118

0 commit comments

Comments
 (0)