Discard duplicated tags

This commit is contained in:
Deluan 2023-12-26 19:35:14 -05:00
parent b4815ecee5
commit ea7ba22699
2 changed files with 29 additions and 0 deletions

View File

@ -57,6 +57,9 @@ func Extract(files ...string) (map[string]Tags, error) {
}
func NewTag(filePath string, fileInfo os.FileInfo, tags ParsedTags) Tags {
for t, values := range tags {
tags[t] = removeDuplicates(values)
}
return Tags{
filePath: filePath,
fileInfo: fileInfo,
@ -64,6 +67,19 @@ func NewTag(filePath string, fileInfo os.FileInfo, tags ParsedTags) Tags {
}
}
func removeDuplicates(values []string) []string {
encountered := map[string]struct{}{}
var result []string
for _, v := range values {
if _, ok := encountered[v]; ok {
continue
}
encountered[v] = struct{}{}
result = append(result, v)
}
return result
}
type ParsedTags map[string][]string
func (p ParsedTags) Map(customMappings ParsedTags) ParsedTags {

View File

@ -68,6 +68,19 @@ var _ = Describe("Tags", func() {
})
})
Describe("removeDuplicates", func() {
It("removes duplicates", func() {
md := NewTag("/music/artist/album01/Song.mp3", nil, ParsedTags{
"genre": []string{"pop", "rock", "pop"},
"date": []string{"2023-03-01", "2023-03-01"},
"mood": []string{"happy", "sad"},
})
Expect(md.tags).To(HaveKeyWithValue("genre", []string{"pop", "rock"}))
Expect(md.tags).To(HaveKeyWithValue("date", []string{"2023-03-01"}))
Expect(md.tags).To(HaveKeyWithValue("mood", []string{"happy", "sad"}))
})
})
Describe("Bpm", func() {
var t *Tags
BeforeEach(func() {