-
Notifications
You must be signed in to change notification settings - Fork 12
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
43 additions
and
1 deletion.
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,18 @@ | ||
package utils | ||
|
||
import "strings" | ||
|
||
func NormalizeUrl(url string) string { | ||
url = strings.TrimSpace(url) | ||
parts := strings.Split(url, " ") | ||
|
||
for _, part := range parts { | ||
// Take the firs string that looks like a URL. | ||
// TODO: We could try to parse the url, but will save that for later | ||
if strings.HasPrefix(part, "http") || strings.HasPrefix(part, "www") { | ||
return part | ||
} | ||
} | ||
|
||
return url | ||
} |
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,21 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestNormalizeUrl(t *testing.T) { | ||
tests := []struct { | ||
url string | ||
want string | ||
}{ | ||
{url: "example.com", want: "example.com"}, | ||
{url: "https://example.com", want: "https://example.com"}, | ||
{url: "https://example.com this is an example", want: "https://example.com"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.url, func(t *testing.T) { | ||
if got := NormalizeUrl(tt.url); got != tt.want { | ||
t.Errorf("NormalizeUrl() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |