diff options
author | Earl Warren <contact@earl-warren.org> | 2024-09-22 09:28:20 +0200 |
---|---|---|
committer | Earl Warren <contact@earl-warren.org> | 2024-09-27 08:42:48 +0200 |
commit | 1bdf334844f398d0a2adafc4499cef15f95df6d8 (patch) | |
tree | 204cdd1a33664291a87c78b4627835b12a14851a /modules/util | |
parent | Lazy load avatar images (#32051) (diff) | |
download | forgejo-1bdf334844f398d0a2adafc4499cef15f95df6d8.tar.xz forgejo-1bdf334844f398d0a2adafc4499cef15f95df6d8.zip |
feat: add IfZero utility function
(cherry picked from commit 43de021ac1ca017212ec75fd88a8a80a9db27c4c)
Diffstat (limited to 'modules/util')
-rw-r--r-- | modules/util/util.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/modules/util/util.go b/modules/util/util.go index 0444680228..dcd7cf4f29 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -225,6 +225,15 @@ func Iif[T any](condition bool, trueVal, falseVal T) T { return falseVal } +// IfZero returns "def" if "v" is a zero value, otherwise "v" +func IfZero[T comparable](v, def T) T { + var zero T + if v == zero { + return def + } + return v +} + func ReserveLineBreakForTextarea(input string) string { // Since the content is from a form which is a textarea, the line endings are \r\n. // It's a standard behavior of HTML. |