Refactor string utilities into its own package str

This commit is contained in:
Deluan 2024-06-05 22:09:27 -04:00
parent 46fc38bf61
commit abe5690018
16 changed files with 158 additions and 125 deletions

View file

@ -1,32 +0,0 @@
package utils
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("SanitizeStrings", func() {
It("returns all lowercase chars", func() {
Expect(SanitizeStrings("Some Text")).To(Equal("some text"))
})
It("removes accents", func() {
Expect(SanitizeStrings("Quintão")).To(Equal("quintao"))
})
It("remove extra spaces", func() {
Expect(SanitizeStrings(" some text ")).To(Equal("some text"))
})
It("remove duplicated words", func() {
Expect(SanitizeStrings("legião urbana urbana legiÃo")).To(Equal("legiao urbana"))
})
It("remove symbols", func() {
Expect(SanitizeStrings("Tom’s Diner ' “40” ‘A’")).To(Equal("40 a diner toms"))
})
It("remove opening brackets", func() {
Expect(SanitizeStrings("[Five Years]")).To(Equal("five years"))
})
})

View file

@ -1,4 +1,4 @@
package utils
package str
import (
"html"
@ -38,3 +38,13 @@ func SanitizeText(text string) string {
s := policy.Sanitize(text)
return html.UnescapeString(s)
}
func SanitizeFieldForSorting(originalValue string) string {
v := strings.TrimSpace(sanitize.Accents(originalValue))
return strings.ToLower(v)
}
func SanitizeFieldForSortingNoArticle(originalValue string) string {
v := strings.TrimSpace(sanitize.Accents(originalValue))
return strings.ToLower(NoArticle(v))
}

View file

@ -0,0 +1,66 @@
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("Sanitize Strings", func() {
Describe("SanitizeStrings", func() {
It("returns all lowercase chars", func() {
Expect(str.SanitizeStrings("Some Text")).To(Equal("some text"))
})
It("removes accents", func() {
Expect(str.SanitizeStrings("Quintão")).To(Equal("quintao"))
})
It("remove extra spaces", func() {
Expect(str.SanitizeStrings(" some text ")).To(Equal("some text"))
})
It("remove duplicated words", func() {
Expect(str.SanitizeStrings("legião urbana urbana legiÃo")).To(Equal("legiao urbana"))
})
It("remove symbols", func() {
Expect(str.SanitizeStrings("Tom’s Diner ' “40” ‘A’")).To(Equal("40 a diner toms"))
})
It("remove opening brackets", func() {
Expect(str.SanitizeStrings("[Five Years]")).To(Equal("five years"))
})
})
Describe("SanitizeFieldForSorting", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The O"
})
It("sanitize accents", func() {
Expect(str.SanitizeFieldForSorting("Céu")).To(Equal("ceu"))
})
It("removes articles", func() {
Expect(str.SanitizeFieldForSorting("The Beatles")).To(Equal("the beatles"))
})
It("removes accented articles", func() {
Expect(str.SanitizeFieldForSorting("Õ Blésq Blom")).To(Equal("o blesq blom"))
})
})
Describe("SanitizeFieldForSortingNoArticle", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The O"
})
It("sanitize accents", func() {
Expect(str.SanitizeFieldForSortingNoArticle("Céu")).To(Equal("ceu"))
})
It("removes articles", func() {
Expect(str.SanitizeFieldForSortingNoArticle("The Beatles")).To(Equal("beatles"))
})
It("removes accented articles", func() {
Expect(str.SanitizeFieldForSortingNoArticle("Õ Blésq Blom")).To(Equal("blesq blom"))
})
})
})

View file

@ -1,12 +1,23 @@
package utils
package str
import (
"strings"
"github.com/deluan/sanitize"
"github.com/navidrome/navidrome/conf"
)
func Clear(name string) string {
r := strings.NewReplacer(
"–", "-",
"‐", "-",
"“", `"`,
"”", `"`,
"‘", `'`,
"’", `'`,
)
return r.Replace(name)
}
func NoArticle(name string) string {
articles := strings.Split(conf.Server.IgnoredArticles, " ")
for _, a := range articles {
@ -33,13 +44,3 @@ func LongestCommonPrefix(list []string) string {
}
return list[0]
}
func SanitizeFieldForSorting(originalValue string) string {
v := strings.TrimSpace(sanitize.Accents(originalValue))
return strings.ToLower(v)
}
func SanitizeFieldForSortingNoArticle(originalValue string) string {
v := strings.TrimSpace(sanitize.Accents(originalValue))
return strings.ToLower(NoArticle(v))
}

View file

@ -0,0 +1,13 @@
package str_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestStrClear(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Str Suite")
}

View file

@ -1,11 +1,24 @@
package utils
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", "k–os", "k-os"),
Entry("k‐os", "k‐os", "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() {
@ -13,10 +26,10 @@ var _ = Describe("Strings", func() {
conf.Server.IgnoredArticles = ""
})
It("returns empty if string is empty", func() {
Expect(NoArticle("")).To(BeEmpty())
Expect(str.NoArticle("")).To(BeEmpty())
})
It("returns same string", func() {
Expect(NoArticle("The Beatles")).To(Equal("The Beatles"))
Expect(str.NoArticle("The Beatles")).To(Equal("The Beatles"))
})
})
Context("Default articles", func() {
@ -24,49 +37,20 @@ var _ = Describe("Strings", func() {
conf.Server.IgnoredArticles = "The El La Los Las Le Les Os As O A"
})
It("returns empty if string is empty", func() {
Expect(NoArticle("")).To(BeEmpty())
Expect(str.NoArticle("")).To(BeEmpty())
})
It("remove prefix article from string", func() {
Expect(NoArticle("Os Paralamas do Sucesso")).To(Equal("Paralamas do Sucesso"))
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(NoArticle("Thelonious Monk")).To(Equal("Thelonious Monk"))
Expect(str.NoArticle("Thelonious Monk")).To(Equal("Thelonious Monk"))
})
})
})
Describe("sanitizeFieldForSorting", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The O"
})
It("sanitize accents", func() {
Expect(SanitizeFieldForSorting("Céu")).To(Equal("ceu"))
})
It("removes articles", func() {
Expect(SanitizeFieldForSorting("The Beatles")).To(Equal("the beatles"))
})
It("removes accented articles", func() {
Expect(SanitizeFieldForSorting("Õ Blésq Blom")).To(Equal("o blesq blom"))
})
})
Describe("SanitizeFieldForSortingNoArticle", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The O"
})
It("sanitize accents", func() {
Expect(SanitizeFieldForSortingNoArticle("Céu")).To(Equal("ceu"))
})
It("removes articles", func() {
Expect(SanitizeFieldForSortingNoArticle("The Beatles")).To(Equal("beatles"))
})
It("removes accented articles", func() {
Expect(SanitizeFieldForSortingNoArticle("Õ Blésq Blom")).To(Equal("blesq blom"))
})
})
Describe("LongestCommonPrefix", func() {
It("finds the longest common prefix", func() {
Expect(LongestCommonPrefix(testPaths)).To(Equal("/Music/iTunes 1/iTunes Media/Music/"))
Expect(str.LongestCommonPrefix(testPaths)).To(Equal("/Music/iTunes 1/iTunes Media/Music/"))
})
})