1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-06 17:41:00 +00:00

refactor(template): remove unused functions and reduce the complexity of truncate function

- Remove unused functions like hasKey, domain, hasPrefix and contains.
- Lower the complexity of truncate from O(n) to O(1).
This commit is contained in:
Julien Voisin 2025-07-18 05:53:41 +02:00 committed by GitHub
parent d80fb242db
commit 1825320369
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 29 deletions

View file

@ -34,7 +34,6 @@ func (f *funcMap) Map() template.FuncMap {
return template.FuncMap{
"formatFileSize": formatFileSize,
"dict": dict,
"hasKey": hasKey,
"truncate": truncate,
"isEmail": isEmail,
"baseURL": config.Opts.BaseURL,
@ -78,8 +77,6 @@ func (f *funcMap) Map() template.FuncMap {
return slices.Contains(config.Opts.MediaProxyResourceTypes(), mediaType)
},
"domain": urllib.Domain,
"hasPrefix": strings.HasPrefix,
"contains": strings.Contains,
"replace": func(str, old, new string) string {
return strings.Replace(str, old, new, 1)
},
@ -132,20 +129,9 @@ func dict(values ...interface{}) (map[string]interface{}, error) {
return dict, nil
}
func hasKey(dict map[string]string, key string) bool {
if value, found := dict[key]; found {
return value != ""
}
return false
}
func truncate(str string, max int) string {
runes := 0
for i := range str {
runes++
if runes > max {
return str[:i] + "…"
}
if runes := []rune(str); len(runes) > max {
return string(runes[:max]) + "…"
}
return str
}

View file

@ -43,18 +43,6 @@ func TestDictWithInvalidMap(t *testing.T) {
}
}
func TestHasKey(t *testing.T) {
input := map[string]string{"k": "v"}
if !hasKey(input, "k") {
t.Fatal(`This key exists in the map and should returns true`)
}
if hasKey(input, "missing") {
t.Fatal(`This key doesn't exists in the given map and should returns false`)
}
}
func TestTruncateWithShortTexts(t *testing.T) {
scenarios := []string{"Short text", "Короткий текст"}