-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.go
358 lines (330 loc) · 10.1 KB
/
array.go
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package lodash
import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
)
func Chunk(output interface{}, input interface{}, sliceNum int) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
if sliceNum <= 0 {
return errors.New(`Chunk args.sliceNum must > 0`)
}
err = CheckKindErr(`Chunk`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
groupNum := int(math.Ceil(float64(inputRv.Len()) / float64(sliceNum)))
if isChain {
result := []interface{}{}
for i := 0; i < groupNum; i++ {
endIndex := (i + 1) * sliceNum
if endIndex > inputRv.Len() {
endIndex = inputRv.Len()
}
result = append(result, inputRv.Slice((i*sliceNum), endIndex).Interface())
}
input.(*lodash).input = result
} else {
outputSet := reflect.ValueOf(output).Elem()
result := reflect.ValueOf(output).Elem()
for i := 0; i < groupNum; i++ {
endIndex := (i + 1) * sliceNum
if endIndex > inputRv.Len() {
endIndex = inputRv.Len()
}
result = reflect.Append(result, inputRv.Slice((i*sliceNum), endIndex))
}
outputSet.Set(result)
}
return nil
}
func Concat(output interface{}, inputs ...interface{}) (err error) {
_, isChain := chainArgConvert(output)
if !isChain {
if kind := reflect.ValueOf(output).Kind(); kind != reflect.Ptr {
return errors.New(`Concat args.output must be pointer`)
}
}
for _, input := range inputs {
if kind := reflect.ValueOf(input).Kind(); kind != reflect.Slice {
return errors.New(`Concat args.input must be slice`)
}
}
if isChain {
result := []interface{}{}
for _, input := range inputs {
inputReflect := reflect.ValueOf(input)
for i := 0; i < inputReflect.Len(); i++ {
result = append(result, inputReflect.Index(i).Interface())
}
}
output.(*lodash).input = result
} else {
outputSet := reflect.ValueOf(output).Elem()
result := reflect.ValueOf(output).Elem()
for _, input := range inputs {
inputReflect := reflect.ValueOf(input)
for i := 0; i < inputReflect.Len(); i++ {
result = reflect.Append(result, inputReflect.Index(i))
}
}
outputSet.Set(result)
}
return nil
}
func Difference(output interface{}, input interface{}, accessory interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
if kind := reflect.ValueOf(accessory).Kind(); kind != reflect.Slice {
return errors.New(`Difference args.accessory must be slice`)
}
err = CheckKindErr(`Difference`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
if isChain {
result := []interface{}{}
for i := 0; i < inputRv.Len(); i++ {
if !Includes(accessory, inputRv.Index(i).Interface()) {
result = append(result, inputRv.Index(i).Interface())
}
}
input.(*lodash).input = result
} else {
outputSet := reflect.ValueOf(output).Elem()
result := reflect.ValueOf(output).Elem()
for i := 0; i < inputRv.Len(); i++ {
if !Includes(accessory, inputRv.Index(i).Interface()) {
result = reflect.Append(result, inputRv.Index(i))
}
}
outputSet.Set(result)
}
return nil
}
func First(output interface{}, input interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
if length := inputRv.Len(); length == 0 {
return errors.New(`First args.input is empty`)
}
err = CheckKindErr(`First`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
chainOutputNoSlice(output, input, isChain, inputRv.Index(0).Interface())
return nil
}
func Last(output interface{}, input interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
if length := inputRv.Len(); length == 0 {
return errors.New(`First args.input is empty`)
}
err = CheckKindErr(`Last`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
chainOutputNoSlice(output, input, isChain, inputRv.Index(inputRv.Len()-1).Interface())
return nil
}
func flattenDepth(array interface{}, result *[]interface{}, level int) {
level -= 1
arrayRv := reflect.ValueOf(array)
if !Includes(ReflectArrayTypes, arrayRv.Kind().String()) {
arrayRv = arrayRv.Elem()
}
for i := 0; i < arrayRv.Len(); i++ {
kind := arrayRv.Index(i).Kind().String()
if kind == `interface` {
kind = arrayRv.Index(i).Elem().Kind().String()
}
if level > 0 && Includes(ReflectArrayTypes, kind) {
flattenDepth(arrayRv.Index(i).Interface(), result, level)
} else {
*result = append(*result, arrayRv.Index(i).Interface())
}
}
return
}
func flattenForever(array interface{}, result *[]interface{}) {
arrayRv := reflect.ValueOf(array)
if !Includes(ReflectArrayTypes, arrayRv.Kind().String()) {
arrayRv = arrayRv.Elem()
}
for i := 0; i < arrayRv.Len(); i++ {
kind := arrayRv.Index(i).Kind().String()
if kind == `interface` {
kind = arrayRv.Index(i).Elem().Kind().String()
}
if Includes(ReflectArrayTypes, kind) {
flattenForever(arrayRv.Index(i).Interface(), result)
} else {
*result = append(*result, arrayRv.Index(i).Interface())
}
}
return
}
// if level <= 0, flatten forever.
func Flatten(output interface{}, input interface{}, level int) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
err = CheckKindErr(`Flatten`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
result := []interface{}{}
if level <= 0 {
flattenForever(useInput, &result)
} else {
flattenDepth(useInput, &result, level+1)
}
err = chainOutputConvert(output, input, isChain, result)
return err
}
func Uniq(output interface{}, input interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
err = CheckKindErr(`Uniq`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
if isChain {
// 利用map的key唯一,实现排重
uniqMaps := map[string]interface{}{}
for i := 0; i < inputRv.Len(); i++ {
uniqKey := fmt.Sprintf(`%+v`, inputRv.Index(i).Interface())
uniqMaps[uniqKey] = inputRv.Index(i).Interface()
}
uniqList := []interface{}{}
for _, v := range uniqMaps {
uniqList = append(uniqList, v)
}
input.(*lodash).input = uniqList
} else {
outputSet := reflect.ValueOf(output).Elem()
uniqMaps := map[string]reflect.Value{}
for i := 0; i < inputRv.Len(); i++ {
uniqKey := fmt.Sprintf(`%+v`, inputRv.Index(i).Interface())
uniqMaps[uniqKey] = inputRv.Index(i)
}
uniqList := reflect.ValueOf(output).Elem()
for _, v := range uniqMaps {
uniqList = reflect.Append(uniqList, v)
}
outputSet.Set(uniqList)
}
return nil
}
func UniqBy(output interface{}, input interface{}, iteratee func(interface{}) interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
err = CheckKindErr(`UniqBy`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
if isChain {
// 利用map的key唯一,实现排重
uniqMaps := map[string]interface{}{}
for i := 0; i < inputRv.Len(); i++ {
uniqKey := fmt.Sprintf(`%+v`, iteratee(inputRv.Index(i).Interface()))
uniqMaps[uniqKey] = inputRv.Index(i).Interface()
}
uniqList := []interface{}{}
for _, v := range uniqMaps {
uniqList = append(uniqList, v)
}
input.(*lodash).input = uniqList
} else {
outputSet := reflect.ValueOf(output).Elem()
uniqMaps := map[string]reflect.Value{}
for i := 0; i < inputRv.Len(); i++ {
uniqKey := fmt.Sprintf(`%+v`, iteratee(inputRv.Index(i).Interface()))
uniqMaps[uniqKey] = inputRv.Index(i)
}
uniqList := reflect.ValueOf(output).Elem()
for _, v := range uniqMaps {
uniqList = reflect.Append(uniqList, v)
}
outputSet.Set(uniqList)
}
return nil
}
func Union(output interface{}, inputs ...interface{}) {
Chain(inputs[0]).Concat(inputs[1:]...).Uniq().Value(output)
}
func UnionBy(output interface{}, iteratee func(interface{}) interface{}, inputs ...interface{}) {
Chain(inputs[0]).Concat(inputs[1:]...).UniqBy(iteratee).Value(output)
}
func IndexOf(input interface{}, iteratee func(interface{}) bool) int {
useInput, _ := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
index := -1
for i := 0; i < inputRv.Len(); i++ {
if iteratee(inputRv.Index(i).Interface()) {
return i
}
}
return index
}
func LastIndexOf(input interface{}, iteratee func(interface{}) bool) int {
useInput, _ := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
index := -1
for i := 0; i < inputRv.Len(); i++ {
if iteratee(inputRv.Index(i).Interface()) {
index = i
}
}
return index
}
func Join(output interface{}, input interface{}, joinStr string) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
err = CheckKindErr(`Join`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
str := ``
for i := 0; i < inputRv.Len(); i++ {
kind := inputRv.Index(i).Kind().String()
if Includes(ReflectIntTypes, kind) {
str = str + strconv.Itoa(int(inputRv.Index(i).Int()))
} else if Includes(ReflectUIntTypes, kind) {
str = str + strconv.Itoa(int(inputRv.Index(i).Uint()))
} else {
str = str + inputRv.Index(i).String()
}
if i != inputRv.Len()-1 {
str += joinStr
}
}
chainOutputNoSlice(output, input, isChain, str)
return nil
}
func Reverse(output interface{}, input interface{}) (err error) {
useInput, isChain := chainArgConvert(input)
inputRv := reflect.ValueOf(useInput)
err = CheckKindErr(`Reverse`, isChain, reflect.ValueOf(output).Kind().String(), inputRv.Kind().String())
if err != nil {
return err
}
if isChain {
result := []interface{}{}
for i := 1; i <= inputRv.Len(); i++ {
result = append(result, inputRv.Index(inputRv.Len()-i).Interface())
}
input.(*lodash).input = result
} else {
outputSet := reflect.ValueOf(output).Elem()
result := reflect.ValueOf(output).Elem()
for i := 1; i <= inputRv.Len(); i++ {
result = reflect.Append(result, inputRv.Index(inputRv.Len()-i))
}
outputSet.Set(result)
}
return nil
}