-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
86 lines (71 loc) · 2.26 KB
/
examples_test.go
File metadata and controls
86 lines (71 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package strings2_test
import (
"fmt"
"strings"
"github.com/arran4/strings2"
)
// Example usage tests (documentation tests)
func ExampleParse() {
words, _ := strings2.Parse("helloWorld")
fmt.Println(words)
// Output: [hello World]
}
func ExampleParse_smartAcronyms() {
words, _ := strings2.Parse("XMLReader", strings2.WithSmartAcronyms(true))
fmt.Println(words)
// Output: [XML Reader]
}
func ExampleParse_snakeCase() {
words, _ := strings2.ParseSnakeCase("hello_world")
fmt.Println(words)
// Output: [hello world]
}
func ExampleNewPartitioner_customFormat() {
// Inventing a custom format: "Dot.Separated.Values"
// We want to split by '.' but keep the parts capitalized as is (or handled by classification).
input := "User.Profile.Settings"
// Create a partitioner that splits on dot
partitioner := strings2.NewPartitioner(strings2.PartitionerConfig{
Delimiters: map[rune]bool{'.': true},
SplitCamel: true, // Split if there's camel case inside a part
})
// Use Parse with the custom partitioner
words, _ := strings2.Parse(input, partitioner)
// Convert to Snake Case
// Using CMWhispering to force lowercase, otherwise ExactCaseWord preserves case by default
snake, _ := strings2.ToSnakeCase(words, strings2.OptionCaseMode(strings2.CMWhispering))
fmt.Println(snake)
// Output: user_profile_settings
}
func ExampleToSnake_options() {
// Converting Camel to Screaming Snake
// ToSnake forces delimiter to "_". CMScreaming forces upper.
input := "camelCase"
output, _ := strings2.ToSnake(input, strings2.OptionCaseMode(strings2.CMScreaming))
fmt.Println(output)
// Output: CAMEL_CASE
}
func ExampleToFormattedCase_customSpongeCase() {
// Custom formatting: SpongeBob Case (sPoNgEbOb cAsE)
// This demonstrates iterating over words and applying custom logic.
input := "hello world"
words, _ := strings2.Parse(input)
var sb strings.Builder
for i, word := range words {
if i > 0 {
sb.WriteString(" ")
}
s := word.String()
for j, r := range s {
// Simple alternating case logic relative to the whole string start or word start
// Let's do word-local alternating
if j%2 == 0 {
sb.WriteString(strings.ToLower(string(r)))
} else {
sb.WriteString(strings.ToUpper(string(r)))
}
}
}
fmt.Println(sb.String())
// Output: hElLo wOrLd
}