tpl/math: Allow multi numbers in add, sub, mul, div, min and max

This commit is contained in:
septs 2023-03-11 18:21:08 +08:00 committed by GitHub
parent 04b9811643
commit 84201e8d5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 107 deletions

View file

@ -3,7 +3,7 @@ title: Math
description: Hugo provides mathematical operators in templates. description: Hugo provides mathematical operators in templates.
date: 2017-02-01 date: 2017-02-01
publishdate: 2017-02-01 publishdate: 2017-02-01
lastmod: 2020-02-23 lastmod: 2023-03-11
keywords: [math, operators] keywords: [math, operators]
categories: [functions] categories: [functions]
menu: menu:
@ -21,13 +21,13 @@ aliases: []
| Function | Description | Example | | Function | Description | Example |
|--------------|-----------------------------------------------------------------------------|----------------------------------| |--------------|-----------------------------------------------------------------------------|----------------------------------|
| `add` | Adds two numbers. | `{{add 1 2}}` → `3` | | `add` | Adds multivalued numbers. | `{{add 1 2}}` → `3` |
| | *If one of the numbers is a float, the result is a float.* | `{{add 1.1 2}}` → `3.1` | | | *If one of the numbers is a float, the result is a float.* | `{{add 1.1 2}}` → `3.1` |
| `sub` | Subtracts two numbers. | `{{sub 3 2}}` → `1` | | `sub` | Subtracts multivalued numbers. | `{{sub 3 2}}` → `1` |
| | *If one of the numbers is a float, the result is a float.* | `{{sub 3 2.5}}` → `0.5` | | | *If one of the numbers is a float, the result is a float.* | `{{sub 3 2.5}}` → `0.5` |
| `mul` | Multiplies two numbers. | `{{mul 2 3}}` → `6` | | `mul` | Multiplies multivalued numbers. | `{{mul 2 3}}` → `6` |
| | *If one of the numbers is a float, the result is a float.* | `{{mul 2 3.1}}` → `6.2` | | | *If one of the numbers is a float, the result is a float.* | `{{mul 2 3.1}}` → `6.2` |
| `div` | Divides two numbers. | `{{div 6 3}}` → `2` | | `div` | Divides multivalued numbers. | `{{div 6 3}}` → `2` |
| | | `{{div 6 4}}` → `1` | | | | `{{div 6 4}}` → `1` |
| | *If one of the numbers is a float, the result is a float.* | `{{div 6 4.0}}` → `1.5` | | | *If one of the numbers is a float, the result is a float.* | `{{div 6 4.0}}` → `1.5` |
| `mod` | Modulus of two integers. | `{{mod 15 3}}` → `0` | | `mod` | Modulus of two integers. | `{{mod 15 3}}` → `0` |
@ -35,8 +35,8 @@ aliases: []
| `math.Ceil` | Returns the least integer value greater than or equal to the given number. | `{{math.Ceil 2.1}}` → `3` | | `math.Ceil` | Returns the least integer value greater than or equal to the given number. | `{{math.Ceil 2.1}}` → `3` |
| `math.Floor` | Returns the greatest integer value less than or equal to the given number. | `{{math.Floor 1.9}}` → `1` | | `math.Floor` | Returns the greatest integer value less than or equal to the given number. | `{{math.Floor 1.9}}` → `1` |
| `math.Log` | Returns the natural logarithm of the given number. | `{{math.Log 42}}` → `3.737` | | `math.Log` | Returns the natural logarithm of the given number. | `{{math.Log 42}}` → `3.737` |
| `math.Max` | Returns the greater of two numbers. | `{{math.Max 1 2}}` → `2` | | `math.Max` | Returns the greater of multivalued numbers. | `{{math.Max 1 2}}` → `2` |
| `math.Min` | Returns the smaller of two numbers. | `{{math.Min 1 2}}` → `1` | | `math.Min` | Returns the smaller of multivalued numbers. | `{{math.Min 1 2}}` → `1` |
| `math.Pow` | Returns the first number raised to the power of the second number. | `{{math.Pow 2 3}}` → `8` | | `math.Pow` | Returns the first number raised to the power of the second number. | `{{math.Pow 2 3}}` → `8` |
| `math.Round` | Returns the nearest integer, rounding half away from zero. | `{{math.Round 1.5}}` → `2` | | `math.Round` | Returns the nearest integer, rounding half away from zero. | `{{math.Round 1.5}}` → `2` |
| `math.Sqrt` | Returns the square root of the given number. | `{{math.Sqrt 81}}` → `9` | | `math.Sqrt` | Returns the square root of the given number. | `{{math.Sqrt 81}}` → `9` |

View file

@ -20,7 +20,6 @@ import (
"sync/atomic" "sync/atomic"
_math "github.com/gohugoio/hugo/common/math" _math "github.com/gohugoio/hugo/common/math"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -32,9 +31,9 @@ func New() *Namespace {
// Namespace provides template functions for the "math" namespace. // Namespace provides template functions for the "math" namespace.
type Namespace struct{} type Namespace struct{}
// Add adds the two addends n1 and n2. // Add adds the multivalued addends n1 and n2 or more values.
func (ns *Namespace) Add(n1, n2 any) (any, error) { func (ns *Namespace) Add(inputs ...any) (any, error) {
return _math.DoArithmetic(n1, n2, '+') return ns.doArithmetic(inputs, '+')
} }
// Ceil returns the least integer value greater than or equal to n. // 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. // Div divides n1 by n2.
func (ns *Namespace) Div(n1, n2 any) (any, error) { func (ns *Namespace) Div(inputs ...any) (any, error) {
return _math.DoArithmetic(n1, n2, '/') return ns.doArithmetic(inputs, '/')
} }
// Floor returns the greatest integer value less than or equal to n. // 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 return math.Log(af), nil
} }
// Max returns the greater of the two numbers n1 or n2. // Max returns the greater of the multivalued numbers n1 and n2 or more values.
func (ns *Namespace) Max(n1, n2 any) (float64, error) { func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
af, erra := cast.ToFloat64E(n1) var value float64
bf, errb := cast.ToFloat64E(n2) for index, input := range inputs {
value, err = cast.ToFloat64E(input)
if erra != nil || errb != nil { if err != nil {
return 0, errors.New("Max operator can't be used with non-float value") err = errors.New("Max operator can't be used with non-float value")
return
} }
if index == 0 {
return math.Max(af, bf), nil maximum = value
continue
}
maximum = math.Max(value, maximum)
}
return
} }
// Min returns the smaller of two numbers n1 or n2. // Min returns the smaller of multivalued numbers n1 and n2 or more values.
func (ns *Namespace) Min(n1, n2 any) (float64, error) { func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {
af, erra := cast.ToFloat64E(n1) var value float64
bf, errb := cast.ToFloat64E(n2) for index, input := range inputs {
value, err = cast.ToFloat64E(input)
if erra != nil || errb != nil { if err != nil {
return 0, errors.New("Min operator can't be used with non-float value") err = errors.New("Max operator can't be used with non-float value")
return
} }
if index == 0 {
return math.Min(af, bf), nil minimum = value
continue
}
minimum = math.Min(value, minimum)
}
return
} }
// Mod returns n1 % n2. // Mod returns n1 % n2.
@ -122,9 +133,9 @@ func (ns *Namespace) ModBool(n1, n2 any) (bool, error) {
return res == int64(0), nil return res == int64(0), nil
} }
// Mul multiplies the two numbers n1 and n2. // Mul multiplies the multivalued numbers n1 and n2 or more values.
func (ns *Namespace) Mul(n1, n2 any) (any, error) { func (ns *Namespace) Mul(inputs ...any) (any, error) {
return _math.DoArithmetic(n1, n2, '*') return ns.doArithmetic(inputs, '*')
} }
// Pow returns n1 raised to the power of n2. // 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 return math.Sqrt(af), nil
} }
// Sub subtracts n2 from n1. // Sub subtracts multivalued.
func (ns *Namespace) Sub(n1, n2 any) (any, error) { func (ns *Namespace) Sub(inputs ...any) (any, error) {
return _math.DoArithmetic(n1, n2, '-') 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 var counter uint64

View file

@ -26,23 +26,28 @@ func TestBasicNSArithmetic(t *testing.T) {
ns := New() ns := New()
for _, test := range []struct { type TestCase struct {
fn func(a, b any) (any, error) fn func(inputs ...any) (any, error)
a any values []any
b any
expect any expect any
}{ }
{ns.Add, 4, 2, int64(6)},
{ns.Add, 1.0, "foo", false}, for _, test := range []TestCase{
{ns.Sub, 4, 2, int64(2)}, {ns.Add, []any{4, 2}, int64(6)},
{ns.Sub, 1.0, "foo", false}, {ns.Add, []any{4, 2, 5}, int64(11)},
{ns.Mul, 4, 2, int64(8)}, {ns.Add, []any{1.0, "foo"}, false},
{ns.Mul, 1.0, "foo", false}, {ns.Sub, []any{4, 2}, int64(2)},
{ns.Div, 4, 2, int64(2)}, {ns.Sub, []any{4, 2, 5}, int64(-3)},
{ns.Div, 1.0, "foo", false}, {ns.Sub, []any{1.0, "foo"}, false},
{ns.Mul, []any{4, 2}, int64(8)},
{ns.Mul, []any{4, 2, 5}, int64(40)},
{ns.Mul, []any{1.0, "foo"}, false},
{ns.Div, []any{4, 2}, int64(2)},
{ns.Div, []any{4, 2, 5}, int64(0)},
{ns.Div, []any{1.0, "foo"}, false},
} { } {
result, err := test.fn(test.a, test.b) result, err := test.fn(test.values...)
if b, ok := test.expect.(bool); ok && !b { if b, ok := test.expect.(bool); ok && !b {
c.Assert(err, qt.Not(qt.IsNil)) c.Assert(err, qt.Not(qt.IsNil))
@ -129,11 +134,11 @@ func TestLog(t *testing.T) {
a any a any
expect any expect any
}{ }{
{1, float64(0)}, {1, 0.0},
{3, float64(1.0986)}, {3, 1.0986},
{0, float64(math.Inf(-1))}, {0, math.Inf(-1)},
{1.0, float64(0)}, {1.0, 0.0},
{3.1, float64(1.1314)}, {3.1, 1.1314},
{"abc", false}, {"abc", false},
} { } {
@ -170,9 +175,9 @@ func TestSqrt(t *testing.T) {
a any a any
expect any expect any
}{ }{
{81, float64(9)}, {81, 9.0},
{0.25, float64(0.5)}, {0.25, 0.5},
{0, float64(0)}, {0, 0.0},
{"abc", false}, {"abc", false},
} { } {
@ -329,15 +334,15 @@ func TestPow(t *testing.T) {
b any b any
expect any expect any
}{ }{
{0, 0, float64(1)}, {0, 0, 1.0},
{2, 0, float64(1)}, {2, 0, 1.0},
{2, 3, float64(8)}, {2, 3, 8.0},
{-2, 3, float64(-8)}, {-2, 3, -8.0},
{2, -3, float64(0.125)}, {2, -3, 0.125},
{-2, -3, float64(-0.125)}, {-2, -3, -0.125},
{0.2, 3, float64(0.008)}, {0.2, 3, 0.008},
{2, 0.3, float64(1.2311)}, {2, 0.3, 1.2311},
{0.2, 0.3, float64(0.617)}, {0.2, 0.3, 0.617},
{"aaa", "3", false}, {"aaa", "3", false},
{"2", "aaa", false}, {"2", "aaa", false},
} { } {
@ -364,28 +369,33 @@ func TestMax(t *testing.T) {
ns := New() ns := New()
for _, test := range []struct { type TestCase struct {
a any values []any
b any
expect any expect any
}{ }
{-1, -1, float64(-1)},
{-1, 0, float64(0)},
{-1, 1, float64(1)},
{0, -1, float64(0)},
{0, 0, float64(0)},
{0, 1, float64(1)},
{1, -1, float64(1)},
{1, 0, float64(1)},
{1, 1, float64(1)},
{1.2, 1.23, float64(1.23)},
{-1.2, -1.23, float64(-1.2)},
{0, "a", false},
{"a", 0, false},
{"a", "b", false},
} {
result, err := ns.Max(test.a, test.b) for _, test := range []TestCase{
// two values
{[]any{-1, -1}, -1.0},
{[]any{-1, 0}, 0.0},
{[]any{-1, 1}, 1.0},
{[]any{0, -1}, 0.0},
{[]any{0, 0}, 0.0},
{[]any{0, 1}, 1.0},
{[]any{1, -1}, 1.0},
{[]any{1, 0}, 1.0},
{[]any{1, 1}, 1.0},
{[]any{1.2, 1.23}, 1.23},
{[]any{-1.2, -1.23}, -1.2},
{[]any{0, "a"}, false},
{[]any{"a", 0}, false},
{[]any{"a", "b"}, false},
// multi values
{[]any{-1, -2, -3}, -1.0},
{[]any{1, 2, 3}, 3.0},
{[]any{"a", 2, 3}, false},
} {
result, err := ns.Max(test.values...)
if b, ok := test.expect.(bool); ok && !b { if b, ok := test.expect.(bool); ok && !b {
c.Assert(err, qt.Not(qt.IsNil)) c.Assert(err, qt.Not(qt.IsNil))
@ -403,28 +413,34 @@ func TestMin(t *testing.T) {
ns := New() ns := New()
for _, test := range []struct { type TestCase struct {
a any values []any
b any
expect any expect any
}{ }
{-1, -1, float64(-1)},
{-1, 0, float64(-1)}, for _, test := range []TestCase{
{-1, 1, float64(-1)}, // two values
{0, -1, float64(-1)}, {[]any{-1, -1}, -1.0},
{0, 0, float64(0)}, {[]any{-1, 0}, -1.0},
{0, 1, float64(0)}, {[]any{-1, 1}, -1.0},
{1, -1, float64(-1)}, {[]any{0, -1}, -1.0},
{1, 0, float64(0)}, {[]any{0, 0}, 0.0},
{1, 1, float64(1)}, {[]any{0, 1}, 0.0},
{1.2, 1.23, float64(1.2)}, {[]any{1, -1}, -1.0},
{-1.2, -1.23, float64(-1.23)}, {[]any{1, 0}, 0.0},
{0, "a", false}, {[]any{1, 1}, 1.0},
{"a", 0, false}, {[]any{1.2, 1.23}, 1.2},
{"a", "b", false}, {[]any{-1.2, -1.23}, -1.23},
{[]any{0, "a"}, false},
{[]any{"a", 0}, false},
{[]any{"a", "b"}, false},
// multi values
{[]any{-1, -2, -3}, -3.0},
{[]any{1, 2, 3}, 1.0},
{[]any{"a", 2, 3}, false},
} { } {
result, err := ns.Min(test.a, test.b) result, err := ns.Min(test.values...)
if b, ok := test.expect.(bool); ok && !b { if b, ok := test.expect.(bool); ok && !b {
c.Assert(err, qt.Not(qt.IsNil)) c.Assert(err, qt.Not(qt.IsNil))