-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsanitize_example_test.go
More file actions
210 lines (175 loc) · 5.84 KB
/
sanitize_example_test.go
File metadata and controls
210 lines (175 loc) · 5.84 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package sanitize_test
import (
"fmt"
"regexp"
"github.com/mrz1836/go-sanitize"
)
// ExampleAlpha example using Alpha() and no spaces flag
func ExampleAlpha() {
fmt.Println(sanitize.Alpha("Example String!", false))
// Output: ExampleString
}
// ExampleAlphaNumeric example using AlphaNumeric() with no spaces
func ExampleAlphaNumeric() {
fmt.Println(sanitize.AlphaNumeric("Example String 2!", false))
// Output: ExampleString2
}
// ExampleAlphaNumeric_withSpaces example using AlphaNumeric() with spaces
func ExampleAlphaNumeric_withSpaces() {
fmt.Println(sanitize.AlphaNumeric("Example String 2!", true))
// Output: Example String 2
}
// ExampleAlpha_withSpaces example using Alpha with a space flag
func ExampleAlpha_withSpaces() {
fmt.Println(sanitize.Alpha("Example String!", true))
// Output: Example String
}
// ExampleBitcoinAddress example using BitcoinAddress()
func ExampleBitcoinAddress() {
fmt.Println(sanitize.BitcoinAddress(":1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!"))
// Output: 1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs
}
// ExampleBitcoinCashAddress example using BitcoinCashAddress() `cashaddr`
func ExampleBitcoinCashAddress() {
fmt.Println(sanitize.BitcoinAddress("qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz!"))
// Output: qze7yy2au5vuznvn8zj5yj5t66vhs75e3meptz
}
// ExampleCustom example using Custom() using an alpha regex
func ExampleCustom() {
fmt.Println(sanitize.Custom("Example String 2!", `[^a-zA-Z]`))
// Output: ExampleString
}
// ExampleCustomCompiled example using CustomCompiled with an alpha regex
func ExampleCustomCompiled() {
re := regexp.MustCompile(`[^a-zA-Z]`)
result, err := sanitize.CustomCompiled("Example String 2!", re)
fmt.Println(result, err)
// Output: ExampleString <nil>
}
// ExampleCustom_numeric example using Custom() using a numeric regex
func ExampleCustom_numeric() {
fmt.Println(sanitize.Custom("Example String 2!", `[^0-9]`))
// Output: 2
}
// ExampleDecimal example using Decimal() for a positive number
func ExampleDecimal() {
fmt.Println(sanitize.Decimal("$ 99.99!"))
// Output: 99.99
}
// ExampleDecimal_negative example using Decimal() for a negative number
func ExampleDecimal_negative() {
fmt.Println(sanitize.Decimal("$ -99.99!"))
// Output: -99.99
}
// ExampleDomain example using Domain()
func ExampleDomain() {
fmt.Println(sanitize.Domain("https://www.Example.COM/?param=value", false, false))
// Output: www.example.com <nil>
}
// ExampleDomain_preserveCase example using Domain() and preserving the case
func ExampleDomain_preserveCase() {
fmt.Println(sanitize.Domain("https://www.Example.COM/?param=value", true, false))
// Output: www.Example.COM <nil>
}
// ExampleDomain_removeWww example using Domain() and removing the www subdomain
func ExampleDomain_removeWww() {
fmt.Println(sanitize.Domain("https://www.Example.COM/?param=value", false, true))
// Output: example.com <nil>
}
// ExampleEmail example using Email()
func ExampleEmail() {
fmt.Println(sanitize.Email("mailto:Person@Example.COM", false))
// Output: person@example.com
}
// ExampleEmail_preserveCase example using Email() and preserving the case
func ExampleEmail_preserveCase() {
fmt.Println(sanitize.Email("mailto:Person@Example.COM", true))
// Output: Person@Example.COM
}
// ExampleFirstToUpper example using FirstToUpper()
func ExampleFirstToUpper() {
fmt.Println(sanitize.FirstToUpper("this works"))
// Output: This works
}
// ExampleFormalName example using FormalName()
func ExampleFormalName() {
fmt.Println(sanitize.FormalName("John McDonald Jr.!"))
// Output: John McDonald Jr.
}
// ExampleHTML example using HTML()
func ExampleHTML() {
fmt.Println(sanitize.HTML("<body>This Works?</body>"))
// Output: This Works?
}
// ExampleIPAddress example using IPAddress() for IPV4 address
func ExampleIPAddress() {
fmt.Println(sanitize.IPAddress(" 192.168.0.1 "))
// Output: 192.168.0.1
}
// ExampleIPAddress_ipv6 example using IPAddress() for IPV6 address
func ExampleIPAddress_ipv6() {
fmt.Println(sanitize.IPAddress(" 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f "))
// Output: 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f
}
// ExampleNumeric example using Numeric()
func ExampleNumeric() {
fmt.Println(sanitize.Numeric("This:123 + 90!"))
// Output: 12390
}
// ExamplePhoneNumber example using PhoneNumber()
func ExamplePhoneNumber() {
fmt.Println(sanitize.PhoneNumber("+1 (234) 567-8900"))
// Output: +12345678900
}
// ExampleNumeric example using PathName()
func ExamplePathName() {
fmt.Println(sanitize.PathName("/This-Works_Now-123/!"))
// Output: This-Works_Now-123
}
// ExamplePunctuation example using Punctuation()
func ExamplePunctuation() {
fmt.Println(sanitize.Punctuation(`[@"Does" 'this' work?@] this too`))
// Output: "Does" 'this' work? this too
}
// ExampleScientificNotation example using ScientificNotation() for a positive number
func ExampleScientificNotation() {
fmt.Println(sanitize.ScientificNotation("$ 1.096e-3!"))
// Output: 1.096e-3
}
// ExampleScripts example using Scripts()
func ExampleScripts() {
fmt.Println(sanitize.Scripts(`Does<script>This</script>Work?`))
// Output: DoesWork?
}
// ExampleSingleLine example using SingleLine()
func ExampleSingleLine() {
fmt.Println(sanitize.SingleLine(`Does
This
Work?`))
// Output: Does This Work?
}
// ExampleTime example using Time()
func ExampleTime() {
fmt.Println(sanitize.Time(`Time 01:02:03!`))
// Output: 01:02:03
}
// ExampleURI example using URI()
func ExampleURI() {
fmt.Println(sanitize.URI("/This/Works?^No&this"))
// Output: /This/Works?No&this
}
// ExampleURL example using URL()
func ExampleURL() {
fmt.Println(sanitize.URL("https://Example.com/This/Works?^No&this"))
// Output: https://Example.com/This/Works?No&this
}
// ExampleXML example using XML()
func ExampleXML() {
fmt.Println(sanitize.XML("<xml>This?</xml>"))
// Output: This?
}
// ExampleXSS example using XSS()
func ExampleXSS() {
fmt.Println(sanitize.XSS("<script>This?</script>"))
// Output: >This?</
}