From 22d28a7b18922d6e1c9f99b97be60d7a239642e4 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sat, 4 May 2024 10:20:46 +1200 Subject: [PATCH] Chore: Remove function duplication - use common tools.InArray() --- internal/htmlcheck/main.go | 17 +++-------------- internal/htmlcheck/platforms.go | 8 ++++++-- internal/storage/tags.go | 6 +++--- internal/storage/utils.go | 12 ------------ 4 files changed, 12 insertions(+), 31 deletions(-) diff --git a/internal/htmlcheck/main.go b/internal/htmlcheck/main.go index fca276e..e953102 100644 --- a/internal/htmlcheck/main.go +++ b/internal/htmlcheck/main.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/PuerkitoBio/goquery" + "github.com/axllent/mailpit/internal/tools" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" @@ -136,12 +137,12 @@ func (c CanIEmail) getTest(k string) (Warning, error) { var y, n, p float32 for family, stats := range found.Stats { - if len(LimitFamilies) != 0 && !inArray(family, LimitFamilies) { + if len(LimitFamilies) != 0 && !tools.InArray(family, LimitFamilies) { continue } for platform, clients := range stats.(map[string]interface{}) { - if len(LimitPlatforms) != 0 && !inArray(platform, LimitPlatforms) { + if len(LimitPlatforms) != 0 && !tools.InArray(platform, LimitPlatforms) { continue } for version, support := range clients.(map[string]interface{}) { @@ -182,18 +183,6 @@ func (c CanIEmail) getTest(k string) (Warning, error) { return warning, nil } -func inArray(n string, h []string) bool { - n = strings.ToLower(n) - - for _, v := range h { - if strings.ToLower(v) == n { - return true - } - } - - return false -} - // Convert markdown to HTML, stripping

&

func mdToHTML(str string) string { md := []byte(str) diff --git a/internal/htmlcheck/platforms.go b/internal/htmlcheck/platforms.go index ba39194..42c0c79 100644 --- a/internal/htmlcheck/platforms.go +++ b/internal/htmlcheck/platforms.go @@ -1,6 +1,10 @@ package htmlcheck -import "sort" +import ( + "sort" + + "github.com/axllent/mailpit/internal/tools" +) // Platforms returns all platforms with their respective email clients func Platforms() (map[string][]string, error) { @@ -19,7 +23,7 @@ func Platforms() (map[string][]string, error) { if !found { data[platform] = []string{} } - if !inArray(niceFamily, c) { + if !tools.InArray(niceFamily, c) { c = append(c, niceFamily) data[platform] = c } diff --git a/internal/storage/tags.go b/internal/storage/tags.go index c281300..9facfc4 100644 --- a/internal/storage/tags.go +++ b/internal/storage/tags.go @@ -25,7 +25,7 @@ func SetMessageTags(id string, tags []string) error { applyTags := []string{} for _, t := range tags { t = tools.CleanTag(t) - if t != "" && config.ValidTagRegexp.MatchString(t) && !inArray(t, applyTags) { + if t != "" && config.ValidTagRegexp.MatchString(t) && !tools.InArray(t, applyTags) { applyTags = append(applyTags, t) } } @@ -34,7 +34,7 @@ func SetMessageTags(id string, tags []string) error { origTagCount := len(currentTags) for _, t := range applyTags { - if t == "" || !config.ValidTagRegexp.MatchString(t) || inArray(t, currentTags) { + if t == "" || !config.ValidTagRegexp.MatchString(t) || tools.InArray(t, currentTags) { continue } @@ -47,7 +47,7 @@ func SetMessageTags(id string, tags []string) error { currentTags = getMessageTags(id) for _, t := range currentTags { - if !inArray(t, applyTags) { + if !tools.InArray(t, applyTags) { if err := DeleteMessageTag(id, t); err != nil { return err } diff --git a/internal/storage/utils.go b/internal/storage/utils.go index 64583c8..fb63493 100644 --- a/internal/storage/utils.go +++ b/internal/storage/utils.go @@ -87,18 +87,6 @@ func isFile(path string) bool { return true } -// Tests if a string is within an array. It is not case sensitive. -func inArray(k string, arr []string) bool { - k = strings.ToLower(k) - for _, v := range arr { - if strings.ToLower(v) == k { - return true - } - } - - return false -} - // Convert `%` to `%%` for SQL searches func escPercentChar(s string) string { return strings.ReplaceAll(s, "%", "%%")