1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-11-29 19:59:59 +01:00

Refactor a little

This commit is contained in:
binwiederhier 2025-07-19 22:52:08 +02:00
parent 8783c86cd6
commit 1f34c39eb0
5 changed files with 8 additions and 22 deletions

View file

@ -38,12 +38,10 @@ func dateInZone(fmt string, date any, zone string) string {
case int32:
t = time.Unix(int64(date), 0)
}
loc, err := time.LoadLocation(zone)
if err != nil {
loc, _ = time.LoadLocation("UTC")
}
return t.In(loc).Format(fmt)
}
@ -65,7 +63,6 @@ func mustDateModify(fmt string, date time.Time) (time.Time, error) {
func dateAgo(date any) string {
var t time.Time
switch date := date.(type) {
default:
t = time.Now()
@ -76,9 +73,7 @@ func dateAgo(date any) string {
case int:
t = time.Unix(int64(date), 0)
}
// Drop resolution to seconds
duration := time.Since(t).Round(time.Second)
return duration.String()
return time.Since(t).Round(time.Second).String()
}
func duration(sec any) string {
@ -106,13 +101,10 @@ func durationRound(duration any) string {
case time.Time:
d = time.Since(duration)
}
u := uint64(d)
neg := d < 0
if neg {
var u uint64
if d < 0 {
u = -u
}
var (
year = uint64(time.Hour) * 24 * 365
month = uint64(time.Hour) * 24 * 30

View file

@ -7,7 +7,7 @@ import (
"strings"
)
// dfault checks whether `given` is set, and returns default if not set.
// defaultValue checks whether `given` is set, and returns default if not set.
//
// This returns `d` if `given` appears not to be set, and `given` otherwise.
//
@ -17,8 +17,7 @@ import (
// Structs are never considered unset.
//
// For everything else, including pointers, a nil value is unset.
func dfault(d any, given ...any) any {
func defaultValue(d any, given ...any) any {
if empty(given) || empty(given[0]) {
return d
}
@ -31,7 +30,6 @@ func empty(given any) bool {
if !g.IsValid() {
return true
}
// Basically adapted from text/template.isTrue
switch g.Kind() {
default:
@ -140,8 +138,7 @@ func mustToRawJSON(v any) (string, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(&v)
if err != nil {
if err := enc.Encode(&v); err != nil {
return "", err
}
return strings.TrimSuffix(buf.String(), "\n"), nil

View file

@ -33,7 +33,7 @@ func pluck(key string, d ...map[string]any) []any {
}
func keys(dicts ...map[string]any) []string {
k := []string{}
var k []string
for _, dict := range dicts {
for key := range dict {
k = append(k, key)
@ -54,12 +54,10 @@ func pick(dict map[string]any, keys ...string) map[string]any {
func omit(dict map[string]any, keys ...string) map[string]any {
res := map[string]any{}
omit := make(map[string]bool, len(keys))
for _, k := range keys {
omit[k] = true
}
for k, v := range dict {
if _, ok := omit[k]; !ok {
res[k] = v

View file

@ -100,7 +100,7 @@ func TxtFuncMap() template.FuncMap {
"sortAlpha": sortAlpha,
// Defaults
"default": dfault,
"default": defaultValue,
"empty": empty,
"coalesce": coalesce,
"all": all,

View file

@ -20,7 +20,6 @@ func push(list any, v any) []any {
if err != nil {
panic(err)
}
return l
}