mirror of
https://github.com/prymitive/karma
synced 2026-02-13 20:59:53 +00:00
62 lines
1.0 KiB
Go
62 lines
1.0 KiB
Go
package slices
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"regexp"
|
|
)
|
|
|
|
// StringSliceToSHA1 returns a SHA1 hash computed from a slice of strings
|
|
func StringSliceToSHA1(stringArray []string) (string, error) {
|
|
h := sha1.New()
|
|
for _, s := range stringArray {
|
|
_, _ = h.Write([]byte(s))
|
|
_, _ = h.Write([]byte("\n"))
|
|
}
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
}
|
|
|
|
func StringSliceDiff(slice1, slice2 []string) ([]string, []string) {
|
|
missing := []string{}
|
|
extra := []string{}
|
|
|
|
var found bool
|
|
|
|
for _, s1 := range slice1 {
|
|
found = false
|
|
for _, s2 := range slice2 {
|
|
if s1 == s2 {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
missing = append(missing, s1)
|
|
}
|
|
}
|
|
|
|
for _, s2 := range slice2 {
|
|
found = false
|
|
for _, s1 := range slice1 {
|
|
if s2 == s1 {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
extra = append(extra, s2)
|
|
}
|
|
}
|
|
|
|
return missing, extra
|
|
}
|
|
|
|
func MatchesAnyRegex(value string, regexes []*regexp.Regexp) bool {
|
|
for _, regex := range regexes {
|
|
if regex.MatchString(value) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|