-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
47 lines (40 loc) · 937 Bytes
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package pagepath
import "strings"
// Matcher : Check the identity between Pages.
type Matcher struct {
ignoreTrailingSlash bool
prepareFuncs []func(p *Page)
}
// Compare : For a given Elements, compares whether it matches the pages
func (m *Matcher) Compare(
target Page,
comparison Page,
elements ...Element,
) bool {
target = m.prepare(target)
comparison = m.prepare(comparison)
for _, element := range elements {
if target.Extract(element) != comparison.Extract(element) {
return false
}
}
return true
}
// prepare : Apply settings to page before compare
func (m *Matcher) prepare(p Page) Page {
if m.ignoreTrailingSlash {
p.Path = strings.TrimSuffix(p.Path, "/")
}
for _, prepareFunc := range m.prepareFuncs {
prepareFunc(&p)
}
return p
}
// NewMatcher : Generate Matcher
func NewMatcher(options ...func(m *Matcher)) *Matcher {
m := &Matcher{}
for _, opt := range options {
opt(m)
}
return m
}