-
Notifications
You must be signed in to change notification settings - Fork 3
/
operate.go
155 lines (127 loc) · 3.55 KB
/
operate.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
package collection
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
)
func (col *Collection[T]) Pluck(key string) map[string]T {
fields := map[string]T{}
for index, data := range col.ToArrayFields() {
var name, ok = data[key].(string)
if _, exists := fields[name]; ok && !exists {
fields[name] = col.array[index]
}
}
return fields
}
func (col *Collection[T]) GroupBy(key string) map[string][]T {
list := map[string][]T{}
for index, data := range col.ToArrayFields() {
var value, _ = data[key].(string)
list[value] = append(list[value], col.array[index])
}
return list
}
func (col *Collection[T]) Only(keys ...string) contracts.Collection[T] {
rawResults := make([]T, 0)
for index, data := range col.ToArrayFields() {
fields := contracts.Fields{}
for key, value := range data {
if utils.IsIn(key, keys) {
fields[key] = value
}
}
rawResults = append(rawResults, col.array[index])
}
return New(rawResults)
}
func (col *Collection[T]) First() *T {
if col.Count() == 0 {
return nil
}
return &col.array[0]
}
func (col *Collection[T]) Last() *T {
if col.Count() == 0 {
return nil
}
return &col.array[len(col.array)-1]
}
func (col *Collection[T]) Prepend(items ...T) contracts.Collection[T] {
return New(append(items, col.array...))
}
func (col *Collection[T]) Push(items ...T) contracts.Collection[T] {
return New(append(col.array, items...))
}
func (col *Collection[T]) Pull(defaultValue ...T) *T {
if result := col.Last(); result != nil {
col.array = col.array[:col.Count()-1]
return result
} else if len(defaultValue) > 0 {
return &defaultValue[0]
}
return nil
}
func (col *Collection[T]) Shift(defaultValue ...T) *T {
if result := col.First(); result != nil {
col.array = col.array[1:]
return result
} else if len(defaultValue) > 0 {
return &defaultValue[0]
}
return nil
}
func (col *Collection[T]) Offset(index int, item T) contracts.Collection[T] {
if col.Count() > index {
col.array[index] = item
return col
}
return col.Push(item)
}
func (col *Collection[T]) Put(index int, item T) contracts.Collection[T] {
if col.Count() > index {
return New(col.array).Offset(index, item)
}
return col.Push(item)
}
func (col *Collection[T]) Merge(collections ...contracts.Collection[T]) contracts.Collection[T] {
newCollection := New(col.array)
for _, collection := range collections {
newCollection = newCollection.Push(collection.ToArray()...)
}
return newCollection
}
func (col *Collection[T]) Reverse() contracts.Collection[T] {
newCollection := &Collection[T]{array: append(col.array)}
for from, to := 0, len(newCollection.array)-1; from < to; from, to = from+1, to-1 {
newCollection.array[from], newCollection.array[to] = newCollection.array[to], newCollection.array[from]
}
return newCollection
}
func (col *Collection[T]) Chunk(size int, handler func(collection contracts.Collection[T], page int) error) (err error) {
total := col.Count()
page := 1
for err == nil && (page-1)*size <= total {
offset := (page - 1) * size
endIndex := size + offset
if endIndex > total {
endIndex = total
}
newCollection := &Collection[T]{array: col.array[offset:endIndex]}
err = handler(newCollection, page)
page++
}
return
}
func (col *Collection[T]) Random(size ...uint) contracts.Collection[T] {
num := 1
if len(size) > 0 {
num = int(size[0])
}
newCollection := &Collection[T]{}
if col.Count() >= num {
for _, index := range utils.RandIntArray(0, col.Count()-1, num) {
newCollection.array = append(newCollection.array, col.array[index])
}
}
return newCollection
}