Skip to content

Commit

Permalink
Formatted file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Dec 26, 2022
1 parent 64d97a5 commit e347d22
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var emptySpace = []byte("")

// 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
//
// View examples: sanitize_test.go
func Alpha(original string, spaces bool) string {

// Leave white spaces?
Expand All @@ -57,7 +58,8 @@ func Alpha(original string, spaces bool) string {

// 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
//
// View examples: sanitize_test.go
func AlphaNumeric(original string, spaces bool) string {

// Leave white spaces?
Expand All @@ -70,37 +72,42 @@ func AlphaNumeric(original string, spaces bool) string {
}

// BitcoinAddress returns sanitized value for bitcoin address
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func BitcoinAddress(original string) string {
return string(bitcoinRegExp.ReplaceAll([]byte(original), emptySpace))
}

// BitcoinCashAddress returns sanitized value for bitcoin `cashaddr`
// address (https://www.bitcoinabc.org/2018-01-14-CashAddr/)
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func BitcoinCashAddress(original string) string {
return string(bitcoinCashAddrRegExp.ReplaceAll([]byte(original), emptySpace))
}

// 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
//
// View examples: sanitize_test.go
func Custom(original string, regExp string) string {

// Return the processed string or panic if regex fails
return string(regexp.MustCompile(regExp).ReplaceAll([]byte(original), emptySpace))
}

// Decimal returns sanitized decimal/float values in either positive or negative.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Decimal(original string) string {
return string(decimalRegExp.ReplaceAll([]byte(original), emptySpace))
}

// 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
//
// View examples: sanitize_test.go
func Domain(original string, preserveCase bool, removeWww bool) (string, error) {

// Try to see if we have a host
Expand Down Expand Up @@ -135,7 +142,8 @@ func Domain(original string, preserveCase bool, removeWww bool) (string, error)

// Email returns a sanitized email address string. Email addresses are forced
// to lowercase and removes any mail-to prefixes.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Email(original string, preserveCase bool) string {

// Leave the email address in its original case
Expand All @@ -153,7 +161,8 @@ func Email(original string, preserveCase bool) string {

// FirstToUpper overwrites the first letter as an uppercase letter
// and preserves the rest of the string.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func FirstToUpper(original string) string {

// Handle empty and 1 character strings
Expand All @@ -167,19 +176,22 @@ func FirstToUpper(original string) string {
}

// FormalName returns a formal name or surname (for First, Middle and Last)
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func FormalName(original string) string {
return string(formalNameRegExp.ReplaceAll([]byte(original), emptySpace))
}

// HTML returns a string without any <HTML> tags.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func HTML(original string) string {
return string(htmlRegExp.ReplaceAll([]byte(original), emptySpace))
}

// IPAddress returns an ip address for both ipv4 and ipv6 formats.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func IPAddress(original string) string {
// Parse the IP - Remove any invalid characters first
ipAddress := net.ParseIP(
Expand All @@ -193,61 +205,71 @@ func IPAddress(original string) string {
}

// Numeric returns numbers only.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Numeric(original string) string {
return string(numericRegExp.ReplaceAll([]byte(original), emptySpace))
}

// PathName returns a formatted path compliant name.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func PathName(original string) string {
return string(pathNameRegExp.ReplaceAll([]byte(original), emptySpace))
}

// Punctuation returns a string with basic punctuation preserved.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Punctuation(original string) string {
return string(punctuationRegExp.ReplaceAll([]byte(original), emptySpace))
}

// Scripts removes all scripts, iframes and embeds tags from string.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Scripts(original string) string {
return string(scriptRegExp.ReplaceAll([]byte(original), emptySpace))
}

// SingleLine returns a single line string, removes all carriage returns.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func SingleLine(original string) string {
return singleLineRegExp.ReplaceAllString(original, " ")
}

// Time returns just the time part of the string.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func Time(original string) string {
return string(timeRegExp.ReplaceAll([]byte(original), emptySpace))
}

// URI returns allowed URI characters only.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func URI(original string) string {
return string(uriRegExp.ReplaceAll([]byte(original), emptySpace))
}

// URL returns a formatted url friendly string.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func URL(original string) string {
return string(urlRegExp.ReplaceAll([]byte(original), emptySpace))
}

// XML returns a string without any <XML> tags - alias of HTML.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func XML(original string) string {
return HTML(original)
}

// XSS removes known XSS attack strings or script strings.
// View examples: sanitize_test.go
//
// View examples: sanitize_test.go
func XSS(original string) string {
original = strings.Replace(original, "<script", "", -1)
original = strings.Replace(original, "script>", "", -1)
Expand Down

0 comments on commit e347d22

Please sign in to comment.