-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
218 lines (179 loc) · 4.07 KB
/
array.go
File metadata and controls
218 lines (179 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package utils
import (
"errors"
"fmt"
"sort"
)
/**
Like Php Array method
ArrayKeysString(slices []ArrayMap, key string) []string
ArrayKeysInt64(slices []ArrayMap, key string) []int64
ArrayKeysInt(slices []ArrayMap, key string) []int
ArrayColumns(slices []ArrayMap, key string) []interface{}
ArrayColumnValue(slices []ArrayMap, key string, index string) (map[string]interface{}, error)
ArrayColumnValues(slices []ArrayMap, key string) (map[string]ArrayMap, error)
InArrayInt64(slice []int64, value int64) int
InArrayInt(slice []int, value int) int
InArrayString(slice []string, value string) int
RemoveRepeatedInt(values []int) []int
RemoveRepeatedInt64(values []int64) []int64
RemoveRepeatedString(values []string) []string
*/
var ArrayMapKexNotExists = "key not exists"
var ArrayMapKexIsEmpty = "slice empty"
type ArrayMap map[string]interface{}
func (am ArrayMap) ExistsKey(key string) bool {
_, ok := am[key]
return ok
}
func ArrayKeysString(slices []ArrayMap, key string) []string {
if len(slices) == 0 {
return nil
}
res := make([]string, 0)
for _, v := range slices {
if v.ExistsKey(key) {
res = append(res, Interface2String(v[key]))
}
}
return res
}
func ArrayKeysInt64(slices []ArrayMap, key string) []int64 {
if len(slices) == 0 {
return nil
}
res := make([]int64, 0)
for _, v := range slices {
if v.ExistsKey(key) {
res = append(res, Interface2Int64(v[key]))
}
}
return res
}
func ArrayKeysInt(slices []ArrayMap, key string) []int {
if len(slices) == 0 {
return nil
}
res := make([]int, 0)
for _, v := range slices {
if v.ExistsKey(key) {
res = append(res, Interface2Int(v[key]))
}
}
return res
}
func ArrayColumns(slices []ArrayMap, key string) []interface{} {
if len(slices) == 0 {
return nil
}
res := make([]interface{}, 0)
for _, v := range slices {
if v.ExistsKey(key) {
res = append(res, v[key])
}
}
return res
}
func ArrayColumnValue(slices []ArrayMap, key string, index string) (map[string]interface{}, error) {
if len(slices) == 0 {
return nil, errors.New(ArrayMapKexIsEmpty)
}
res := make(map[string]interface{})
for i, v := range slices {
if !v.ExistsKey(key) {
return nil, errors.New(fmt.Sprintf(ArrayMapKexNotExists+", key %d ", i))
}
if !v.ExistsKey(index) {
return nil, errors.New(fmt.Sprintf(ArrayMapKexNotExists+", index %d ", i))
}
res[Interface2String(v[index])] = v[key]
}
return res, nil
}
func ArrayColumnValues(slices []ArrayMap, key string) (map[string]ArrayMap, error) {
if len(slices) == 0 {
return nil, errors.New(ArrayMapKexIsEmpty)
}
res := make(map[string]ArrayMap)
for i, v := range slices {
if !v.ExistsKey(key) {
return nil, errors.New(fmt.Sprintf(ArrayMapKexNotExists+", key %d ", i))
}
res[Interface2String(v[key])] = v
}
return res, nil
}
func InArrayInt64(slice []int64, value int64) int {
for i, v := range slice {
if v == value {
return i
}
}
return -1
}
func InArrayInt(slice []int, value int) int {
for i, v := range slice {
if v == value {
return i
}
}
return -1
}
func InArrayString(slice []string, value string) int {
for i, v := range slice {
if v == value {
return i
}
}
return -1
}
func RemoveRepeatedString(values []string) []string {
res := make([]string, 0)
sort.Strings(values)
for i := 0; i < len(values); i++ {
repeat := false
for j := i + 1; j < len(values); j++ {
if values[i] == values[j] {
repeat = true
break
}
}
if !repeat {
res = append(res, values[i])
}
}
return res
}
func RemoveRepeatedInt64(arr []int64) (newArr []int64) {
newArr = make([]int64, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return
}
func RemoveRepeatedInt(values []int) []int {
res := make([]int, 0)
sort.Ints(values)
for i := 0; i < len(values); i++ {
repeat := false
for j := i + 1; j < len(values); j++ {
if values[i] == values[j] {
repeat = true
break
}
}
if !repeat {
res = append(res, values[i])
}
}
return res
}