Skip to content

Commit

Permalink
cleanup helpers
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <[email protected]>
  • Loading branch information
Noxsios committed Oct 25, 2023
1 parent 0fb1995 commit e160dd9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 64 deletions.
64 changes: 0 additions & 64 deletions src/pkg/utils/helpers/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,9 @@ import (
"fmt"
"reflect"
"regexp"
"strings"
"time"
)

// Unique returns a new slice with only unique elements.
func Unique[T comparable](s []T) (r []T) {
exists := make(map[T]bool)
for _, str := range s {
if _, ok := exists[str]; !ok {
exists[str] = true
r = append(r, str)
}
}
return r
}

// Reverse returns a new slice with the elements in reverse order.
func Reverse[T any](s []T) (r []T) {
for i := len(s) - 1; i >= 0; i-- {
r = append(r, s[i])
}
return r
}

// Filter returns a new slice with only the elements that pass the test.
func Filter[T any](ss []T, test func(T) bool) (r []T) {
for _, s := range ss {
if test(s) {
r = append(r, s)
}
}
return r
}

// Find returns the first element that passes the test.
func Find[T any](ss []T, test func(T) bool) (r T) {
for _, s := range ss {
if test(s) {
return s
}
}
return r
}

// RemoveMatches removes the given element from the slice that matches the test.
func RemoveMatches[T any](ss []T, test func(T) bool) (r []T) {
for _, s := range ss {
if !test(s) {
r = append(r, s)
}
}
return r
}

// Retry will retry a function until it succeeds or the timeout is reached, timeout == retries * delay.
func Retry(fn func() error, retries int, delay time.Duration) (err error) {
for r := 0; r < retries; r++ {
Expand Down Expand Up @@ -180,16 +129,3 @@ func MergeNonZero[T any](original T, overrides T) T {
}
return originalValue.Interface().(T)
}

// StringToSlice converts a comma-separated string to a slice of lowercase strings.
func StringToSlice(s string) []string {
if s != "" {
split := strings.Split(s, ",")
for idx, element := range split {
split[idx] = strings.ToLower(strings.TrimSpace(element))
}
return split
}

return []string{}
}
65 changes: 65 additions & 0 deletions src/pkg/utils/helpers/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,71 @@
// Package helpers provides generic helper functions with no external imports
package helpers

import "strings"

// Unique returns a new slice with only unique elements.
func Unique[T comparable](s []T) (r []T) {
exists := make(map[T]bool)
for _, str := range s {
if _, ok := exists[str]; !ok {
exists[str] = true
r = append(r, str)
}
}
return r
}

// Reverse returns a new slice with the elements in reverse order.
func Reverse[T any](s []T) (r []T) {
for i := len(s) - 1; i >= 0; i-- {
r = append(r, s[i])
}
return r
}

// Filter returns a new slice with only the elements that pass the test.
func Filter[T any](ss []T, test func(T) bool) (r []T) {
for _, s := range ss {
if test(s) {
r = append(r, s)
}
}
return r
}

// Find returns the first element that passes the test.
func Find[T any](ss []T, test func(T) bool) (r T) {
for _, s := range ss {
if test(s) {
return s
}
}
return r
}

// RemoveMatches removes the given element from the slice that matches the test.
func RemoveMatches[T any](ss []T, test func(T) bool) (r []T) {
for _, s := range ss {
if !test(s) {
r = append(r, s)
}
}
return r
}

// StringToSlice converts a comma-separated string to a slice of lowercase strings.
func StringToSlice(s string) []string {
if s != "" {
split := strings.Split(s, ",")
for idx, element := range split {
split[idx] = strings.ToLower(strings.TrimSpace(element))
}
return split
}

return []string{}
}

// EqualFunc defines a type for a function that determines equality between two elements of type T.
type EqualFunc[T any] func(a, b T) bool

Expand Down

0 comments on commit e160dd9

Please sign in to comment.