more tinkering.

This commit is contained in:
2022-08-14 13:57:56 +02:00
parent e752b1bafd
commit 15a4f3289f
16 changed files with 331 additions and 237 deletions
+51
View File
@@ -0,0 +1,51 @@
package formattools
import (
"fmt"
"sort"
"strings"
)
func CompactIDList(ids []string) []string {
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
}