Moved logic of collapsing songs into albums to model package

(it should really be called domain.... maybe will rename it later)
This commit is contained in:
Deluan
2022-12-19 11:00:20 -05:00
committed by Deluan Quintão
parent e03ccb3166
commit 28e7371d93
13 changed files with 507 additions and 26 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/navidrome/navidrome/utils/math2"
"github.com/navidrome/navidrome/utils/number"
)
const baseUrl = "https://www.gravatar.com/avatar"
@@ -19,7 +19,7 @@ func Url(email string, size int) string {
if size < 1 {
size = defaultSize
}
size = math2.Min(maxSize, size)
size = number.Min(maxSize, size)
return fmt.Sprintf("%s/%x?s=%d", baseUrl, hash, size)
}
@@ -1,4 +1,4 @@
package math2
package number
import "golang.org/x/exp/constraints"
@@ -1,38 +1,38 @@
package math2_test
package number_test
import (
"testing"
"github.com/navidrome/navidrome/utils/math2"
"github.com/navidrome/navidrome/utils/number"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestMath2(t *testing.T) {
func TestNumber(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Math2 Suite")
RunSpecs(t, "Number Suite")
}
var _ = Describe("Min", func() {
It("returns zero value if no arguments are passed", func() {
Expect(math2.Min[int]()).To(BeZero())
Expect(number.Min[int]()).To(BeZero())
})
It("returns the smallest int", func() {
Expect(math2.Min(1, 2)).To(Equal(1))
Expect(number.Min(1, 2)).To(Equal(1))
})
It("returns the smallest float", func() {
Expect(math2.Min(-4.1, -4.2, -4.0)).To(Equal(-4.2))
Expect(number.Min(-4.1, -4.2, -4.0)).To(Equal(-4.2))
})
})
var _ = Describe("Max", func() {
It("returns zero value if no arguments are passed", func() {
Expect(math2.Max[int]()).To(BeZero())
Expect(number.Max[int]()).To(BeZero())
})
It("returns the biggest int", func() {
Expect(math2.Max(1, 2)).To(Equal(2))
Expect(number.Max(1, 2)).To(Equal(2))
})
It("returns the biggest float", func() {
Expect(math2.Max(-4.1, -4.2, -4.0)).To(Equal(-4.0))
Expect(number.Max(-4.1, -4.2, -4.0)).To(Equal(-4.0))
})
})
+35
View File
@@ -0,0 +1,35 @@
package slice
func Group[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T {
m := map[K][]T{}
for _, item := range s {
k := keyFunc(item)
m[k] = append(m[k], item)
}
return m
}
func MostFrequent[T comparable](list []T) T {
if len(list) == 0 {
var zero T
return zero
}
var topItem T
var topCount int
counters := map[T]int{}
if len(list) == 1 {
topItem = list[0]
} else {
for _, id := range list {
c := counters[id] + 1
counters[id] = c
if c > topCount {
topItem = id
topCount = c
}
}
}
return topItem
}
+43
View File
@@ -0,0 +1,43 @@
package slice_test
import (
"testing"
"github.com/navidrome/navidrome/utils/slice"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestSlice(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Slice Suite")
}
var _ = Describe("Group", func() {
It("returns empty map for an empty input", func() {
keyFunc := func(v int) int { return v % 2 }
result := slice.Group([]int{}, keyFunc)
Expect(result).To(BeEmpty())
})
It("groups by the result of the key function", func() {
keyFunc := func(v int) int { return v % 2 }
result := slice.Group([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, keyFunc)
Expect(result).To(HaveLen(2))
Expect(result[0]).To(ConsistOf(2, 4, 6, 8, 10))
Expect(result[1]).To(ConsistOf(1, 3, 5, 7, 9, 11))
})
})
var _ = Describe("MostFrequent", func() {
It("returns zero value if no arguments are passed", func() {
Expect(slice.MostFrequent([]int{})).To(BeZero())
})
It("returns the single item", func() {
Expect(slice.MostFrequent([]string{"123"})).To(Equal("123"))
})
It("returns the item that appeared more times", func() {
Expect(slice.MostFrequent([]string{"1", "2", "1", "2", "3", "2"})).To(Equal("2"))
})
})