diff --git a/FUNDING.yml b/FUNDING.yml deleted file mode 100644 index 3df6d04..0000000 --- a/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: dunglas diff --git a/README.md b/README.md index c1c8e4c..b12f014 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # Go URL Pattern +[![Go Reference](https://pkg.go.dev/badge/github.com/dunglas/go-urlpattern.svg)](https://pkg.go.dev/github.com/dunglas/go-urlpattern) + A spec-compliant implementation of [the WHATWG URL Pattern Living Standard](https://urlpattern.spec.whatwg.org/) written in [Go](https://go.dev). Tested with [web-platform-test](https://web-platform-tests.org) test suite. +## Docs + +[Read the docs on Go Packages](https://pkg.go.dev/github.com/dunglas/go-urlpattern). + ## Limitations * Some [advanced unicode features (JavaScript's `v` mode)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets) are not supported, because they are not supported by Go regular expressions. diff --git a/parser.go b/parser.go index 33efa2d..5e4238a 100644 --- a/parser.go +++ b/parser.go @@ -334,7 +334,6 @@ func (p *patternParser) addPart(prefix string, nameToken *token, regexpOrWildcar } // https://urlpattern.spec.whatwg.org/#is-a-duplicate-name -// TODO: use a map to improve performance? func (p *patternParser) isDuplicateName(name string) bool { for _, part := range p.partList { if part.name == name { @@ -499,21 +498,6 @@ func canonicalizePort(portValue, protocolValue string) (string, error) { } return p, nil - - // TODO: old code, remove me - /*if _, ok := DefaultPorts[protocolValue]; ok { - return "", nil - } - - p, err := strconv.Atoi(portValue) - if err != nil { - return "", err - } - if p > 65535 { - return "", PortOutOfRangeError - } - - return portValue, nil*/ } // https://urlpattern.spec.whatwg.org/#canonicalize-a-pathname diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index 8f9a1bd..0000000 --- a/pull_request_template.md +++ /dev/null @@ -1,23 +0,0 @@ - diff --git a/urlpattern.go b/urlpattern.go index ae343f1..96aa5ff 100644 --- a/urlpattern.go +++ b/urlpattern.go @@ -110,25 +110,29 @@ func (c *component) protocolComponentMatchesSpecialScheme() bool { } // https://urlpattern.spec.whatwg.org/#url-pattern-create -func New(input string, baseURL *string, options Options) (*URLPattern, error) { +func New(input string, baseURL string, options *Options) (*URLPattern, error) { init, err := parseConstructorString(input) if err != nil { return nil, err } - if baseURL == nil && init.Protocol == nil { + if baseURL == "" && init.Protocol == nil { return nil, NoBaseURLError } - if baseURL != nil { - init.BaseURL = baseURL + if baseURL != "" { + init.BaseURL = &baseURL } return init.New(options) } // https://urlpattern.spec.whatwg.org/#url-pattern-create -func (init *URLPatternInit) New(opt Options) (*URLPattern, error) { +func (init *URLPatternInit) New(opt *Options) (*URLPattern, error) { + if opt == nil { + opt = &Options{} + } + processedInit, err := init.process("pattern", nil, nil, nil, nil, nil, nil, nil, nil) if err != nil { return nil, err diff --git a/urlpattern_test.go b/urlpattern_test.go index f1a4fb9..2bf86f0 100644 --- a/urlpattern_test.go +++ b/urlpattern_test.go @@ -131,8 +131,8 @@ func TestURLPattern(t *testing.T) { } func newPattern(t *testing.T, entry *Entry) (*urlpattern.URLPattern, error) { - var baseURL *string - var options urlpattern.Options + var baseURL string + options := &urlpattern.Options{} switch len(entry.Pattern) { case 0: @@ -145,8 +145,7 @@ func newPattern(t *testing.T, entry *Entry) (*urlpattern.URLPattern, error) { options.IgnoreCase = true case string: - bu := entry.Pattern[1].(string) - baseURL = &bu + baseURL = entry.Pattern[1].(string) default: return nil, errors.New("invalid constructor parameter #1") @@ -160,7 +159,7 @@ func newPattern(t *testing.T, entry *Entry) (*urlpattern.URLPattern, error) { return nil, errors.New("invalid constructor parameter #2") } - baseURL = &bu + baseURL = bu } switch entry.Pattern[0].(type) { @@ -168,7 +167,7 @@ func newPattern(t *testing.T, entry *Entry) (*urlpattern.URLPattern, error) { return urlpattern.New(entry.Pattern[0].(string), baseURL, options) case map[string]interface{}: - if baseURL != nil { + if baseURL != "" { return nil, errors.New("Invalid second argument baseURL provided with a URLPatternInit input. Use the URLPatternInit.baseURL property instead.") } @@ -208,7 +207,7 @@ func newExpectedResult(e Entry) *urlpattern.URLPatternResult { for k, v := range mv["groups"].(map[string]interface{}) { if v == nil { - // TODO: this should be null, but it's currently not implemented + // TODO: this should probably be nil, but it's currently not implemented component.Groups[k] = "" continue } @@ -425,17 +424,18 @@ func assertExpectedObjectProp(t *testing.T, key string, entry Entry, value strin } } -/* -func assertSameResult(t *testing.T, expected *urlpattern.URLPatternResult, value *urlpattern.URLPatternResult) { - -} - -func assertSameResultComponent(t *testing.T, entry Entry, component string, expected *urlpattern.URLPatternComponentResult, value *urlpattern.URLPatternComponentResult) { - if expected.Input != value.Input { - t.Logf("want %q; got %q (%s: %#v)", expected.Input, value.Input, component, entry) +func Example() { + pattern, err := urlpattern.New("/books/:id", "https://example.com", nil) + if err != nil { + panic(err) } - if len(expected.Groups) != len(value.Groups) + fmt.Printf("%t\n", pattern.Test("https://example.com/books/123", "")) + fmt.Printf("%t\n", pattern.Test("https://example.com/authors/123", "")) + + fmt.Printf("%v", pattern.Exec("123", "https://example.com/books/").Pathname.Groups) + // Output: true + // false + // map[id:123] } -*/