Skip to content

Commit ced326e

Browse files
committed
feat: type/maputil: add MapStringSlice.FlattenJoin()
1 parent bf5c98f commit ced326e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

type/maputil/map_string_slice.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package maputil
22

33
import (
44
"net/url"
5+
"slices"
56
"sort"
67
"strings"
78

@@ -76,6 +77,21 @@ func (mss MapStringSlice) FlattenAny(useLast, skipEmpty bool) map[string]any {
7677
return msa
7778
}
7879

80+
func (mss MapStringSlice) FlattenJoin(dedupe, sortAsc bool, sep string) map[string]string {
81+
out := map[string]string{}
82+
for k, vs := range mss {
83+
vs2 := slices.Clone(vs)
84+
if dedupe {
85+
vs2 = slicesutil.Dedupe(vs2)
86+
}
87+
if sortAsc {
88+
sort.Strings(vs2)
89+
}
90+
out[k] = strings.Join(vs2, sep)
91+
}
92+
return out
93+
}
94+
7995
func (mss MapStringSlice) Keys() []string {
8096
return StringKeys(mss, nil)
8197
}

0 commit comments

Comments
 (0)