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 {
unknownDate := "unknown"
idx := len(dictionary.Entries) - 1
if len(dictionary.Entries) == 0 {
return "unknown"
}
dateEntry := dictionary.Entries[len(dictionary.Entries)-1]
if len(dateEntry.Sense) == 0 || len(dateEntry.Sense[0].Glossary) == 0 {
return "unknown"
return unknownDate
} else if len(dictionary.Entries[idx].Sense) == 0 {
return unknownDate
} else if len(dictionary.Entries[idx].Sense[0].Glossary) == 0 {
return unknownDate
}
dateGloss := dictionary.Entries[idx].Sense[0].Glossary[0].Content
r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
jmdictDate := r.FindString(dateEntry.Sense[0].Glossary[0].Content)
if jmdictDate != "" {
return jmdictDate
date := r.FindString(dateGloss)
if date != "" {
return date
} else {
return "unknown"
return unknownDate
}
}

View File

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