Skip to content

Commit

Permalink
tpl/math: Allow multi numbers in add, sub, mul, div, min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
septs authored Mar 11, 2023
1 parent 04b9811 commit 84201e8
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 109 deletions.
14 changes: 7 additions & 7 deletions docs/content/en/functions/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Math
description: Hugo provides mathematical operators in templates.
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2020-02-23
lastmod: 2023-03-11
keywords: [math, operators]
categories: [functions]
menu:
Expand All @@ -21,22 +21,22 @@ aliases: []

| 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` |
| `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` |
| `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` |
| `div` | Divides two numbers. | `{{div 6 3}}` → `2` |
| `div` | Divides multivalued numbers. | `{{div 6 3}}` → `2` |
| | | `{{div 6 4}}` → `1` |
| | *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` |
| `modBool` | Boolean of modulus of two integers. Evaluates to `true` if result equals 0. | `{{modBool 15 3}}` → `true` |
| `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.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.Min` | Returns the smaller of two numbers. | `{{math.Min 1 2}}` → `1` |
| `math.Max` | Returns the greater of multivalued numbers. | `{{math.Max 1 2}}` → `2` |
| `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.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` |
86 changes: 54 additions & 32 deletions tpl/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"sync/atomic"

_math "github.com/gohugoio/hugo/common/math"

"github.com/spf13/cast"
)

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
156 changes: 86 additions & 70 deletions tpl/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ func TestBasicNSArithmetic(t *testing.T) {

ns := New()

for _, test := range []struct {
fn func(a, b any) (any, error)
a any
b any
type TestCase struct {
fn func(inputs ...any) (any, error)
values []any
expect any
}{
{ns.Add, 4, 2, int64(6)},
{ns.Add, 1.0, "foo", false},
{ns.Sub, 4, 2, int64(2)},
{ns.Sub, 1.0, "foo", false},
{ns.Mul, 4, 2, int64(8)},
{ns.Mul, 1.0, "foo", false},
{ns.Div, 4, 2, int64(2)},
{ns.Div, 1.0, "foo", false},
}

for _, test := range []TestCase{
{ns.Add, []any{4, 2}, int64(6)},
{ns.Add, []any{4, 2, 5}, int64(11)},
{ns.Add, []any{1.0, "foo"}, false},
{ns.Sub, []any{4, 2}, int64(2)},
{ns.Sub, []any{4, 2, 5}, int64(-3)},
{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 {
c.Assert(err, qt.Not(qt.IsNil))
Expand Down Expand Up @@ -129,11 +134,11 @@ func TestLog(t *testing.T) {
a any
expect any
}{
{1, float64(0)},
{3, float64(1.0986)},
{0, float64(math.Inf(-1))},
{1.0, float64(0)},
{3.1, float64(1.1314)},
{1, 0.0},
{3, 1.0986},
{0, math.Inf(-1)},
{1.0, 0.0},
{3.1, 1.1314},
{"abc", false},
} {

Expand Down Expand Up @@ -170,9 +175,9 @@ func TestSqrt(t *testing.T) {
a any
expect any
}{
{81, float64(9)},
{0.25, float64(0.5)},
{0, float64(0)},
{81, 9.0},
{0.25, 0.5},
{0, 0.0},
{"abc", false},
} {

Expand Down Expand Up @@ -329,15 +334,15 @@ func TestPow(t *testing.T) {
b any
expect any
}{
{0, 0, float64(1)},
{2, 0, float64(1)},
{2, 3, float64(8)},
{-2, 3, float64(-8)},
{2, -3, float64(0.125)},
{-2, -3, float64(-0.125)},
{0.2, 3, float64(0.008)},
{2, 0.3, float64(1.2311)},
{0.2, 0.3, float64(0.617)},
{0, 0, 1.0},
{2, 0, 1.0},
{2, 3, 8.0},
{-2, 3, -8.0},
{2, -3, 0.125},
{-2, -3, -0.125},
{0.2, 3, 0.008},
{2, 0.3, 1.2311},
{0.2, 0.3, 0.617},
{"aaa", "3", false},
{"2", "aaa", false},
} {
Expand All @@ -364,28 +369,33 @@ func TestMax(t *testing.T) {

ns := New()

for _, test := range []struct {
a any
b any
type TestCase struct {
values []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 {
c.Assert(err, qt.Not(qt.IsNil))
Expand All @@ -403,28 +413,34 @@ func TestMin(t *testing.T) {

ns := New()

for _, test := range []struct {
a any
b any
type TestCase struct {
values []any
expect any
}{
{-1, -1, float64(-1)},
{-1, 0, float64(-1)},
{-1, 1, float64(-1)},
{0, -1, float64(-1)},
{0, 0, float64(0)},
{0, 1, float64(0)},
{1, -1, float64(-1)},
{1, 0, float64(0)},
{1, 1, float64(1)},
{1.2, 1.23, float64(1.2)},
{-1.2, -1.23, float64(-1.23)},
{0, "a", false},
{"a", 0, false},
{"a", "b", false},
}

for _, test := range []TestCase{
// two values
{[]any{-1, -1}, -1.0},
{[]any{-1, 0}, -1.0},
{[]any{-1, 1}, -1.0},
{[]any{0, -1}, -1.0},
{[]any{0, 0}, 0.0},
{[]any{0, 1}, 0.0},
{[]any{1, -1}, -1.0},
{[]any{1, 0}, 0.0},
{[]any{1, 1}, 1.0},
{[]any{1.2, 1.23}, 1.2},
{[]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 {
c.Assert(err, qt.Not(qt.IsNil))
Expand Down

0 comments on commit 84201e8

Please sign in to comment.