Add denormalized list of artist_ids to album, to speed-up artist's albums queries
This will be removed once we have a proper many-to-many relationship between album and artist
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/kennygrant/sanitize"
|
||||
)
|
||||
|
||||
var quotesRegex = regexp.MustCompile("[“”‘’'\"\\[\\(\\{\\]\\)\\}]")
|
||||
|
||||
func SanitizeStrings(text ...string) string {
|
||||
sanitizedText := strings.Builder{}
|
||||
for _, txt := range text {
|
||||
sanitizedText.WriteString(strings.TrimSpace(sanitize.Accents(strings.ToLower(txt))) + " ")
|
||||
}
|
||||
words := make(map[string]struct{})
|
||||
for _, w := range strings.Fields(sanitizedText.String()) {
|
||||
words[w] = struct{}{}
|
||||
}
|
||||
var fullText []string
|
||||
for w := range words {
|
||||
w = quotesRegex.ReplaceAllString(w, "")
|
||||
if w != "" {
|
||||
fullText = append(fullText, w)
|
||||
}
|
||||
}
|
||||
sort.Strings(fullText)
|
||||
return strings.Join(fullText, " ")
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "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"))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user