-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections-query.go
262 lines (210 loc) · 5.36 KB
/
collections-query.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
package minidb
import (
"errors"
"strings"
)
// Remove removes the first element that is equal to v. It wraps around RemoveMany().
func (c *MiniCollections) Remove(v interface{}) error {
return c.RemoveMany(v, 1)
}
// RemoveAll removes all elements that are equal to v. It wraps around RemoveMany().
func (c *MiniCollections) RemoveAll(v interface{}) error {
return c.RemoveMany(v, -1)
}
// RemoveMany removes the number of elements corresponding to l. Use RemoveAll() for removing
// all elements and Remove() for a single element.
// `l` cannot be less than -1 or equal to 0.
func (c *MiniCollections) RemoveMany(v interface{}, l int) error {
if l < -1 || l == 0 {
return errors.New("l cannot be less than -1 or equal to 0")
}
d := c.getOrCreateMutex(len(c.content) + l)
d.Lock()
defer d.Unlock()
values := []interface{}{}
removed := []interface{}{}
for index, x := range c.content {
// -1 means remove all similar values
if l > 0 {
// check if total is similar
if len(removed) == l {
values = append(values, c.content[index:]...)
break
}
}
// if x is same with v, append it to removed
// otherwise, to the new values
if v == x {
removed = append(removed, x)
} else {
values = append(values, x)
}
}
// write new values
c.content = values
c.writeToDB()
return nil
}
// Push adds an item to the store slice.
func (c *MiniCollections) Push(v ...interface{}) {
d := c.getOrCreateMutex(len(c.content) + 1)
d.Lock()
defer d.Unlock()
c.content = append(c.content, v...)
c.writeToDB()
}
// List returns all of the contents of the collection
func (c *MiniCollections) List() []interface{} {
return c.content
}
// First returns the first element of the collections.
func (c *MiniCollections) First() interface{} {
return c.content[0]
}
// Last returns the last element of the collections.
func (c *MiniCollections) Last() interface{} {
return c.content[len(c.content)-1]
}
// Find attemps to find the value from the store and returns it's index.
// If it doesn't exist, it will return -1.
func (c *MiniCollections) Find(v interface{}) int {
for index, value := range c.content {
if value == v {
return index
}
}
return -1
}
// FindAll returns all values that matches v.
func (c *MiniCollections) FindAll(v interface{}) []interface{} {
values := []interface{}{}
for _, value := range c.content {
if value == v {
values = append(values, value)
}
}
return values
}
// MatchString returns the first element that contains v.
func (c *MiniCollections) MatchString(v string) (string, error) {
for _, value := range c.content {
s, ok := value.(string)
if ok {
if strings.Contains(s, v) {
return s, nil
}
}
}
return "", nil
}
// MatchStringAll returns the first element that contains v.
func (c *MiniCollections) MatchStringAll(v string) ([]string, error) {
values := []string{}
for _, value := range c.content {
s, ok := value.(string)
if ok {
if strings.Contains(s, v) {
values = append(values, s)
}
}
}
return values, nil
}
// FilterString returns all string elements.
func (c *MiniCollections) FilterString() []string {
values := []string{}
for _, i := range c.content {
if v, ok := i.(string); ok {
values = append(values, v)
}
}
return values
}
// FilterInt returns all int elements.
func (c *MiniCollections) FilterInt() []int {
values := []int{}
for _, i := range c.content {
if v, ok := i.(int); ok {
values = append(values, v)
}
}
return values
}
// FilterBool returns all bool elements.
func (c *MiniCollections) FilterBool() []bool {
values := []bool{}
for _, i := range c.content {
if v, ok := i.(bool); ok {
values = append(values, v)
}
}
return values
}
// FilterFloat returns all float elements (uses float64). Some integers (int) might be included.
func (c *MiniCollections) FilterFloat() []float64 {
values := []float64{}
for _, i := range c.content {
if v, ok := i.(float64); ok {
values = append(values, v)
}
}
return values
}
// Filter accepts a function that return the values that returns true with it. // TODO
func (c *MiniCollections) Filter(f func(interface{}) bool) []interface{} {
values := []interface{}{}
for _, i := range c.content {
if f(i) {
values = append(values, i)
}
}
return values
}
// FilterStringFunc returns all string elements.
func (c *MiniCollections) FilterStringFunc(f func(x string) bool) []string {
values := []string{}
for _, i := range c.content {
if v, ok := i.(string); ok {
if f(v) {
values = append(values, v)
}
}
}
return values
}
// FilterIntFunc returns all int elements.
func (c *MiniCollections) FilterIntFunc(f func(x int) bool) []int {
values := []int{}
for _, i := range c.content {
if v, ok := i.(int); ok {
if f(v) {
values = append(values, v)
}
}
}
return values
}
// FilterBoolFunc returns all bool elements.
func (c *MiniCollections) FilterBoolFunc(f func(x bool) bool) []bool {
values := []bool{}
for _, i := range c.content {
if v, ok := i.(bool); ok {
if f(v) {
values = append(values, v)
}
}
}
return values
}
// FilterFloatFunc returns all float elements (uses float64). Some integers (int) might be included.
func (c *MiniCollections) FilterFloatFunc(f func(x float64) bool) []float64 {
values := []float64{}
for _, i := range c.content {
if v, ok := i.(float64); ok {
if f(v) {
values = append(values, v)
}
}
}
return values
}