tijl.dev-core/internal/apps/flags/util.go

92 lines
1.8 KiB
Go
Raw Normal View History

2024-08-26 14:22:24 +02:00
package flags
import (
"crypto/sha256"
"encoding/binary"
2024-08-26 16:57:28 +02:00
"errors"
"fmt"
2024-08-26 14:22:24 +02:00
"math/rand"
)
func shuffleSliceRandom(slice []string) []string {
shuffled := make([]string, len(slice))
copy(shuffled, slice)
for i := len(shuffled) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
}
return shuffled
}
func shuffleSlice(slice []string, seed string) []string {
hasher := sha256.New()
hasher.Write([]byte(seed))
hash := hasher.Sum(nil)
seedInt := int64(binary.LittleEndian.Uint64(hash[:8]))
r := rand.New(rand.NewSource(seedInt))
shuffled := make([]string, len(slice))
copy(shuffled, slice)
r.Shuffle(len(shuffled), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
return shuffled
}
2024-08-26 16:57:28 +02:00
func filterCountriesByTags(tags []string) (error, []string) {
var result []string
for _, tag := range tags {
isSupported := false
for _, supportedTag := range supportedTags {
if tag == supportedTag {
isSupported = true
}
}
if isSupported == false {
return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result
}
}
for _, country := range countryCodes {
for _, tag := range tags {
for _, cTag := range country.Tags {
if cTag == tag {
result = append(result, country.Code)
}
}
}
}
return nil, result
}
2024-08-29 12:36:58 +02:00
func filterSlice(slice []string, value string) []string {
var result []string
for _, v := range slice {
if v != value {
result = append(result, v)
}
}
return result
}
2024-08-29 12:54:38 +02:00
func hasCommonTag(tags1, tags2 []string) bool {
tagSet := make(map[string]struct{})
for _, tag := range tags1 {
tagSet[tag] = struct{}{}
}
for _, tag := range tags2 {
if _, exists := tagSet[tag]; exists {
return true
}
}
return false
}