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.

51 lines
1.0 KiB

2 years ago
  1. package formattools
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. )
  7. func CompactIDList(ids []string) []string {
  8. sort.Strings(ids)
  9. currentGroup := ""
  10. currentIDs := make([]string, 0, len(ids))
  11. groups := make([]string, 0, 2)
  12. for _, id := range ids {
  13. if id == "" {
  14. continue
  15. }
  16. split := strings.SplitN(id, ":", 3)
  17. if len(split) < 3 {
  18. continue
  19. }
  20. group := id[:len(split[0])+len(split[1])+1]
  21. if group != currentGroup {
  22. if len(currentIDs) > 0 {
  23. if len(currentIDs) == 1 {
  24. groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
  25. } else {
  26. groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
  27. }
  28. currentIDs = currentIDs[:0]
  29. }
  30. currentGroup = group
  31. }
  32. currentIDs = append(currentIDs, split[2])
  33. }
  34. if len(currentIDs) > 0 {
  35. if len(currentIDs) == 1 {
  36. groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
  37. } else {
  38. groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
  39. }
  40. }
  41. return groups
  42. }