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

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