Skip to content

Commit

Permalink
fix(release): don't modify slice when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxencs committed Jan 24, 2024
1 parent 4987103 commit 033e476
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
16 changes: 2 additions & 14 deletions internal/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package release

import (
"strings"

"seasonpackarr/internal/domain"
"seasonpackarr/internal/utils"

Expand Down Expand Up @@ -55,8 +53,8 @@ func compareReleases(r1, r2 rls.Release, fuzzyMatching domain.FuzzyMatching) int

// normalize any HDR format down to plain HDR when simplifyHdrCompare is enabled
if fuzzyMatching.SimplifyHdrCompare {
r1.HDR = replaceHDR(r1.HDR)
r2.HDR = replaceHDR(r2.HDR)
r1.HDR = utils.SimplifyHDRSlice(r1.HDR)
r2.HDR = utils.SimplifyHDRSlice(r2.HDR)
}

if !utils.CompareStringSlices(r1.HDR, r2.HDR) {
Expand All @@ -83,13 +81,3 @@ func PercentOfTotalEpisodes(totalEpisodes int, matchedEpisodes []int) float32 {

return percent
}

func replaceHDR(slice []string) []string {
for i, v := range slice {
if strings.Contains(v, "HDR") {
slice[i] = "HDR"
}
}

return slice
}
30 changes: 26 additions & 4 deletions internal/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package utils

import "golang.org/x/exp/slices"
import (
"strings"

"golang.org/x/exp/slices"
)

func DedupeSlice[T comparable](s []T) []T {
inResult := make(map[T]bool)
Expand All @@ -18,11 +22,29 @@ func DedupeSlice[T comparable](s []T) []T {
}

func CompareStringSlices(x, y []string) bool {
slices.Sort(x)
slices.Sort(y)
if len(x) != len(y) {
return false
}

sortedX := slices.Clone(x)
sortedY := slices.Clone(y)

if slices.Equal(x, y) {
slices.Sort(sortedX)
slices.Sort(sortedY)

if slices.Equal(sortedX, sortedY) {
return true
}

return false
}

func SimplifyHDRSlice(hdrSlice []string) []string {
for i, v := range hdrSlice {
if strings.Contains(v, "HDR") {
hdrSlice[i] = "HDR"
}
}

return hdrSlice
}

0 comments on commit 033e476

Please sign in to comment.