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

View File

@@ -7,7 +7,7 @@ import (
"strings" "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. // This returns `d` if `given` appears not to be set, and `given` otherwise.
// //
@@ -17,8 +17,7 @@ import (
// Structs are never considered unset. // Structs are never considered unset.
// //
// For everything else, including pointers, a nil value is 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]) { if empty(given) || empty(given[0]) {
return d return d
} }
@@ -31,7 +30,6 @@ func empty(given any) bool {
if !g.IsValid() { if !g.IsValid() {
return true return true
} }
// Basically adapted from text/template.isTrue // Basically adapted from text/template.isTrue
switch g.Kind() { switch g.Kind() {
default: default:
@@ -140,8 +138,7 @@ func mustToRawJSON(v any) (string, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
enc := json.NewEncoder(buf) enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false) enc.SetEscapeHTML(false)
err := enc.Encode(&v) if err := enc.Encode(&v); err != nil {
if err != nil {
return "", err return "", err
} }
return strings.TrimSuffix(buf.String(), "\n"), nil 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 { func keys(dicts ...map[string]any) []string {
k := []string{} var k []string
for _, dict := range dicts { for _, dict := range dicts {
for key := range dict { for key := range dict {
k = append(k, key) 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 { func omit(dict map[string]any, keys ...string) map[string]any {
res := map[string]any{} res := map[string]any{}
omit := make(map[string]bool, len(keys)) omit := make(map[string]bool, len(keys))
for _, k := range keys { for _, k := range keys {
omit[k] = true omit[k] = true
} }
for k, v := range dict { for k, v := range dict {
if _, ok := omit[k]; !ok { if _, ok := omit[k]; !ok {
res[k] = v res[k] = v

View File

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

View File

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