Skip to content

Commit c17a202

Browse files
authored
Fixes delimited methods on Caps & improves perf
* Delimited methods on Caps (`ToSnake`, `ToScreamingSnake`, `ToKebab`, `ToScreamingKebab`, `ToDotNotation, `ToScreamingDotNotation`) were previously not using the instance `ToDelimited` method and thus config was not being used. * Adds `WriteUpperFirstLowerRest`, `WriteSplitLowerFirstUpperRest`, `WriteSplitLower`, `WriteSplitUpper`, `Write`, `WriteUpper`, `WriteLower`, `WriteRune` to `token` package for a minor perf improvement. * updates benchmarks
1 parent 4500636 commit c17a202

File tree

8 files changed

+873
-75
lines changed

8 files changed

+873
-75
lines changed

README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -319,46 +319,48 @@ Using a `caps.Caps` instance:
319319

320320
```
321321
BenchmarkCapsToTitle
322-
BenchmarkCapsToTitle-10 2418796 473.9 ns/op 208 B/op 9 allocs/op
322+
BenchmarkCapsToTitle-10 2488982 470.2 ns/op 200 B/op 8 allocs/op
323323
BenchmarkCapsToCamel
324-
BenchmarkCapsToCamel-10 2572858 466.4 ns/op 208 B/op 9 allocs/op
324+
BenchmarkCapsToCamel-10 2474846 467.8 ns/op 200 B/op 8 allocs/op
325325
BenchmarkCapsToLowerCamel
326-
BenchmarkCapsToLowerCamel-10 2607831 458.1 ns/op 208 B/op 9 allocs/op
326+
BenchmarkCapsToLowerCamel-10 2650843 451.0 ns/op 200 B/op 8 allocs/op
327327
BenchmarkCapsToSnake
328-
BenchmarkCapsToSnake-10 2567318 466.2 ns/op 208 B/op 9 allocs/op
328+
BenchmarkCapsToSnake-10 2619945 456.9 ns/op 200 B/op 8 allocs/op
329329
BenchmarkCapsToScreamingSnake
330-
BenchmarkCapsToScreamingSnake-10 2380708 504.7 ns/op 256 B/op 10 allocs/op
330+
BenchmarkCapsToScreamingSnake-10 2591302 461.9 ns/op 200 B/op 8 allocs/op
331331
BenchmarkCapsToKebab
332-
BenchmarkCapsToKebab-10 2408059 501.0 ns/op 256 B/op 10 allocs/op
332+
BenchmarkCapsToKebab-10 2621020 457.0 ns/op 200 B/op 8 allocs/op
333333
BenchmarkCapsToScreamingKebab
334-
BenchmarkCapsToScreamingKebab-10 2383544 500.0 ns/op 256 B/op 10 allocs/op
334+
BenchmarkCapsToScreamingKebab-10 2593428 466.3 ns/op 200 B/op 8 allocs/op
335335
BenchmarkCapsToDotNotation
336-
BenchmarkCapsToDotNotation-10 2418474 495.8 ns/op 256 B/op 10 allocs/op
336+
BenchmarkCapsToDotNotation-10 2575178 463.0 ns/op 200 B/op 8 allocs/op
337337
BenchmarkCapsToScreamingDotNotation
338-
BenchmarkCapsToScreamingDotNotation-10 2375731 504.0 ns/op 256 B/op 10 allocs/op
338+
BenchmarkCapsToScreamingDotNotation-10 2526782 472.8 ns/op 200 B/op 8 allocs/op
339339
```
340340

341341
Using top-level functions:
342342

343343
```
344344
BenchmarkToTitle
345-
BenchmarkToTitle-10 2349061 505.8 ns/op 256 B/op 10 allocs/op
345+
BenchmarkToTitle-10 2375107 506.0 ns/op 248 B/op 9 allocs/op
346346
BenchmarkToCamel
347-
BenchmarkToCamel-10 2406518 503.7 ns/op 256 B/op 10 allocs/op
347+
BenchmarkToCamel-10 2374930 501.1 ns/op 248 B/op 9 allocs/op
348348
BenchmarkToLowerCamel
349-
BenchmarkToLowerCamel-10 2391112 503.9 ns/op 256 B/op 10 allocs/op
349+
BenchmarkToLowerCamel-10 2373976 495.3 ns/op 248 B/op 9 allocs/op
350350
BenchmarkToSnake
351-
BenchmarkToSnake-10 2392641 503.3 ns/op 256 B/op 10 allocs/op
351+
BenchmarkToSnake-10 2370608 518.8 ns/op 248 B/op 9 allocs/op
352352
BenchmarkToScreamingSnake
353-
BenchmarkToScreamingSnake-10 2311863 517.7 ns/op 256 B/op 10 allocs/op
353+
BenchmarkToScreamingSnake-10 2339785 500.4 ns/op 248 B/op 9 allocs/op
354354
BenchmarkToKebab
355-
BenchmarkToKebab-10 2390890 503.5 ns/op 256 B/op 10 allocs/op
355+
BenchmarkToKebab-10 2424816 502.7 ns/op 248 B/op 9 allocs/op
356356
BenchmarkToScreamingKebab
357-
BenchmarkToScreamingKebab-10 2313615 519.0 ns/op 256 B/op 10 allocs/op
357+
BenchmarkToScreamingKebab-10 2371681 510.4 ns/op 248 B/op 9 allocs/op
358358
BenchmarkToDotNotation
359-
BenchmarkToDotNotation-10 2344407 510.3 ns/op 256 B/op 10 allocs/op
359+
BenchmarkToDotNotation-10 2367168 512.4 ns/op 248 B/op 9 allocs/op
360360
BenchmarkToScreamingDotNotation
361-
BenchmarkToScreamingDotNotation-10 2294145 521.2 ns/op 256 B/op 10 allocs/op
361+
BenchmarkToScreamingDotNotation-10 2295390 509.7 ns/op 248 B/op 9 allocs/op
362+
BenchmarkWithLongerInput
363+
BenchmarkWithLongerInput-10 1476777 817.1 ns/op 280 B/op 13 allocs/op
362364
```
363365

364366
## License

caps.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (c Caps) AllowedSymbols() string {
164164
return c.allowedSymbols
165165
}
166166

167+
func (c Caps) Converter() Converter {
168+
return c.converter
169+
}
170+
167171
// UpperFirst converts the first rune of str to unicode upper case.
168172
//
169173
// This method does not support special cases (such as Turkish and Azeri)
@@ -244,15 +248,15 @@ func (c Caps) ToSnake(str string) string {
244248
//
245249
// caps.ToScreamingSnake("This is [an] {example}${id32}.") // THIS_IS_AN_EXAMPLE_ID_32
246250
func (c Caps) ToScreamingSnake(str string) string {
247-
return ToDelimited(str, "_", false)
251+
return c.ToDelimited(str, "_", false)
248252
}
249253

250254
// ToKebab transforms the case of str into Lower Kebab Case (e.g. an-example-string) using
251255
// either the provided Converter or the DefaultConverter otherwise.
252256
//
253257
// caps.ToKebab("This is [an] {example}${id32}.") // this-is-an-example-id-32
254258
func (c Caps) ToKebab(str string) string {
255-
return ToDelimited(str, "-", true)
259+
return c.ToDelimited(str, "-", true)
256260
}
257261

258262
// ToScreamingKebab transforms the case of str into Screaming Kebab Snake (e.g.
@@ -261,15 +265,15 @@ func (c Caps) ToKebab(str string) string {
261265
//
262266
// caps.ToScreamingKebab("This is [an] {example}${id32}.") // THIS-IS-AN-EXAMPLE-ID-32
263267
func (c Caps) ToScreamingKebab(str string) string {
264-
return ToDelimited(str, "-", false)
268+
return c.ToDelimited(str, "-", false)
265269
}
266270

267271
// ToDotNotation transforms the case of str into Lower Dot Notation Case (e.g. an.example.string) using
268272
// either the provided Converter or the DefaultConverter otherwise.
269273
//
270274
// caps.ToDotNotation("This is [an] {example}${id32}.") // this.is.an.example.id.32
271275
func (c Caps) ToDotNotation(str string) string {
272-
return ToDelimited(str, ".", true)
276+
return c.ToDelimited(str, ".", true)
273277
}
274278

275279
// ToScreamingDotNotation transforms the case of str into Screaming Kebab Case (e.g.
@@ -278,7 +282,7 @@ func (c Caps) ToDotNotation(str string) string {
278282
//
279283
// caps.ToScreamingDotNotation("This is [an] {example}${id32}.") // THIS.IS.AN.EXAMPLE.ID.32
280284
func (c Caps) ToScreamingDotNotation(str string) string {
281-
return ToDelimited(str, ".", false)
285+
return c.ToDelimited(str, ".", false)
282286
}
283287

284288
// ToTitle transforms the case of str into Title Case (e.g. An Example String) using

0 commit comments

Comments
 (0)