Skip to content

Commit

Permalink
fix: making matching more permissive for author, narrator and tags (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumKerson committed Apr 14, 2023
1 parent c118587 commit 990d803
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
31 changes: 27 additions & 4 deletions internal/audiobooks/service/filters.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package service

import "github.com/CallumKerson/Athenaeum/pkg/audiobooks"
import (
"strings"
"unicode"

"github.com/CallumKerson/Athenaeum/pkg/audiobooks"
)

type Filter func(a *audiobooks.Audiobook) bool

func AuthorFilter(name string) Filter {
return func(a *audiobooks.Audiobook) bool {
if a != nil && contains(a.Authors, name) {
if a != nil && containsIgnoringCaseAndWhitespace(a.Authors, name) {
return true
}
return false
Expand All @@ -24,7 +29,7 @@ func GenreFilter(genre audiobooks.Genre) Filter {

func NarratorFilter(name string) Filter {
return func(a *audiobooks.Audiobook) bool {
if a != nil && contains(a.Narrators, name) {
if a != nil && containsIgnoringCaseAndWhitespace(a.Narrators, name) {
return true
}
return false
Expand All @@ -33,7 +38,7 @@ func NarratorFilter(name string) Filter {

func TagFilter(tag string) Filter {
return func(a *audiobooks.Audiobook) bool {
if a != nil && contains(a.Tags, tag) {
if a != nil && containsIgnoringCaseAndWhitespace(a.Tags, tag) {
return true
}
return false
Expand All @@ -48,3 +53,21 @@ func contains[K comparable](slice []K, item K) bool {
}
return false
}

func containsIgnoringCaseAndWhitespace(slice []string, item string) bool {
for _, v := range slice {
if normaliseString(v) == normaliseString(item) {
return true
}
}
return false
}

func normaliseString(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return unicode.ToLower(r)
}, str)
}
11 changes: 11 additions & 0 deletions internal/audiobooks/service/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ func TestFilers(t *testing.T) {
expectedMatch bool
}{
{name: "match author", filter: AuthorFilter("Max Gladstone"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match author ignoring case", filter: AuthorFilter("Max gladstone"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match author ignoring whitespace", filter: AuthorFilter("MaxGladstone"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match author ignoring case and whitespace", filter: AuthorFilter("maxgladstone"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "not match author", filter: AuthorFilter("Max Gladstone"), book: testbooks.Audiobooks[1], expectedMatch: false},
{name: "match genre", filter: GenreFilter(audiobooks.SciFi), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "not match genre", filter: GenreFilter(audiobooks.SciFi), book: testbooks.Audiobooks[1], expectedMatch: false},
{name: "match narrator", filter: NarratorFilter("Emily Woo Zeller"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match narrator ignoring case", filter: NarratorFilter("Emily woo zeller"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match narrator ignoring whitespace", filter: NarratorFilter("EmilyWooZeller"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match narrator ignoring case and whitespace", filter: NarratorFilter("emilywoozeller"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "not match narrator", filter: NarratorFilter("Emily Woo Zeller"), book: testbooks.Audiobooks[1], expectedMatch: false},
{name: "match tag", filter: TagFilter("Hugo Awards"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match tag ignoring case", filter: TagFilter("Hugo awards"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match tag ignoring whitespace", filter: TagFilter("HugoAwards"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "match tag ignoring case and whitespace", filter: TagFilter("hugoawards"), book: testbooks.Audiobooks[0], expectedMatch: true},
{name: "not match tag", filter: TagFilter("Nebula Awards"), book: testbooks.Audiobooks[1], expectedMatch: false},
}

for _, testCase := range tests {
Expand Down

0 comments on commit 990d803

Please sign in to comment.