Skip to content

Commit

Permalink
feat(logic): add some functions to deal with urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 13, 2023
1 parent ce12c96 commit 5f9c8a1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions x/logic/util/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package util

import (
"net/url"
)

// UrlMatches is a function that returns a function that matches the given url against the given other item.
//
// The function matches the components of the given url against the components of the given other url. If the component
// of the given other url is empty, it is considered to match the component of the given url.
// For example:
// - matchUrl("http://example.com/foo")("http://example.com/foo") -> true
// - matchUrl("http://example.com/foo")("http://example.com/foo?bar=baz") -> false
// - matchUrl("tel:123456789")("tel:") -> true
//
// The function is curried, and is a binary relation that is reflexive, associative (but not commutative).
func UrlMatches(this *url.URL) func(*url.URL) bool {
return func(that *url.URL) bool {
return (that.Scheme == "" || that.Scheme == this.Scheme) &&
(that.Opaque == "" || that.Opaque == this.Opaque) &&
(that.User == nil || that.User.String() == "" || that.User.String() == this.User.String()) &&
(that.Host == "" || that.Host == this.Host) &&
(that.Path == "" || that.Path == this.Path) &&
(that.RawQuery == "" || that.RawQuery == this.RawQuery) &&
(that.Fragment == "" || that.Fragment == this.Fragment)
}
}

// ParseUrlMust parses the given url and panics if it fails.
// You have been warned.
func ParseUrlMust(s string) *url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return u
}

0 comments on commit 5f9c8a1

Please sign in to comment.