From 164232cf0d1007e20c96920c7e03d92e946073bf Mon Sep 17 00:00:00 2001 From: mkelvers Date: Fri, 12 Jun 2026 13:17:51 +0200 Subject: [PATCH] fix: prefer original over japanese in jikan displaytitle --- integrations/jikan/types.go | 27 ++++++++++++++++++--------- integrations/jikan/types_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 integrations/jikan/types_test.go diff --git a/integrations/jikan/types.go b/integrations/jikan/types.go index 4b466d1..f7a2e80 100644 --- a/integrations/jikan/types.go +++ b/integrations/jikan/types.go @@ -33,12 +33,18 @@ type Aired struct { String string `json:"string"` } +type TitleEntry struct { + Type string `json:"type"` + Title string `json:"title"` +} + type Anime struct { - MalID int `json:"mal_id"` - Title string `json:"title"` - TitleEnglish string `json:"title_english"` - TitleJapanese string `json:"title_japanese"` - TitleSynonyms []string `json:"title_synonyms"` + MalID int `json:"mal_id"` + Title string `json:"title"` + TitleEnglish string `json:"title_english"` + TitleJapanese string `json:"title_japanese"` + TitleSynonyms []string `json:"title_synonyms"` + Titles []TitleEntry `json:"titles"` Images struct { Jpg struct { LargeImageURL string `json:"large_image_url"` @@ -454,13 +460,16 @@ type ReviewsResponse struct { Pagination Pagination `json:"pagination"` } -// DisplayTitle returns English title if available, otherwise Japanese, then default. +// DisplayTitle returns English title if available, otherwise default title, titles[0], then Japanese. func (a Anime) DisplayTitle() string { if a.TitleEnglish != "" { return a.TitleEnglish } - if a.TitleJapanese != "" { - return a.TitleJapanese + if a.Title != "" { + return a.Title } - return a.Title + if len(a.Titles) > 0 && a.Titles[0].Title != "" { + return a.Titles[0].Title + } + return a.TitleJapanese } diff --git a/integrations/jikan/types_test.go b/integrations/jikan/types_test.go new file mode 100644 index 0000000..587ee0f --- /dev/null +++ b/integrations/jikan/types_test.go @@ -0,0 +1,27 @@ +package jikan + +import "testing" + +func TestAnimeDisplayTitlePrefersTitleBeforeJapanese(t *testing.T) { + anime := Anime{ + Title: "Cyberpunk: Edgerunners", + TitleJapanese: "サイバーパンク エッジランナーズ", + } + + if got := anime.DisplayTitle(); got != "Cyberpunk: Edgerunners" { + t.Fatalf("DisplayTitle() = %q, want default title", got) + } +} + +func TestAnimeDisplayTitleFallsBackToFirstTitleEntryBeforeJapanese(t *testing.T) { + anime := Anime{ + TitleJapanese: "サイバーパンク エッジランナーズ", + Titles: []TitleEntry{ + {Type: "Default", Title: "Cyberpunk: Edgerunners"}, + }, + } + + if got := anime.DisplayTitle(); got != "Cyberpunk: Edgerunners" { + t.Fatalf("DisplayTitle() = %q, want first title entry", got) + } +}