-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
45 lines (38 loc) · 954 Bytes
/
id.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
package kanvas
import (
"path/filepath"
"strings"
)
// SiblingID calculates the ID of a component relative to the parent of the caller.
// If the caller is empty, the component is resolved relative to the root.
// If the caller is non-empty,
// the component is resolved relative to the parent of the caller.
func SiblingID(caller, component string) string {
if component == "" {
return ""
}
if component[0] == '/' {
return normalize(component)
}
if caller == "" {
return normalize("/" + component)
}
return normalize(filepath.Join(filepath.Dir(caller), component))
}
func ID(components ...string) string {
var ns []string
for i, n := range components {
if n == "" {
continue
} else if n[0] == '/' {
return normalize(n)
} else if i == 0 {
n = "/" + n
}
ns = append(ns, normalize(n))
}
return strings.Join(ns, "/")
}
func normalize(n string) string {
return strings.ToLower(strings.ReplaceAll(n, " ", "-"))
}