-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
86 lines (68 loc) · 1.5 KB
/
path.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package lib
import (
"os"
"path/filepath"
"strings"
)
type Path struct {
Path string
Separator string
}
const (
URLPathSeparator = "/"
)
func (p Path) PathSeparator() string {
if p.Separator == "" {
return string(os.PathSeparator)
}
return p.Separator
}
func (p Path) Pop() string {
_, last := filepath.Split(strings.TrimSuffix(p.Path, p.PathSeparator()))
return last
}
func (p Path) EndingSlash() bool {
if p.Path == "" {
return false
}
return p.Path[len(p.Path)-1:] == p.PathSeparator()
}
func (p Path) PruneStartingSlash() Path {
if p.Path == "" {
return p
}
if p.Path[0:1] == p.PathSeparator() {
return Path{Path: p.Path[1:]}
}
return p
}
func (p Path) PruneEndingSlash() Path {
if !p.EndingSlash() {
return Path{Path: p.Path}
}
return Path{Path: p.Path[0 : len(p.Path)-1]}
}
func (p Path) ConvertEmptyToRoot() Path {
if p.Path == "" {
return Path{Path: "."}
}
return p
}
func (p Path) Join(str ...string) Path {
return Path{Path: filepath.Join(p.String(), filepath.Join(str...))}
}
func (p Path) String() string {
return p.Path
}
func (p Path) NormalizePathSystemForAPI() Path {
return NewUrlPath(UrlJoinNoEscape(strings.Split(filepath.Clean(p.Path), p.PathSeparator())...)).PruneStartingSlash()
}
func (p Path) SwitchPathSeparator(separator string) Path {
return Path{Path: strings.Join(strings.Split(p.Path, p.PathSeparator()), separator), Separator: separator}
}
func NewUrlPath(path string) Path {
return Path{
Path: path,
Separator: URLPathSeparator,
}
}