Skip to content

Commit 55a6cf3

Browse files
committed
feat: return nil for slice operations that received nil
1 parent d93dd9a commit 55a6cf3

File tree

2 files changed

+231
-59
lines changed

2 files changed

+231
-59
lines changed

slice.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
1111
// Play: https://go.dev/play/p/Apjg3WeSi7K
1212
func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice {
13+
if collection == nil {
14+
return nil
15+
}
16+
1317
result := make(Slice, 0, len(collection))
1418

1519
for i := range collection {
@@ -24,6 +28,10 @@ func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index in
2428
// Map manipulates a slice and transforms it to a slice of another type.
2529
// Play: https://go.dev/play/p/OkPcYAhBo0D
2630
func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
31+
if collection == nil {
32+
return nil
33+
}
34+
2735
result := make([]R, len(collection))
2836

2937
for i := range collection {
@@ -40,6 +48,10 @@ func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
4048
//
4149
// Play: https://go.dev/play/p/-AuYXfy7opz
4250
func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
51+
if collection == nil {
52+
return nil
53+
}
54+
4355
result := []R{}
4456

4557
for i := range collection {
@@ -56,6 +68,10 @@ func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R
5668
// no value is added to the final slice.
5769
// Play: https://go.dev/play/p/YSoYmQTA8-U
5870
func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R {
71+
if collection == nil {
72+
return nil
73+
}
74+
5975
result := make([]R, 0, len(collection))
6076

6177
for i := range collection {
@@ -111,6 +127,10 @@ func Times[T any](count int, iteratee func(index int) T) []T {
111127
// The order of result values is determined by the order they occur in the array.
112128
// Play: https://go.dev/play/p/DTzbeXZ6iEN
113129
func Uniq[T comparable, Slice ~[]T](collection Slice) Slice {
130+
if collection == nil {
131+
return nil
132+
}
133+
114134
result := make(Slice, 0, len(collection))
115135
seen := make(map[T]struct{}, len(collection))
116136

@@ -131,6 +151,10 @@ func Uniq[T comparable, Slice ~[]T](collection Slice) Slice {
131151
// invoked for each element in array to generate the criterion by which uniqueness is computed.
132152
// Play: https://go.dev/play/p/g42Z3QSb53u
133153
func UniqBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice {
154+
if collection == nil {
155+
return nil
156+
}
157+
134158
result := make(Slice, 0, len(collection))
135159
seen := make(map[U]struct{}, len(collection))
136160

@@ -170,6 +194,10 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
170194
panic("Second parameter must be greater than 0")
171195
}
172196

197+
if collection == nil {
198+
return nil
199+
}
200+
173201
chunksNum := len(collection) / size
174202
if len(collection)%size != 0 {
175203
chunksNum += 1
@@ -193,6 +221,10 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
193221
// of running each element of collection through iteratee.
194222
// Play: https://go.dev/play/p/NfQ_nGjkgXW
195223
func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K) []Slice {
224+
if collection == nil {
225+
return nil
226+
}
227+
196228
result := []Slice{}
197229
seen := map[K]int{}
198230

@@ -219,6 +251,10 @@ func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee fun
219251
// Flatten returns an array a single level deep.
220252
// Play: https://go.dev/play/p/rbp9ORaMpjw
221253
func Flatten[T any, Slice ~[]T](collection []Slice) Slice {
254+
if collection == nil {
255+
return nil
256+
}
257+
222258
totalLen := 0
223259
for i := range collection {
224260
totalLen += len(collection[i])
@@ -283,6 +319,10 @@ func Shuffle[T any, Slice ~[]T](collection Slice) Slice {
283319
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
284320
// Play: https://go.dev/play/p/fhUMLvZ7vS6
285321
func Reverse[T any, Slice ~[]T](collection Slice) Slice {
322+
if collection == nil {
323+
return nil
324+
}
325+
286326
length := len(collection)
287327
half := length / 2
288328

@@ -297,6 +337,10 @@ func Reverse[T any, Slice ~[]T](collection Slice) Slice {
297337
// Fill fills elements of array with `initial` value.
298338
// Play: https://go.dev/play/p/VwR34GzqEub
299339
func Fill[T Clonable[T]](collection []T, initial T) []T {
340+
if collection == nil {
341+
return nil
342+
}
343+
300344
result := make([]T, 0, len(collection))
301345

302346
for range collection {
@@ -370,6 +414,10 @@ func SliceToMap[T any, K comparable, V any](collection []T, transform func(item
370414
// Drop drops n elements from the beginning of a slice or array.
371415
// Play: https://go.dev/play/p/JswS7vXRJP2
372416
func Drop[T any, Slice ~[]T](collection Slice, n int) Slice {
417+
if collection == nil {
418+
return nil
419+
}
420+
373421
if len(collection) <= n {
374422
return make(Slice, 0)
375423
}
@@ -382,6 +430,10 @@ func Drop[T any, Slice ~[]T](collection Slice, n int) Slice {
382430
// DropRight drops n elements from the end of a slice or array.
383431
// Play: https://go.dev/play/p/GG0nXkSJJa3
384432
func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice {
433+
if collection == nil {
434+
return nil
435+
}
436+
385437
if len(collection) <= n {
386438
return Slice{}
387439
}
@@ -393,6 +445,10 @@ func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice {
393445
// DropWhile drops elements from the beginning of a slice or array while the predicate returns true.
394446
// Play: https://go.dev/play/p/7gBPYw2IK16
395447
func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice {
448+
if collection == nil {
449+
return nil
450+
}
451+
396452
i := 0
397453
for ; i < len(collection); i++ {
398454
if !predicate(collection[i]) {
@@ -407,6 +463,10 @@ func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool)
407463
// DropRightWhile drops elements from the end of a slice or array while the predicate returns true.
408464
// Play: https://go.dev/play/p/3-n71oEC0Hz
409465
func DropRightWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice {
466+
if collection == nil {
467+
return nil
468+
}
469+
410470
i := len(collection) - 1
411471
for ; i >= 0; i-- {
412472
if !predicate(collection[i]) {
@@ -453,6 +513,10 @@ func DropByIndex[T any](collection []T, indexes ...int) []T {
453513
// Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.
454514
// Play: https://go.dev/play/p/YkLMODy1WEL
455515
func Reject[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice {
516+
if collection == nil {
517+
return nil
518+
}
519+
456520
result := Slice{}
457521

458522
for i := range collection {
@@ -549,6 +613,10 @@ func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) m
549613
// Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow.
550614
// Play: https://go.dev/play/p/tOQu1GhFcog
551615
func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice {
616+
if collection == nil {
617+
return nil
618+
}
619+
552620
size := len(collection)
553621

554622
if offset < 0 {
@@ -572,6 +640,10 @@ func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice
572640
// Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow.
573641
// Play: https://go.dev/play/p/8XWYhfMMA1h
574642
func Slice[T any, Slice ~[]T](collection Slice, start int, end int) Slice {
643+
if collection == nil {
644+
return nil
645+
}
646+
575647
size := len(collection)
576648

577649
if start >= end {
@@ -598,7 +670,11 @@ func Slice[T any, Slice ~[]T](collection Slice, start int, end int) Slice {
598670
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
599671
// Play: https://go.dev/play/p/XfPzmf9gql6
600672
func Replace[T comparable, Slice ~[]T](collection Slice, old T, new T, n int) Slice {
601-
result := make(Slice, len(collection))
673+
if collection == nil {
674+
return nil
675+
}
676+
677+
result := make([]T, len(collection))
602678
copy(result, collection)
603679

604680
for i := range result {
@@ -620,6 +696,10 @@ func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old T, new T) Slice
620696
// Compact returns a slice of all non-zero elements.
621697
// Play: https://go.dev/play/p/tXiy-iK6PAc
622698
func Compact[T comparable, Slice ~[]T](collection Slice) Slice {
699+
if collection == nil {
700+
return nil
701+
}
702+
623703
var zero T
624704

625705
result := make(Slice, 0, len(collection))

0 commit comments

Comments
 (0)