Skip to content

Commit

Permalink
Refactor SortContent and DeleteDupe functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Jan 31, 2024
1 parent de3ff67 commit 70ee5a7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions handler/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func ReadmeSug(filename, group string, content ...string) string {
if len(finds) == 1 {
return finds[0]
}
finds = SortContent(finds)
finds = SortContent(finds...)

// match either the filename or the group name with a priority extension
// e.g. .nfo, .txt, .unp, .doc
Expand Down Expand Up @@ -839,7 +839,7 @@ func SafeHTML(s string) template.HTML {
// SortContent sorts the content list by the number of slashes in each string.
// It prioritizes strings with fewer slashes (i.e., closer to the root).
// If the number of slashes is the same, it sorts alphabetically.
func SortContent(content []string) []string {
func SortContent(content ...string) []string {
sort.Slice(content, func(i, j int) bool {
// Fix any Windows path separators
content[i] = strings.ReplaceAll(content[i], "\\", "/")
Expand Down
6 changes: 5 additions & 1 deletion handler/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func TestSortContent(t *testing.T) {
content []string
expected []string
}{
{
content: nil,
expected: nil,
},
{
content: []string{
"dir1/file1",
Expand Down Expand Up @@ -150,7 +154,7 @@ func TestSortContent(t *testing.T) {
copy(originalContent, tt.content)

// Sort the content using the SortContent function
sortedContent := app.SortContent(tt.content)
sortedContent := app.SortContent(tt.content...)

for i, v := range sortedContent {
assert.Equal(t, tt.expected[i], v)
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Cache int

const (
Pouet Cache = iota // pouet data cache
Pouet Cache = iota // data cache for the Pouet website, API requests
Test // test cache
)

Expand Down
2 changes: 1 addition & 1 deletion internal/helper/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Capitalize(s string) string {

// DeleteDupe removes duplicate strings from a slice.
// The returned slice is sorted and compacted.
func DeleteDupe(s []string) []string {
func DeleteDupe(s ...string) []string {
slices.Sort(s)
s = slices.Compact(s)
x := make([]string, 0, len(s))
Expand Down
8 changes: 4 additions & 4 deletions internal/helper/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func TestCapitalize(t *testing.T) {
}

func TestDeleteDupe(t *testing.T) {
s := helper.DeleteDupe([]string{})
s := helper.DeleteDupe(nil...)
assert.EqualValues(t, []string{}, s)
s = helper.DeleteDupe([]string{"a"})
s = helper.DeleteDupe([]string{"a"}...)
assert.EqualValues(t, []string{"a"}, s)
s = helper.DeleteDupe([]string{"a", "b", "abcde"})
s = helper.DeleteDupe([]string{"a", "b", "abcde"}...)
assert.EqualValues(t, []string{"a", "abcde", "b"}, s) // sorted
s = helper.DeleteDupe([]string{"a", "b", "a"})
s = helper.DeleteDupe([]string{"a", "b", "a"}...)
assert.EqualValues(t, []string{"a", "b"}, s)
}

Expand Down
10 changes: 4 additions & 6 deletions model/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ import (
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

var (
ErrDB = errors.New("database connection is nil")
)
var ErrDB = errors.New("database connection is nil")

// Repair the database data.
// Repair a column or type of data within the database.
type Repair int

const (
None Repair = iota - 1 // None does nothing.
All // All repairs all the repairable data.
Releaser // Releaser repairs the releaser data.
Releaser // Releaser focuses on the releaser data using the group_brand_by and group_brand_for columns.
)

// In the future we may want to add a Debug or TestRun func.

// Run the repair.
// Run the database repair based on the repair option.
func (r Repair) Run(ctx context.Context, w io.Writer, db *sql.DB) error {
if w == nil {
w = io.Discard
Expand Down
2 changes: 1 addition & 1 deletion model/scener.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ func (s Sceners) Sort() []string {
for _, scener := range s {
sceners = append(sceners, strings.Split(string(scener.Name), ",")...)
}
return helper.DeleteDupe(sceners)
return helper.DeleteDupe(sceners...)
}

0 comments on commit 70ee5a7

Please sign in to comment.