-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
Package sanitize provides functions to sanitize user input into a safe format. | ||
*/ | ||
package sanitize | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// String sanitizes a string by removing any non-alphanumeric characters. | ||
func String(s string) string { | ||
return strings.ReplaceAll(strings.ReplaceAll(s, "\r", ""), "\n", "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package sanitize | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type TestCase struct { | ||
input string | ||
expected string | ||
} | ||
|
||
func TestSanitize(t *testing.T) { | ||
tt := []TestCase{ | ||
{"hello\nworld", "helloworld"}, | ||
{"hello\rworld", "helloworld"}, | ||
{"hello\r\nworld", "helloworld"}, | ||
{"hello world", "hello world"}, | ||
{`{ "hello": "world" }`, `{ "hello": "world" }`}, | ||
} | ||
|
||
for _, tc := range tt { | ||
if got := String(tc.input); got != tc.expected { | ||
t.Errorf("Sanitize(%s) = %s; want %s", tc.input, got, tc.expected) | ||
} | ||
} | ||
} |