pagepath

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2023 License: MIT Imports: 3 Imported by: 0

README

Go Report Card golangci-lint test

pagepath

pagepath is a Golang library for handling and matching page paths.

Usage

Get element from page path

import (
	"fmt"
	"log"

	"github.com/hirokisan/pagepath"
)

func main() {
	pagePath := "https://pkg.go.dev/net/url?xxx=bbb#Parse"

	host, err := pagepath.Extract(pagePath, pagepath.ElementHost)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(host) // pkg.go.dev
}

Compare pages, for given elements

import (
	"fmt"
	"log"

	"github.com/hirokisan/pagepath"
)

func main() {
	matcher := pagepath.NewMatcher()

	page, err := pagepath.New("https://pkg.go.dev/net/url?xxx=bbb#Parse")
	if err != nil {
		log.Fatal(err)
	}

	comparisonPage, err := pagepath.New("https://pkg.go.dev/net/url?xxx=bbb")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(matcher.Compare(page, comparisonPage, pagepath.ElementPath)) // true
}

Documentation

Index

Constants

View Source
const (
	// ElementScheme : scheme
	// e.g. http, https, ...
	ElementScheme = Element("scheme")
	// ElementHost : host
	// e.g. example.com, www.example.com, ...
	ElementHost = Element("host")
	// ElementPath : path
	// e.g. /aaa/bbb, /aaa/bbb/, ...
	ElementPath = Element("path")
	// ElementQuery : query
	// e.g. ?a=aa&b=bb, ...
	ElementQuery = Element("query")
	// ElementFragment : fragment
	// e.g. #xxx, ...
	ElementFragment = Element("fragment")
)

Variables

This section is empty.

Functions

func Extract

func Extract(
	pagePath string,
	element Element,
) (string, error)

Extract : Extracts the specified element from page path

func WithIgnoreTrailingSlash

func WithIgnoreTrailingSlash() func(m *Matcher)

WithIgnoreTrailingSlash :

func WithPrepareFuncs

func WithPrepareFuncs(fs ...func(p *Page)) func(m *Matcher)

WithPrepareFuncs :

Types

type Element

type Element string

Element : Components of page path

func Elements

func Elements() []Element

Elements : Reflects the sort order

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

Matcher : Check the identity between Pages.

func NewMatcher

func NewMatcher(options ...func(m *Matcher)) *Matcher

NewMatcher : Generate Matcher

func (*Matcher) Compare

func (m *Matcher) Compare(
	target Page,
	comparison Page,
	elements ...Element,
) bool

Compare : For a given Elements, compares whether it matches the pages

type Page

type Page struct {
	Scheme   string
	Host     string
	Path     string
	Query    string
	Fragment string
}

Page : Page that holds the Elements

func New

func New(pagePath string) (Page, error)

New :

func (*Page) Extract

func (p *Page) Extract(element Element) string

Extract : Extracts the specified element

func (*Page) String

func (p *Page) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL