Small refactoring

This commit is contained in:
Deluan 2024-06-05 22:39:36 -04:00
parent abe5690018
commit 11bef060a3
5 changed files with 53 additions and 56 deletions

View File

@ -141,7 +141,7 @@ func (r *artistRepository) toModels(dba []dbArtist) model.Artists {
}
func (r *artistRepository) getIndexKey(a *model.Artist) string {
name := strings.ToLower(str.NoArticle(a.Name))
name := strings.ToLower(str.RemoveArticle(a.Name))
for k, v := range r.indexGroups {
key := strings.ToLower(k)
if strings.HasPrefix(name, key) {

View File

@ -8,6 +8,7 @@ import (
"github.com/deluan/sanitize"
"github.com/microcosm-cc/bluemonday"
"github.com/navidrome/navidrome/conf"
)
var quotesRegex = regexp.MustCompile("[“”‘’'\"\\[\\(\\{\\]\\)\\}]")
@ -46,5 +47,16 @@ func SanitizeFieldForSorting(originalValue string) string {
func SanitizeFieldForSortingNoArticle(originalValue string) string {
v := strings.TrimSpace(sanitize.Accents(originalValue))
return strings.ToLower(NoArticle(v))
return strings.ToLower(RemoveArticle(v))
}
func RemoveArticle(name string) string {
articles := strings.Split(conf.Server.IgnoredArticles, " ")
for _, a := range articles {
n := strings.TrimPrefix(name, a+" ")
if n != name {
return n
}
}
return name
}

View File

@ -63,4 +63,32 @@ var _ = Describe("Sanitize Strings", func() {
Expect(str.SanitizeFieldForSortingNoArticle("Õ Blésq Blom")).To(Equal("blesq blom"))
})
})
Describe("RemoveArticle", func() {
Context("Empty articles list", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = ""
})
It("returns empty if string is empty", func() {
Expect(str.RemoveArticle("")).To(BeEmpty())
})
It("returns same string", func() {
Expect(str.RemoveArticle("The Beatles")).To(Equal("The Beatles"))
})
})
Context("Default articles", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The El La Los Las Le Les Os As O A"
})
It("returns empty if string is empty", func() {
Expect(str.RemoveArticle("")).To(BeEmpty())
})
It("remove prefix article from string", func() {
Expect(str.RemoveArticle("Os Paralamas do Sucesso")).To(Equal("Paralamas do Sucesso"))
})
It("does not remove article if it is part of the first word", func() {
Expect(str.RemoveArticle("Thelonious Monk")).To(Equal("Thelonious Monk"))
})
})
})
})

View File

@ -2,8 +2,6 @@ package str
import (
"strings"
"github.com/navidrome/navidrome/conf"
)
func Clear(name string) string {
@ -18,17 +16,6 @@ func Clear(name string) string {
return r.Replace(name)
}
func NoArticle(name string) string {
articles := strings.Split(conf.Server.IgnoredArticles, " ")
for _, a := range articles {
n := strings.TrimPrefix(name, a+" ")
if n != name {
return n
}
}
return name
}
func LongestCommonPrefix(list []string) string {
if len(list) == 0 {
return ""

View File

@ -1,51 +1,22 @@
package str_test
import (
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/utils/str"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Clean", func() {
DescribeTable("replaces some Unicode chars with their equivalent ASCII",
func(input, expected string) {
Expect(str.Clear(input)).To(Equal(expected))
},
Entry("k-os", "kos", "k-os"),
Entry("kos", "kos", "k-os"),
Entry(`"Weird" Al Yankovic`, "“Weird” Al Yankovic", `"Weird" Al Yankovic`),
Entry("Single quotes", "Single quotes", "'Single' quotes"),
)
})
var _ = Describe("Strings", func() {
Describe("NoArticle", func() {
Context("Empty articles list", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = ""
})
It("returns empty if string is empty", func() {
Expect(str.NoArticle("")).To(BeEmpty())
})
It("returns same string", func() {
Expect(str.NoArticle("The Beatles")).To(Equal("The Beatles"))
})
})
Context("Default articles", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The El La Los Las Le Les Os As O A"
})
It("returns empty if string is empty", func() {
Expect(str.NoArticle("")).To(BeEmpty())
})
It("remove prefix article from string", func() {
Expect(str.NoArticle("Os Paralamas do Sucesso")).To(Equal("Paralamas do Sucesso"))
})
It("does not remove article if it is part of the first word", func() {
Expect(str.NoArticle("Thelonious Monk")).To(Equal("Thelonious Monk"))
})
})
var _ = Describe("String Utils", func() {
Describe("Clear", func() {
DescribeTable("replaces some Unicode chars with their equivalent ASCII",
func(input, expected string) {
Expect(str.Clear(input)).To(Equal(expected))
},
Entry("k-os", "kos", "k-os"),
Entry("kos", "kos", "k-os"),
Entry(`"Weird" Al Yankovic`, "“Weird” Al Yankovic", `"Weird" Al Yankovic`),
Entry("Single quotes", "Single quotes", "'Single' quotes"),
)
})
Describe("LongestCommonPrefix", func() {
@ -53,7 +24,6 @@ var _ = Describe("Strings", func() {
Expect(str.LongestCommonPrefix(testPaths)).To(Equal("/Music/iTunes 1/iTunes Media/Music/"))
})
})
})
var testPaths = []string{