1

Improve readability of publication date functions

This commit is contained in:
stephenmk 2023-02-04 01:42:08 -06:00
parent a9d85dc720
commit ecf22da5a3
No known key found for this signature in database
GPG Key ID: B6DA730DB06235F1
2 changed files with 24 additions and 18 deletions

View File

@ -63,19 +63,22 @@ func doDisplaySenseNumberTag(headword headword, entry jmdict.JmdictEntry, meta j
} }
func jmdictPublicationDate(dictionary jmdict.Jmdict) string { func jmdictPublicationDate(dictionary jmdict.Jmdict) string {
unknownDate := "unknown"
idx := len(dictionary.Entries) - 1
if len(dictionary.Entries) == 0 { if len(dictionary.Entries) == 0 {
return "unknown" return unknownDate
} } else if len(dictionary.Entries[idx].Sense) == 0 {
dateEntry := dictionary.Entries[len(dictionary.Entries)-1] return unknownDate
if len(dateEntry.Sense) == 0 || len(dateEntry.Sense[0].Glossary) == 0 { } else if len(dictionary.Entries[idx].Sense[0].Glossary) == 0 {
return "unknown" return unknownDate
} }
dateGloss := dictionary.Entries[idx].Sense[0].Glossary[0].Content
r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
jmdictDate := r.FindString(dateEntry.Sense[0].Glossary[0].Content) date := r.FindString(dateGloss)
if jmdictDate != "" { if date != "" {
return jmdictDate return date
} else { } else {
return "unknown" return unknownDate
} }
} }

View File

@ -8,19 +8,22 @@ import (
) )
func jmnedictPublicationDate(dictionary jmdict.Jmnedict) string { func jmnedictPublicationDate(dictionary jmdict.Jmnedict) string {
unknownDate := "unknown"
idx := len(dictionary.Entries) - 1
if len(dictionary.Entries) == 0 { if len(dictionary.Entries) == 0 {
return "unknown" return unknownDate
} } else if len(dictionary.Entries[idx].Translations) == 0 {
dateEntry := dictionary.Entries[len(dictionary.Entries)-1] return unknownDate
if len(dateEntry.Translations) == 0 || len(dateEntry.Translations[0].Translations) == 0 { } else if len(dictionary.Entries[idx].Translations[0].Translations) == 0 {
return "unknown" return unknownDate
} }
dateGloss := dictionary.Entries[idx].Translations[0].Translations[0]
r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
jmnedictDate := r.FindString(dateEntry.Translations[0].Translations[0]) date := r.FindString(dateGloss)
if jmnedictDate != "" { if date != "" {
return jmnedictDate return date
} else { } else {
return "unknown" return unknownDate
} }
} }