From 4fd9a8731e0b117b0469eb1fa9467393f31be54c Mon Sep 17 00:00:00 2001 From: Pedram Hadjian Date: Tue, 28 Nov 2023 08:47:53 +0100 Subject: [PATCH] chore(util): renamed Prep to ToTrimmedLower --- internal/commands/fetch.go | 11 ++--------- internal/model/toc.go | 10 +++++----- internal/toc/toc.go | 2 +- internal/util.go | 2 +- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/internal/commands/fetch.go b/internal/commands/fetch.go index 943913db..1f176dd3 100644 --- a/internal/commands/fetch.go +++ b/internal/commands/fetch.go @@ -5,7 +5,6 @@ import ( "fmt" "log/slog" "regexp" - "strings" "time" "github.com/Masterminds/semver/v3" @@ -66,7 +65,7 @@ func FetchThingByName(fn *FetchName, remote remotes.Remote) ([]byte, error) { } } - official := internal.Prep(tocThing.Author.Name) == internal.Prep(tocThing.Manufacturer.Name) + official := internal.ToTrimmedLower(tocThing.Author.Name) == internal.ToTrimmedLower(tocThing.Manufacturer.Name) tmid, err := model.ParseTMID(id, official) thing, err = remote.Fetch(tmid) @@ -165,7 +164,7 @@ func findDigest(versions []model.TOCVersion, digest string) (id string, err erro return "", errors.New(msg) } - digest = prep(digest) + digest = internal.ToTrimmedLower(digest) for _, version := range versions { // TODO: how to know if it is official? tmid, err := model.ParseTMID(version.TMID, false) @@ -181,9 +180,3 @@ func findDigest(versions []model.TOCVersion, digest string) (id string, err erro log.Error(msg) return "", errors.New(msg) } - -func prep(s string) string { - s = strings.TrimSpace(s) - s = strings.ToLower(s) - return s -} diff --git a/internal/model/toc.go b/internal/model/toc.go index 732f8c71..6ff7fa0b 100644 --- a/internal/model/toc.go +++ b/internal/model/toc.go @@ -47,18 +47,18 @@ func (toc *TOC) Filter(filter string) { } func matchFilter(entry TOCEntry, filter string) bool { - filter = internal.Prep(filter) - if strings.Contains(internal.Prep(entry.Name), filter) { + filter = internal.ToTrimmedLower(filter) + if strings.Contains(internal.ToTrimmedLower(entry.Name), filter) { return true } - if strings.Contains(internal.Prep(entry.Manufacturer.Name), filter) { + if strings.Contains(internal.ToTrimmedLower(entry.Manufacturer.Name), filter) { return true } - if strings.Contains(internal.Prep(entry.Mpn), filter) { + if strings.Contains(internal.ToTrimmedLower(entry.Mpn), filter) { return true } for _, version := range entry.Versions { - if strings.Contains(internal.Prep(version.Description), filter) { + if strings.Contains(internal.ToTrimmedLower(version.Description), filter) { return true } } diff --git a/internal/toc/toc.go b/internal/toc/toc.go index d357ec4a..42df29a8 100644 --- a/internal/toc/toc.go +++ b/internal/toc/toc.go @@ -100,7 +100,7 @@ func saveToc(rootPath string, tocBytes []byte) error { } func insert(relPath string, toc *model.TOC, ctm model.CatalogThingModel) error { - official := internal.Prep(ctm.Manufacturer.Name) == internal.Prep(ctm.Author.Name) + official := internal.ToTrimmedLower(ctm.Manufacturer.Name) == internal.ToTrimmedLower(ctm.Author.Name) tmid, err := model.ParseTMID(ctm.ID, official) if err != nil { return err diff --git a/internal/util.go b/internal/util.go index 3e61d9b7..e82fa762 100644 --- a/internal/util.go +++ b/internal/util.go @@ -65,7 +65,7 @@ func ExpandHome(path string) (string, error) { return filepath.Join(home, rest), nil } -func Prep(s string) string { +func ToTrimmedLower(s string) string { s = strings.TrimSpace(s) s = strings.ToLower(s) return s