Documentation ¶
Overview ¶
Package sanitize (go-sanitize) implements a simple library of various sanitation methods for data transformation.
If you have any suggestions or comments, please feel free to open an issue on this project's GitHub page.
Index ¶
- func Alpha(original string, spaces bool) string
- func AlphaNumeric(original string, spaces bool) string
- func BitcoinAddress(original string) string
- func BitcoinCashAddress(original string) string
- func Custom(original string, regExp string) string
- func Decimal(original string) string
- func Domain(original string, preserveCase bool, removeWww bool) (string, error)
- func Email(original string, preserveCase bool) string
- func FirstToUpper(original string) string
- func FormalName(original string) string
- func HTML(original string) string
- func IPAddress(original string) string
- func Numeric(original string) string
- func PathName(original string) string
- func Punctuation(original string) string
- func Scripts(original string) string
- func SingleLine(original string) string
- func Time(original string) string
- func URI(original string) string
- func URL(original string) string
- func XML(original string) string
- func XSS(original string) string
Examples ¶
- Alpha
- Alpha (WithSpaces)
- AlphaNumeric
- AlphaNumeric (WithSpaces)
- BitcoinAddress
- BitcoinCashAddress
- Custom
- Custom (Numeric)
- Decimal
- Decimal (Negative)
- Domain
- Domain (PreserveCase)
- Domain (RemoveWww)
- Email (PreserveCase)
- FirstToUpper
- FormalName
- HTML
- IPAddress
- IPAddress (Ipv6)
- Numeric
- PathName
- Punctuation
- Scripts
- SingleLine
- Time
- URI
- URL
- XML
- XSS
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alpha ¶
Alpha returns only alpha characters. Set the parameter spaces to true if you want to allow space characters. Valid characters are a-z and A-Z.
View examples: sanitize_test.go
Example ¶
ExampleAlpha example using Alpha() and no spaces flag
fmt.Println(Alpha("Example String!", false))
Output: ExampleString
Example (WithSpaces) ¶
ExampleAlpha_withSpaces example using Alpha with spaces flag
fmt.Println(Alpha("Example String!", true))
Output: Example String
func AlphaNumeric ¶
AlphaNumeric returns only alphanumeric characters. Set the parameter spaces to true if you want to allow space characters. Valid characters are a-z, A-Z and 0-9.
View examples: sanitize_test.go
Example ¶
ExampleAlphaNumeric example using AlphaNumeric() with no spaces
fmt.Println(AlphaNumeric("Example String 2!", false))
Output: ExampleString2
Example (WithSpaces) ¶
ExampleAlphaNumeric_withSpaces example using AlphaNumeric() with spaces
fmt.Println(AlphaNumeric("Example String 2!", true))
Output: Example String 2
func BitcoinAddress ¶ added in v1.0.5
BitcoinAddress returns sanitized value for bitcoin address
View examples: sanitize_test.go
Example ¶
ExampleBitcoinAddress example using BitcoinAddress()
fmt.Println(BitcoinAddress(":1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!"))
Output: 1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs
func BitcoinCashAddress ¶ added in v1.0.6
BitcoinCashAddress returns sanitized value for bitcoin `cashaddr` address (https://www.bitcoinabc.org/2018-01-14-CashAddr/)
View examples: sanitize_test.go
Example ¶
ExampleBitcoinCashAddress example using BitcoinCashAddress() `cashaddr`
fmt.Println(BitcoinAddress("qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz!"))
Output: qze7yy2au5vuznvn8zj5yj5t66vhs75e3meptz
func Custom ¶
Custom uses a custom regex string and returns the sanitized result. This is used for any additional regex that this package does not contain.
View examples: sanitize_test.go
Example ¶
ExampleCustom example using Custom() using an alpha regex
fmt.Println(Custom("Example String 2!", `[^a-zA-Z]`))
Output: ExampleString
Example (Numeric) ¶
ExampleCustom_numeric example using Custom() using a numeric regex
fmt.Println(Custom("Example String 2!", `[^0-9]`))
Output: 2
func Decimal ¶
Decimal returns sanitized decimal/float values in either positive or negative.
View examples: sanitize_test.go
Example ¶
ExampleDecimal example using Decimal() for a positive number
fmt.Println(Decimal("$ 99.99!"))
Output: 99.99
Example (Negative) ¶
ExampleDecimal_negative example using Decimal() for a negative number
fmt.Println(Decimal("$ -99.99!"))
Output: -99.99
func Domain ¶
Domain returns a proper hostname / domain name. Preserve case is to flag keeping the case versus forcing to lowercase. Use the removeWww flag to strip the www sub-domain. This method returns an error if parse critically fails.
View examples: sanitize_test.go
Example ¶
ExampleDomain example using Domain()
fmt.Println(Domain("https://www.Example.COM/?param=value", false, false))
Output: www.example.com <nil>
Example (PreserveCase) ¶
ExampleDomain_preserveCase example using Domain() and preserving the case
fmt.Println(Domain("https://www.Example.COM/?param=value", true, false))
Output: www.Example.COM <nil>
Example (RemoveWww) ¶
ExampleDomain_removeWww example using Domain() and removing the www sub-domain
fmt.Println(Domain("https://www.Example.COM/?param=value", false, true))
Output: example.com <nil>
func Email ¶
Email returns a sanitized email address string. Email addresses are forced to lowercase and removes any mail-to prefixes.
View examples: sanitize_test.go
Example ¶
ExampleEmail example using Email()
fmt.Println(Email("mailto:Person@Example.COM", false))
Output: person@example.com
Example (PreserveCase) ¶
ExampleEmail_preserveCase example using Email() and preserving the case
fmt.Println(Email("mailto:Person@Example.COM", true))
Output: Person@Example.COM
func FirstToUpper ¶
FirstToUpper overwrites the first letter as an uppercase letter and preserves the rest of the string.
View examples: sanitize_test.go
Example ¶
ExampleFirstToUpper example using FirstToUpper()
fmt.Println(FirstToUpper("this works"))
Output: This works
func FormalName ¶
FormalName returns a formal name or surname (for First, Middle and Last)
View examples: sanitize_test.go
Example ¶
ExampleFormalName example using FormalName()
fmt.Println(FormalName("John McDonald Jr.!"))
Output: John McDonald Jr.
func HTML ¶
HTML returns a string without any <HTML> tags.
View examples: sanitize_test.go
Example ¶
ExampleHTML example using HTML()
fmt.Println(HTML("<body>This Works?</body>"))
Output: This Works?
func IPAddress ¶
IPAddress returns an ip address for both ipv4 and ipv6 formats.
View examples: sanitize_test.go
Example ¶
ExampleIPAddress example using IPAddress() for IPV4 address
fmt.Println(IPAddress(" 192.168.0.1 "))
Output: 192.168.0.1
Example (Ipv6) ¶
ExampleIPAddress_ipv6 example using IPAddress() for IPV6 address
fmt.Println(IPAddress(" 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f "))
Output: 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f
func Numeric ¶
Numeric returns numbers only.
View examples: sanitize_test.go
Example ¶
ExampleNumeric example using Numeric()
fmt.Println(Numeric("This:123 + 90!"))
Output: 12390
func PathName ¶
PathName returns a formatted path compliant name.
View examples: sanitize_test.go
Example ¶
ExampleNumeric example using PathName()
fmt.Println(PathName("/This-Works_Now-123/!"))
Output: This-Works_Now-123
func Punctuation ¶
Punctuation returns a string with basic punctuation preserved.
View examples: sanitize_test.go
Example ¶
ExamplePunctuation example using Punctuation()
fmt.Println(Punctuation(`[@"Does" 'this' work?@] this too`))
Output: "Does" 'this' work? this too
func Scripts ¶
Scripts removes all scripts, iframes and embeds tags from string.
View examples: sanitize_test.go
Example ¶
ExampleScripts example using Scripts()
fmt.Println(Scripts(`Does<script>This</script>Work?`))
Output: DoesWork?
func SingleLine ¶
SingleLine returns a single line string, removes all carriage returns.
View examples: sanitize_test.go
Example ¶
ExampleSingleLine example using SingleLine()
fmt.Println(SingleLine(`Does This Work?`))
Output: Does This Work?
func Time ¶
Time returns just the time part of the string.
View examples: sanitize_test.go
Example ¶
ExampleTime example using Time()
fmt.Println(Time(`Time 01:02:03!`))
Output: 01:02:03
func URI ¶
URI returns allowed URI characters only.
View examples: sanitize_test.go
Example ¶
ExampleURI example using URI()
fmt.Println(URI("/This/Works?^No&this"))
Output: /This/Works?No&this
func URL ¶
URL returns a formatted url friendly string.
View examples: sanitize_test.go
Example ¶
ExampleURL example using URL()
fmt.Println(URL("https://Example.com/This/Works?^No&this"))
Output: https://Example.com/This/Works?No&this
Types ¶
This section is empty.