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
|
package state
import (
"strings"
"github.com/bradenaw/juniper/xslices"
"golang.org/x/exp/slices"
)
// listSuperiors returns all names superior to the given name, if hierarchies are indicated with the given delimiter.
func listSuperiors(name, delimiter string) []string {
if delimiter == "" {
return nil
}
split := strings.Split(name, delimiter)
if len(split) == 0 {
return nil
}
var inferiors []string
for i := range split {
if i == 0 {
continue
}
inferiors = append(inferiors, strings.Join(split[0:i], delimiter))
}
return inferiors
}
func listInferiors(parent, delimiter string, names []string) []string {
inferiors := xslices.Filter(names, func(name string) bool {
return slices.Contains(listSuperiors(name, delimiter), parent)
})
slices.Sort(inferiors)
xslices.Reverse(inferiors)
return inferiors
}
|