You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

52 lines
1.1 KiB

package formattools
import (
"fmt"
"sort"
"strings"
)
func CompactIDList(ids []string) []string {
ids = append(ids[:0:0], ids...)
sort.Strings(ids)
currentGroup := ""
currentIDs := make([]string, 0, len(ids))
groups := make([]string, 0, 2)
for _, id := range ids {
if id == "" {
continue
}
split := strings.SplitN(id, ":", 3)
if len(split) < 3 {
continue
}
group := id[:len(split[0])+len(split[1])+1]
if group != currentGroup {
if len(currentIDs) > 0 {
if len(currentIDs) == 1 {
groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
} else {
groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
}
currentIDs = currentIDs[:0]
}
currentGroup = group
}
currentIDs = append(currentIDs, split[2])
}
if len(currentIDs) > 0 {
if len(currentIDs) == 1 {
groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
} else {
groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
}
}
return groups
}