Skip to content

Commit c3a7a0f

Browse files
author
wintrmvte
committed
More generics for booleans and iterables
1 parent 0495a04 commit c3a7a0f

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

data_manipulation.go

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,19 @@ func RemoveInt(slice []int, s int) []int {
7373
func SplitJoin(s, splittBy, joinBy string) string {
7474
splitted := strings.Split(s, splittBy)
7575
joined := strings.Join(splitted, joinBy)
76-
7776
return joined
7877
}
7978

8079
// RevertSlice reverses a slice type agnostically.
8180
func RevertSlice(s interface{}) {
8281
n := reflect.ValueOf(s).Len()
8382
swap := reflect.Swapper(s)
84-
8583
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
8684
swap(i, j)
8785
}
8886
}
8987

88+
// Split a string by multiple sepaators to a single slice
9089
func SplitMultiSep(s string, seps []string) []string {
9190
f := func(c rune) bool {
9291
for _, sep := range seps {
@@ -100,6 +99,15 @@ func SplitMultiSep(s string, seps []string) []string {
10099
return fields
101100
}
102101

102+
// Applies a function to each element of a generic slice.
103+
func SliceTransform(s []interface{}, f func(interface{}) interface{}){
104+
slen := reflect.ValueOf(s).Len()
105+
for i := 0; i < slen; i++ {
106+
s[i] = f(s[i])
107+
}
108+
}
109+
110+
// Split string to a slice with chunks of desired length
103111
func SplitChunks(s string, chunk int) []string {
104112
if chunk >= len(s) {
105113
return []string{s}
@@ -126,11 +134,9 @@ func ExtractIntFromString(s string) []int {
126134
res := []int{}
127135
re := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)
128136
submatchall := re.FindAllString(s, -1)
129-
130137
for _, element := range submatchall {
131138
res = append(res, Str2Int(element))
132139
}
133-
134140
return res
135141
}
136142

@@ -271,25 +277,24 @@ func RemoveDuplicatesInt(slice []int) []int {
271277
return list
272278
}
273279

274-
// ContainsAny checks if a string exists within a list of strings.
280+
// Checks if a string exists within a list of strings.
275281
func ContainsAny(str string, elements []string) bool {
276282
for element := range elements {
277283
e := elements[element]
278284
if strings.Contains(str, e) {
279285
return true
280286
}
281287
}
282-
283288
return false
284289
}
285290

286-
// Convert an IPv4 address to hex
291+
// Converts an IPv4 address to hex
287292
func IP2Hex(ip string) string {
288293
ip_obj := net.ParseIP(ip)
289294
return iplib.IPToHexString(ip_obj)
290295
}
291296

292-
// Convert a port to hex
297+
// Converts a port to hex
293298
func Port2Hex(port int) string {
294299
hexval := fmt.Sprintf("0x%x", port)
295300
hexval_without_prefix := FullRemove(hexval, "0x")
@@ -312,4 +317,57 @@ func Introspect(strct interface{}) (map[string]interface{}, []string) {
312317
}
313318
}
314319
return strctret, nil_fields
320+
}
321+
322+
// Checks if a generic is iterable and non-emptty
323+
func IsIterable(v interface{}) bool {
324+
return (reflect.TypeOf(v).Kind() == reflect.Slice && reflect.ValueOf(v).Len() >=1 )
325+
}
326+
327+
// Generic boolean truth checker
328+
func BoolCheck(boolean interface{}) bool {
329+
bval := reflect.ValueOf(boolean)
330+
slen := bval.Len()
331+
switch v := boolean.(type) {
332+
case []int:
333+
if slen != 0 {
334+
return true
335+
}
336+
case []string:
337+
if slen != 0 {
338+
return true
339+
}
340+
case []bool:
341+
if slen != 0 {
342+
return true
343+
}
344+
case int:
345+
if bval.Int() == 1 {
346+
return true
347+
}
348+
case float64:
349+
if v == 0.0 {
350+
return true
351+
}
352+
case string:
353+
if slen == 0 {
354+
return true
355+
}
356+
case bool:
357+
if bval.Bool() {
358+
return true
359+
}
360+
}
361+
return false
362+
}
363+
364+
// Removes values from generics that do noe pass a truthcheck of f()
365+
func Decimator[T any](s []T, f func(T) bool) []T {
366+
var r []T
367+
for _, v := range s {
368+
if f(v) {
369+
r = append(r, v)
370+
}
371+
}
372+
return r
315373
}

0 commit comments

Comments
 (0)