Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
184 lines (165 loc) · 4.2 KB
/
Copy pathexample_test.go
File metadata and controls
184 lines (165 loc) · 4.2 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package stringx_test
import (
"fmt"
"github.com/bycigo/stringx"
)
func ExampleLen() {
fmt.Println(stringx.Len("hello"))
fmt.Println(stringx.Len("你好"))
fmt.Println(stringx.Len("🎉🎊"))
// Output:
// 5
// 2
// 2
}
func ExampleReverse() {
fmt.Println(stringx.Reverse("hello"))
fmt.Println(stringx.Reverse("你好世界"))
fmt.Println(stringx.Reverse("abc123"))
// Output:
// olleh
// 界世好你
// 321cba
}
func ExamplePadLeft() {
fmt.Println(stringx.PadLeft("go", 5, "x"))
fmt.Println(stringx.PadLeft("hello", 10, "0"))
fmt.Println(stringx.PadLeft("test", 8, "ab"))
// Output:
// xxxgo
// 00000hello
// ababtest
}
func ExamplePadRight() {
fmt.Println(stringx.PadRight("go", 5, "x"))
fmt.Println(stringx.PadRight("hello", 10, "0"))
fmt.Println(stringx.PadRight("test", 8, "ab"))
// Output:
// goxxx
// hello00000
// testabab
}
func ExamplePadBoth() {
fmt.Println(stringx.PadBoth("go", 6, "x"))
fmt.Println(stringx.PadBoth("hi", 8, "ab"))
fmt.Println(stringx.PadBoth("test", 10, "="))
// Output:
// xxgoxx
// abahiaba
// ===test===
}
func ExampleCamelCase() {
fmt.Println(stringx.CamelCase("hello_world"))
fmt.Println(stringx.CamelCase("hello-world"))
fmt.Println(stringx.CamelCase("hello world"))
fmt.Println(stringx.CamelCase("HelloWorld"))
// Output:
// helloWorld
// helloWorld
// helloWorld
// helloWorld
}
func ExamplePascalCase() {
fmt.Println(stringx.PascalCase("hello_world"))
fmt.Println(stringx.PascalCase("hello-world"))
fmt.Println(stringx.PascalCase("hello world"))
fmt.Println(stringx.PascalCase("helloWorld"))
// Output:
// HelloWorld
// HelloWorld
// HelloWorld
// HelloWorld
}
func ExampleSnakeCase() {
fmt.Println(stringx.SnakeCase("helloWorld"))
fmt.Println(stringx.SnakeCase("HelloWorld"))
fmt.Println(stringx.SnakeCase("hello-world"))
fmt.Println(stringx.SnakeCase("HTTPServer"))
// Output:
// hello_world
// hello_world
// hello_world
// http_server
}
func ExampleKebabCase() {
fmt.Println(stringx.KebabCase("helloWorld"))
fmt.Println(stringx.KebabCase("HelloWorld"))
fmt.Println(stringx.KebabCase("hello_world"))
fmt.Println(stringx.KebabCase("HTTPServer"))
// Output:
// hello-world
// hello-world
// hello-world
// http-server
}
func ExampleRandom() {
// Generate a random string of length 10
s := stringx.Random(10)
fmt.Printf("Random string length: %d\n", len(s))
// Output:
// Random string length: 10
}
func ExampleFromBytes() {
bytes := []byte("hello world")
s := stringx.FromBytes(bytes)
fmt.Println(s)
// Output:
// hello world
}
func ExampleToBytes() {
s := "hello world"
bytes := stringx.ToBytes(s)
fmt.Println(string(bytes))
// Output:
// hello world
}
func ExamplePassword() {
// Generate a secure password of length 16 using the default character sets
// (letters, numbers and symbols).
pwd := stringx.Password(16)
fmt.Printf("Password length: %d\n", len(pwd))
// Output:
// Password length: 16
}
func ExamplePassword_includes() {
// Restrict the password to letters and numbers only.
pwd := stringx.Password(12, stringx.PasswordIncludes{
stringx.PasswordLetter,
stringx.PasswordNumber,
})
fmt.Printf("Password length: %d\n", len(pwd))
// Output:
// Password length: 12
}
func ExampleNewPasswordFactory() {
// A non-cryptographic factory is faster and suitable for non-sensitive
// scenarios such as test fixtures.
factory := stringx.NewPasswordFactory()
pwd := factory.MakePassword(20)
fmt.Printf("Password length: %d\n", len(pwd))
// Output:
// Password length: 20
}
func ExampleNewSecuredPasswordFactory() {
// A cryptographically secure factory backed by crypto/rand.
factory := stringx.NewSecuredPasswordFactory()
pwd := factory.MakePassword(20)
fmt.Printf("Password length: %d\n", len(pwd))
// Output:
// Password length: 20
}
func ExampleNewPasswordFactoryWithSource() {
// Provide a custom character source to fully control the alphabet.
src := stringx.PasswordSource{
stringx.PasswordLetter: []byte("abcdef"),
stringx.PasswordNumber: []byte("0123"),
}
factory := stringx.NewPasswordFactoryWithSource(src)
pwd := factory.MakePassword(16, stringx.PasswordIncludes{
stringx.PasswordLetter,
stringx.PasswordNumber,
})
fmt.Printf("Password length: %d\n", len(pwd))
// Output:
// Password length: 16
}