fix: address code review findings

This commit is contained in:
2026-07-19 19:07:20 +03:00
parent a4c426f640
commit c70f46af27
10 changed files with 77 additions and 124 deletions

View File

@@ -18,6 +18,10 @@ var (
parenRe = regexp.MustCompile(`\([^)]*\)`)
yearRe = regexp.MustCompile(`\b(1[0-9]{3}|2[0-9]{3})\b`)
spaceRe = regexp.MustCompile(`\s+`)
// wordRe matches any alphabetic character. Used to decide whether a title
// that collapses entirely to a year actually had other words worth keeping
// (e.g. "1989 (Deluxe)") versus being a bare year title (e.g. "1989").
wordRe = regexp.MustCompile(`[a-z]`)
)
// NormalizeString normalizes a string for fuzzy matching by:
@@ -28,6 +32,10 @@ var (
// - Collapsing multiple spaces into one
// - Trimming leading/trailing whitespace
func NormalizeString(s string) string {
// Capture the original input; used after stripping to tell a bare year
// title apart from a title that merely collapses to a year.
original := s
// Convert to lowercase
s = strings.ToLower(s)
@@ -37,11 +45,23 @@ func NormalizeString(s string) string {
// Remove parenthesized content (e.g., (Deluxe), (Remastered))
s = parenRe.ReplaceAllString(s, "")
// Remove years (4-digit numbers between 1000-2999). If stripping the
// year would empty the entire string (e.g. an album literally titled
// "1989" or "2112"), keep the original form so the title can still match.
// Remove years (4-digit numbers between 1000-2999). If stripping the year
// would empty the entire string, we must decide what to keep:
// - A bare year title (e.g. "1989", "2112") has no other words, so keep
// the year so it can still match itself (the user owns that album).
// - A title that had OTHER words alongside the year (e.g. "1989 (Deluxe)")
// collapses to empty on purpose: it is a distinct release group that
// must NOT be considered already-present just because the user owns the
// standard "1989". Collapsing to empty makes it score 0.0 against a
// plain "1989", correctly reporting the reissue as missing.
stripped := yearRe.ReplaceAllString(s, "")
if strings.TrimSpace(stripped) != "" {
if strings.TrimSpace(stripped) == "" {
if wordRe.MatchString(strings.ToLower(original)) {
s = ""
} else {
s = strings.TrimSpace(s)
}
} else {
s = stripped
}