feat(url): add normalize utility for url scheme handling
- commit
- 5ee1115980bbbeefa0afbb02d92c8773a3ba3cee
- author
- bnema · · 2025-12-18 03:51 UTC
- parent
- 9c927bd
- signature
- unsigned
1 file · +43 -0
A internal/domain/url/normalize.go +43 -0
| @@ -0,0 +1,43 @@ | |||
| 1 | + | // Package url provides URL manipulation utilities for the browser. | |
| 2 | + | package url | |
| 3 | + | ||
| 4 | + | import "strings" | |
| 5 | + | ||
| 6 | + | // Normalize adds https:// prefix if missing for URL-like inputs. | |
| 7 | + | // Returns the input unchanged if it already has a scheme or doesn't look like a URL. | |
| 8 | + | func Normalize(input string) string { | |
| 9 | + | if input == "" { | |
| 10 | + | return "" | |
| 11 | + | } | |
| 12 | + | ||
| 13 | + | // Already has scheme | |
| 14 | + | switch { | |
| 15 | + | case strings.HasPrefix(input, "http://"): | |
| 16 | + | return input | |
| 17 | + | case strings.HasPrefix(input, "https://"): | |
| 18 | + | return input | |
| 19 | + | case strings.HasPrefix(input, "dumb://"): | |
| 20 | + | return input | |
| 21 | + | case strings.HasPrefix(input, "file://"): | |
| 22 | + | return input | |
| 23 | + | case strings.HasPrefix(input, "about:"): | |
| 24 | + | return input | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | // Looks like a URL (contains . and no spaces) | |
| 28 | + | if strings.Contains(input, ".") && !strings.Contains(input, " ") { | |
| 29 | + | return "https://" + input | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | return input | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | // LooksLikeURL checks if the input appears to be a URL (not a search query). | |
| 36 | + | // Returns true for strings like "github.com", "google.com/search", etc. | |
| 37 | + | func LooksLikeURL(input string) bool { | |
| 38 | + | if input == "" { | |
| 39 | + | return false | |
| 40 | + | } | |
| 41 | + | // Contains a dot and no spaces = likely a URL | |
| 42 | + | return strings.Contains(input, ".") && !strings.Contains(input, " ") | |
| 43 | + | } | |
| @@ -0,0 +1,43 @@ | |||||
| 1 | + | // Package url provides URL manipulation utilities for the browser. | |||
| 2 | + | package url | |||
| 3 | + | ||||
| 4 | + | import "strings" | |||
| 5 | + | ||||
| 6 | + | // Normalize adds https:// prefix if missing for URL-like inputs. | |||
| 7 | + | // Returns the input unchanged if it already has a scheme or doesn't look like a URL. | |||
| 8 | + | func Normalize(input string) string { | |||
| 9 | + | if input == "" { | |||
| 10 | + | return "" | |||
| 11 | + | } | |||
| 12 | + | ||||
| 13 | + | // Already has scheme | |||
| 14 | + | switch { | |||
| 15 | + | case strings.HasPrefix(input, "http://"): | |||
| 16 | + | return input | |||
| 17 | + | case strings.HasPrefix(input, "https://"): | |||
| 18 | + | return input | |||
| 19 | + | case strings.HasPrefix(input, "dumb://"): | |||
| 20 | + | return input | |||
| 21 | + | case strings.HasPrefix(input, "file://"): | |||
| 22 | + | return input | |||
| 23 | + | case strings.HasPrefix(input, "about:"): | |||
| 24 | + | return input | |||
| 25 | + | } | |||
| 26 | + | ||||
| 27 | + | // Looks like a URL (contains . and no spaces) | |||
| 28 | + | if strings.Contains(input, ".") && !strings.Contains(input, " ") { | |||
| 29 | + | return "https://" + input | |||
| 30 | + | } | |||
| 31 | + | ||||
| 32 | + | return input | |||
| 33 | + | } | |||
| 34 | + | ||||
| 35 | + | // LooksLikeURL checks if the input appears to be a URL (not a search query). | |||
| 36 | + | // Returns true for strings like "github.com", "google.com/search", etc. | |||
| 37 | + | func LooksLikeURL(input string) bool { | |||
| 38 | + | if input == "" { | |||
| 39 | + | return false | |||
| 40 | + | } | |||
| 41 | + | // Contains a dot and no spaces = likely a URL | |||
| 42 | + | return strings.Contains(input, ".") && !strings.Contains(input, " ") | |||
| 43 | + | } | |||