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:
parent
d80fb242db
commit
1825320369
2 changed files with 3 additions and 29 deletions
|
@ -34,7 +34,6 @@ func (f *funcMap) Map() template.FuncMap {
|
||||||
return template.FuncMap{
|
return template.FuncMap{
|
||||||
"formatFileSize": formatFileSize,
|
"formatFileSize": formatFileSize,
|
||||||
"dict": dict,
|
"dict": dict,
|
||||||
"hasKey": hasKey,
|
|
||||||
"truncate": truncate,
|
"truncate": truncate,
|
||||||
"isEmail": isEmail,
|
"isEmail": isEmail,
|
||||||
"baseURL": config.Opts.BaseURL,
|
"baseURL": config.Opts.BaseURL,
|
||||||
|
@ -77,9 +76,7 @@ func (f *funcMap) Map() template.FuncMap {
|
||||||
"mustBeProxyfied": func(mediaType string) bool {
|
"mustBeProxyfied": func(mediaType string) bool {
|
||||||
return slices.Contains(config.Opts.MediaProxyResourceTypes(), mediaType)
|
return slices.Contains(config.Opts.MediaProxyResourceTypes(), mediaType)
|
||||||
},
|
},
|
||||||
"domain": urllib.Domain,
|
"domain": urllib.Domain,
|
||||||
"hasPrefix": strings.HasPrefix,
|
|
||||||
"contains": strings.Contains,
|
|
||||||
"replace": func(str, old, new string) string {
|
"replace": func(str, old, new string) string {
|
||||||
return strings.Replace(str, old, new, 1)
|
return strings.Replace(str, old, new, 1)
|
||||||
},
|
},
|
||||||
|
@ -132,20 +129,9 @@ func dict(values ...interface{}) (map[string]interface{}, error) {
|
||||||
return dict, nil
|
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 {
|
func truncate(str string, max int) string {
|
||||||
runes := 0
|
if runes := []rune(str); len(runes) > max {
|
||||||
for i := range str {
|
return string(runes[:max]) + "…"
|
||||||
runes++
|
|
||||||
if runes > max {
|
|
||||||
return str[:i] + "…"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestTruncateWithShortTexts(t *testing.T) {
|
||||||
scenarios := []string{"Short text", "Короткий текст"}
|
scenarios := []string{"Short text", "Короткий текст"}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue