Skip to content

Commit 8f52d81

Browse files
committed
update
1 parent 736b31a commit 8f52d81

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

json.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"strings"
78

89
"github.com/webx-top/com/encoding/json"
910
)
@@ -71,3 +72,44 @@ func Dump(m interface{}, args ...bool) (r string) {
7172
}
7273
return
7374
}
75+
76+
var jsonArrayReplacer = strings.NewReplacer(
77+
`[`, ``,
78+
`]`, ``,
79+
`"`, ``,
80+
`'`, ``,
81+
"`", ``,
82+
`\`, ``,
83+
)
84+
85+
func ListToJSONArray(list string, unique ...bool) string {
86+
items := strings.Split(list, `,`)
87+
result := make([]string, 0, len(items))
88+
if len(unique) > 0 && unique[0] {
89+
uniqMap := map[string]struct{}{}
90+
for _, item := range items {
91+
item = strings.TrimSpace(item)
92+
item = jsonArrayReplacer.Replace(item)
93+
if len(item) == 0 {
94+
continue
95+
}
96+
if _, ok := uniqMap[item]; ok {
97+
continue
98+
}
99+
result = append(result, item)
100+
uniqMap[item] = struct{}{}
101+
}
102+
list, _ = SetJSON(result)
103+
return list
104+
}
105+
for _, item := range items {
106+
item = strings.TrimSpace(item)
107+
item = jsonArrayReplacer.Replace(item)
108+
if len(item) == 0 {
109+
continue
110+
}
111+
result = append(result, item)
112+
}
113+
list, _ = SetJSON(result)
114+
return list
115+
}

0 commit comments

Comments
 (0)