|
4 | 4 | "fmt"
|
5 | 5 | "io"
|
6 | 6 | "os"
|
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/webx-top/com/encoding/json"
|
9 | 10 | )
|
@@ -71,3 +72,44 @@ func Dump(m interface{}, args ...bool) (r string) {
|
71 | 72 | }
|
72 | 73 | return
|
73 | 74 | }
|
| 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