summaryrefslogtreecommitdiffstats
path: root/tpl/math/math.go
diff options
context:
space:
mode:
authorsepts <github@septs.pw>2023-03-11 18:21:08 +0800
committerGitHub <noreply@github.com>2023-03-11 11:21:08 +0100
commit84201e8d5d319399a868e64d20d7a40e5aae2023 (patch)
tree971b63b58017eac4e56a5900498df7a4daaf177f /tpl/math/math.go
parent04b9811643830297a24180a48bc1b665b6cc749f (diff)
tpl/math: Allow multi numbers in add, sub, mul, div, min and max
Diffstat (limited to 'tpl/math/math.go')
-rw-r--r--tpl/math/math.go86
1 files changed, 54 insertions, 32 deletions
diff --git a/tpl/math/math.go b/tpl/math/math.go
index 257e803e4..e7460e7ec 100644
--- a/tpl/math/math.go
+++ b/tpl/math/math.go
@@ -20,7 +20,6 @@ import (
"sync/atomic"
_math "github.com/gohugoio/hugo/common/math"
-
"github.com/spf13/cast"
)
@@ -32,9 +31,9 @@ func New() *Namespace {
// Namespace provides template functions for the "math" namespace.
type Namespace struct{}
-// Add adds the two addends n1 and n2.
-func (ns *Namespace) Add(n1, n2 any) (any, error) {
- return _math.DoArithmetic(n1, n2, '+')
+// Add adds the multivalued addends n1 and n2 or more values.
+func (ns *Namespace) Add(inputs ...any) (any, error) {
+ return ns.doArithmetic(inputs, '+')
}
// Ceil returns the least integer value greater than or equal to n.
@@ -48,8 +47,8 @@ func (ns *Namespace) Ceil(n any) (float64, error) {
}
// Div divides n1 by n2.
-func (ns *Namespace) Div(n1, n2 any) (any, error) {
- return _math.DoArithmetic(n1, n2, '/')
+func (ns *Namespace) Div(inputs ...any) (any, error) {
+ return ns.doArithmetic(inputs, '/')
}
// Floor returns the greatest integer value less than or equal to n.
@@ -72,28 +71,40 @@ func (ns *Namespace) Log(n any) (float64, error) {
return math.Log(af), nil
}
-// Max returns the greater of the two numbers n1 or n2.
-func (ns *Namespace) Max(n1, n2 any) (float64, error) {
- af, erra := cast.ToFloat64E(n1)
- bf, errb := cast.ToFloat64E(n2)
-
- if erra != nil || errb != nil {
- return 0, errors.New("Max operator can't be used with non-float value")
+// Max returns the greater of the multivalued numbers n1 and n2 or more values.
+func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
+ var value float64
+ for index, input := range inputs {
+ value, err = cast.ToFloat64E(input)
+ if err != nil {
+ err = errors.New("Max operator can't be used with non-float value")
+ return
+ }
+ if index == 0 {
+ maximum = value
+ continue
+ }
+ maximum = math.Max(value, maximum)
}
-
- return math.Max(af, bf), nil
-}
-
-// Min returns the smaller of two numbers n1 or n2.
-func (ns *Namespace) Min(n1, n2 any) (float64, error) {
- af, erra := cast.ToFloat64E(n1)
- bf, errb := cast.ToFloat64E(n2)
-
- if erra != nil || errb != nil {
- return 0, errors.New("Min operator can't be used with non-float value")
+ return
+}
+
+// Min returns the smaller of multivalued numbers n1 and n2 or more values.
+func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {
+ var value float64
+ for index, input := range inputs {
+ value, err = cast.ToFloat64E(input)
+ if err != nil {
+ err = errors.New("Max operator can't be used with non-float value")
+ return
+ }
+ if index == 0 {
+ minimum = value
+ continue
+ }
+ minimum = math.Min(value, minimum)
}
-
- return math.Min(af, bf), nil
+ return
}
// Mod returns n1 % n2.
@@ -122,9 +133,9 @@ func (ns *Namespace) ModBool(n1, n2 any) (bool, error) {
return res == int64(0), nil
}
-// Mul multiplies the two numbers n1 and n2.
-func (ns *Namespace) Mul(n1, n2 any) (any, error) {
- return _math.DoArithmetic(n1, n2, '*')
+// Mul multiplies the multivalued numbers n1 and n2 or more values.
+func (ns *Namespace) Mul(inputs ...any) (any, error) {
+ return ns.doArithmetic(inputs, '*')
}
// Pow returns n1 raised to the power of n2.
@@ -159,9 +170,20 @@ func (ns *Namespace) Sqrt(n any) (float64, error) {
return math.Sqrt(af), nil
}
-// Sub subtracts n2 from n1.
-func (ns *Namespace) Sub(n1, n2 any) (any, error) {
- return _math.DoArithmetic(n1, n2, '-')
+// Sub subtracts multivalued.
+func (ns *Namespace) Sub(inputs ...any) (any, error) {
+ return ns.doArithmetic(inputs, '-')
+}
+
+func (ns *Namespace) doArithmetic(inputs []any, operation rune) (value any, err error) {
+ value = inputs[0]
+ for i := 1; i < len(inputs); i++ {
+ value, err = _math.DoArithmetic(value, inputs[i], operation)
+ if err != nil {
+ return
+ }
+ }
+ return
}
var counter uint64