-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_example_test.go
501 lines (391 loc) · 9.52 KB
/
collection_example_test.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package collection_test
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/wilhelm-murdoch/go-collection"
)
func ExampleCollection_Sort_numeric() {
numbers := collection.New(1, 4, 2, 3)
fmt.Println("Unsorted:")
numbers.Each(func(i, n int) bool {
fmt.Print(" ", n)
return false
})
fmt.Println("\nSorted:")
numbers.Sort(func(i, j int) bool {
left, _ := numbers.At(i)
right, _ := numbers.At(j)
return left < right
}).Each(func(i, n int) bool {
fmt.Print(" ", n)
return false
})
// Output:
// Unsorted:
// 1 4 2 3
// Sorted:
// 1 2 3 4
}
func ExampleCollection_Batch() {
type Job struct {
Timestamp int64
Processed bool
}
jobs := make([]*Job, 0)
for i := 1; i <= 100; i++ {
jobs = append(jobs, &Job{time.Now().UnixNano(), false})
}
c1 := collection.New(jobs...)
c1.Batch(func(b, j int, job *Job) {
job.Processed = true
}, 5)
processed := c1.All(func(i int, job *Job) bool {
return job.Processed == true
})
fmt.Printf("processed %d/%d jobs:%v\n", c1.Length(), c1.Length(), processed)
// Output:
// processed 100/100 jobs:true
}
func ExampleCollection_Sort_alpha() {
names := collection.New("wilhelm", "peter", "josh", "luke", "rob")
fmt.Println("Unsorted:")
fmt.Println(strings.Join(names.Items(), ","))
names.Sort(func(i, j int) bool {
left, _ := names.At(i)
right, _ := names.At(j)
return left < right
})
fmt.Println("Sorted:")
fmt.Println(strings.Join(names.Items(), ","))
// Output:
// Unsorted:
// wilhelm,peter,josh,luke,rob
// Sorted:
// josh,luke,peter,rob,wilhelm
}
func ExampleNew() {
fruits := collection.New("apple", "orange", "strawberry", "cherry", "banana", "apricot")
fmt.Println("Fruits:", fruits.Length())
fruits.Each(func(index int, item string) bool {
fmt.Println("-", item)
return false
})
// Output:
// Fruits: 6
// - apple
// - orange
// - strawberry
// - cherry
// - banana
// - apricot
}
func ExampleCollection_Slice() {
collection.New("apple", "orange", "strawberry").Slice(0, 2).Each(func(i int, item string) bool {
fmt.Println(item)
return false
})
// Output:
// apple
// orange
}
func ExampleCollection_Items() {
c := collection.New("apple", "orange", "strawberry")
for i, item := range c.Items() {
fmt.Println(i, item)
}
// Output:
// 0 apple
// 1 orange
// 2 strawberry
}
func ExampleCollection_PushDistinct() {
c := collection.New("apple", "orange", "strawberry")
c.PushDistinct("orange", "orange", "watermelon")
c.Each(func(index int, item string) bool {
fmt.Println(item)
return false
})
// Output:
// apple
// orange
// strawberry
// watermelon
}
func ExampleCollection_Concat() {
collection.New("apple", "orange", "strawberry").Concat([]string{"dog", "cat", "horse"}).Each(func(index int, item string) bool {
fmt.Println(item)
return false
})
// Output:
// apple
// orange
// strawberry
// dog
// cat
// horse
}
func ExampleCollection_Filter() {
c := collection.New("apple", "orange", "strawberry").Filter(func(item string) bool {
return item == "apple"
})
c.Each(func(index int, item string) bool {
fmt.Println(item)
return false
})
// Output:
// apple
}
func ExampleCollection_Contains() {
fmt.Println(collection.New("apple", "orange", "strawberry").Contains("horse"))
// Output:
// false
}
func ExampleCollection_ContainsBy() {
type Person struct {
Name string
Age int
}
people := []Person{
{"wilhelm", 31},
{"luke", 42},
{"rob", 17},
{"peter", 26},
{"josh", 26},
}
fmt.Println(collection.New(people...).ContainsBy(func(i int, p Person) bool {
return p.Age < 20
}))
// Output:
// true
}
func ExampleCollection_Shift() {
fmt.Println(collection.New("apple", "orange", "strawberry").Shift())
// Output:
// apple
}
func ExampleCollection_Unshift() {
c := collection.New("apple", "orange", "strawberry")
fmt.Println("Length Current:", c.Length())
fmt.Println("Length New: ", c.Unshift("horse"))
c.Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// Length Current: 3
// Length New: 4
// 0 horse
// 1 apple
// 2 orange
// 3 strawberry
}
func ExampleCollection_At() {
item, ok := collection.New("apple", "orange", "strawberry").At(1)
fmt.Println(item, ok)
// Output:
// orange true
}
func ExampleCollection_IsEmpty() {
c := collection.New("lonely")
fmt.Println(c.IsEmpty())
c.Empty()
fmt.Println(c.IsEmpty())
// Output:
// false
// true
}
func ExampleCollection_Empty() {
fmt.Println(collection.New("apple", "orange", "strawberry").Empty().Length())
// Output:
// 0
}
func ExampleCollection_Find() {
fmt.Println(collection.New("apple", "orange", "strawberry").Find(func(i int, item string) bool {
return item == "orange"
}))
// Output:
// orange
}
func ExampleCollection_FindIndex() {
fmt.Println(collection.New("apple", "orange", "strawberry").FindIndex(func(i int, item string) bool {
return item == "orange"
}))
// Output:
// 1
}
func ExampleCollection_Random() {
item, ok := collection.New("apple", "orange", "strawberry").Random()
if ok {
fmt.Println("My random item is:", item)
}
}
func ExampleCollection_RandomIndex() {
index := collection.New("apple", "orange", "strawberry").RandomIndex()
fmt.Println("My random index is:", index)
}
func ExampleCollection_LastIndexOf() {
fmt.Println(collection.New("apple", "orange", "orange", "strawberry").LastIndexOf("orange"))
// Output:
// 2
}
func ExampleCollection_Reduce() {
acc := collection.New("apple", "orange", "strawberry").Reduce(func(i int, item, accumulator string) string {
return accumulator + item
})
fmt.Println(acc)
// Output:
// appleorangestrawberry
}
func ExampleCollection_Reverse() {
collection.New("apple", "orange", "orange", "strawberry").Reverse().Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 strawberry
// 1 orange
// 2 orange
// 3 apple
}
func ExampleCollection_Some() {
found := collection.New("apple", "orange", "strawberry").Some(func(i int, item string) bool {
return item == "orange"
})
fmt.Println("Found \"orange\"?", found)
// Output:
// Found "orange"? true
}
func ExampleCollection_None() {
found := collection.New("apple", "orange", "strawberry").Some(func(i int, item string) bool {
return item == "blackberry"
})
fmt.Println("Found \"blackberry\"?", found)
// Output:
// Found "blackberry"? false
}
func ExampleCollection_All() {
c := collection.New("apple", "orange", "strawberry")
fmt.Println("Contains all items?", c.All(func(i int, item string) bool {
return c.Contains(item)
}))
// Output:
// Contains all items? true
}
func ExampleCollection_Push() {
c := collection.New("apple", "orange", "strawberry")
fmt.Println("Collection Length:", c.Push("blueberry", "watermelon"))
c.Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// Collection Length: 5
// 0 apple
// 1 orange
// 2 strawberry
// 3 blueberry
// 4 watermelon
}
func ExampleCollection_Pop() {
item, ok := collection.New("apple", "orange", "strawberry").Pop()
fmt.Println(item, ok)
// Output:
// strawberry true
}
func ExampleCollection_Length() {
fmt.Println("Collection Length:", collection.New("apple", "orange", "strawberry").Length())
// Output:
// Collection Length: 3
}
func ExampleCollection_Map() {
c := collection.New("apple", "orange", "strawberry").Map(func(i int, item string) string {
return fmt.Sprintf("The %s is yummo!", item)
})
c.Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 The apple is yummo!
// 1 The orange is yummo!
// 2 The strawberry is yummo!
}
func ExampleCollection_Each() {
collection.New("apple", "orange", "strawberry").Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 apple
// 1 orange
// 2 strawberry
}
func ExampleCollection_InsertAt() {
collection.New("apple", "orange", "strawberry").InsertAt("banana", 2).Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 apple
// 1 orange
// 2 banana
// 3 strawberry
}
func ExampleCollection_InsertBefore() {
collection.New("apple", "orange", "strawberry").InsertBefore("banana", 2).Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 apple
// 1 banana
// 2 orange
// 3 strawberry
}
func ExampleCollection_InsertAfter() {
collection.New("apple", "orange", "strawberry").InsertAfter("banana", 1).Each(func(i int, item string) bool {
fmt.Println(i, item)
return false
})
// Output:
// 0 apple
// 1 orange
// 2 banana
// 3 strawberry
}
func ExampleCollection_AtLast() {
last, ok := collection.New("apple", "orange", "strawberry").AtLast()
fmt.Println(last, ok)
// Output:
// strawberry true
}
func ExampleCollection_AtFirst() {
first, ok := collection.New("apple", "orange", "strawberry").AtFirst()
fmt.Println(first, ok)
// Output:
// apple true
}
func ExampleCollection_Count() {
count := collection.New("apple", "orange", "orange", "strawberry").Count("orange")
fmt.Println("Orange Count:", count)
// Output:
// Orange Count: 2
}
func ExampleCollection_CountBy() {
count := collection.New("apple", "orange", "strawberry", "blueberry").CountBy(func(item string) bool {
return strings.HasSuffix(item, "berry")
})
fmt.Println("Berry Types:", count)
// Output:
// Berry Types: 2
}
func ExampleCollection_MarshalJSON() {
var buffer strings.Builder
encoder := json.NewEncoder(&buffer)
encoder.Encode(collection.New("apple", "orange", "strawberry"))
fmt.Println(buffer.String())
// Output:
// ["apple","orange","strawberry"]
}