-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub-path.go
41 lines (32 loc) 路 1.07 KB
/
sub-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
package nav
import (
"path/filepath"
"github.com/samber/lo"
)
// Tail extracts the end part of a string, starting from the offset
func Tail(input string, offset int) string {
asRunes := []rune(input)
if offset >= len(asRunes) {
return ""
}
return string(asRunes[offset:])
}
// difference returns the difference between a child path and a parent path
// Designed to be used with paths created from the file system rather than
// custom created or user provided input. For this reason, if there is no
// relationship between the parent and child paths provided then a panic
// may occur.
func difference(parent, child string) string {
return Tail(child, len(parent))
}
// RootItemSubPathHookFn
func RootItemSubPathHookFn(info *SubPathInfo) string {
return difference(info.Root, info.Item.Path)
}
// RootParentSubPathHookFn
func RootParentSubPathHookFn(info *SubPathInfo) string {
if info.Item.Extension.NodeScope == ScopeTopEn {
return lo.Ternary(info.Behaviour.KeepTrailingSep, string(filepath.Separator), "")
}
return difference(info.Root, info.Item.Extension.Parent)
}