79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
package flags
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func filterSlice(slice []string, value string) []string {
|
|
var result []string
|
|
for _, v := range slice {
|
|
if v != value {
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|