/
sliceutil.go
173 lines (132 loc) · 3.15 KB
/
sliceutil.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
// Package sliceutil provides methods for working with slices
package sliceutil
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"errors"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Copy creates copy of given slice
func Copy[K comparable](slice []K) []K {
if len(slice) == 0 {
return nil
}
s := make([]K, len(slice))
copy(s, slice)
return s
}
// IsEqual compares two slices and returns true if the slices are equal
func IsEqual[K comparable](s1, s2 []K) bool {
switch {
case s1 == nil && s2 == nil:
return true
case len(s1) != len(s2):
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}
// StringToInterface converts slice with strings to slice with any
func StringToInterface(data []string) []any {
if len(data) == 0 {
return nil
}
result := make([]any, len(data))
for i, r := range data {
result[i] = r
}
return result
}
// IntToInterface converts slice with ints to slice with any
func IntToInterface(data []int) []any {
if len(data) == 0 {
return nil
}
result := make([]any, len(data))
for i, r := range data {
result[i] = r
}
return result
}
// StringToError converts slice with strings to slice with errors
func StringToError(data []string) []error {
if len(data) == 0 {
return nil
}
result := make([]error, len(data))
for i, e := range data {
result[i] = errors.New(e)
}
return result
}
// ErrorToString converts slice with errors to slice with strings
func ErrorToString(data []error) []string {
if len(data) == 0 {
return nil
}
result := make([]string, len(data))
for i, e := range data {
result[i] = e.Error()
}
return result
}
// Index returns index of given item in a slice or -1 otherwise
func Index[K comparable](slice []K, item K) int {
if len(slice) == 0 {
return -1
}
for i, v := range slice {
if v == item {
return i
}
}
return -1
}
// Contains checks if string slice contains some value
func Contains[K comparable](slice []K, value K) bool {
return Index(slice, value) != -1
}
// Exclude removes items from slice
func Exclude[K comparable](slice []K, items ...K) []K {
var n int
s := Copy(slice)
if len(slice) == 0 || len(items) == 0 {
return s
}
LOOP:
for _, i := range s {
for _, j := range items {
if i == j {
continue LOOP
}
}
s[n] = i
n++
}
return s[:n]
}
// Deduplicate removes duplicates from slice.
// Slice must be sorted before deduplication.
func Deduplicate[K comparable](slice []K) []K {
var n int
s := Copy(slice)
if len(slice) <= 1 {
return s
}
for i := 1; i < len(s); i++ {
if s[n] == s[i] {
continue
}
n++
s[n] = s[i]
}
return s[:n+1]
}