Skip to content

Commit

Permalink
feat: improve public API and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Sep 15, 2024
1 parent 342fbe4 commit c525520
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 62 deletions.
1 change: 0 additions & 1 deletion FUNDING.yml

This file was deleted.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 0 additions & 16 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions pull_request_template.md

This file was deleted.

14 changes: 9 additions & 5 deletions urlpattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions urlpattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -160,15 +159,15 @@ 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) {
case string:
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.")
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
}
*/

0 comments on commit c525520

Please sign in to comment.