add helper package

This commit is contained in:
Jonas Leder 2025-03-18 20:13:24 +01:00
commit ed3f0eef79
3 changed files with 44 additions and 0 deletions

16
helper/anyToString.go Normal file
View file

@ -0,0 +1,16 @@
package helper
import "fmt"
func AnyToString(value interface{}) string {
switch v := value.(type) {
case string:
return v
case float64:
return fmt.Sprintf("%.2f", v)
case int:
return fmt.Sprintf("%d", v)
default:
return ""
}
}

View file

@ -0,0 +1,15 @@
package helper
import (
"math/rand"
)
func GenerateRandomString(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
// Generate random string
result := make([]byte, length)
for i := range result {
result[i] = charset[rand.Intn(len(charset))]
}
return string(result)
}

13
helper/sortMapKeys.go Normal file
View file

@ -0,0 +1,13 @@
package helper
import "sort"
// Helper function to get sorted keys from a map
func SortMapKeys(data map[string]interface{}) []string {
keys := make([]string, 0, len(data))
for key := range data {
keys = append(keys, key)
}
sort.Strings(keys) // Sort keys alphabetically
return keys
}