Skip to content

Commit

Permalink
chore(util): renamed Prep to ToTrimmedLower
Browse files Browse the repository at this point in the history
  • Loading branch information
hadjian committed Nov 28, 2023
1 parent f445f6d commit 4fd9a87
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
11 changes: 2 additions & 9 deletions internal/commands/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log/slog"
"regexp"
"strings"
"time"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions internal/model/toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/toc/toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4fd9a87

Please sign in to comment.