]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Add math.Abs
authorOleksandr Redko <Oleksandr_Redko@epam.com>
Tue, 16 May 2023 16:32:07 +0000 (19:32 +0300)
committerGitHub <noreply@github.com>
Tue, 16 May 2023 16:32:07 +0000 (18:32 +0200)
Fixes #10941.

docs/content/en/functions/math.md
tpl/math/init.go
tpl/math/math.go
tpl/math/math_test.go

index 4abf34d1d517a05a80e7364b482ce8abed33cc34..e0222f3b29cc24652c91a45708799f5e59cc2f27 100644 (file)
@@ -3,7 +3,7 @@ title: Math
 description: Hugo provides mathematical operators in templates.
 date: 2017-02-01
 publishdate: 2017-02-01
-lastmod: 2023-03-11
+lastmod: 2023-05-15
 keywords: [math, operators]
 categories: [functions]
 menu:
@@ -31,6 +31,7 @@ aliases: []
 |              | *If one of the numbers is a float, the result is a float.*                  | `{{ div 6 4.0 }}` &rarr; `1.5`      |
 | `mod`        | Modulus of two integers.                                                    | `{{ mod 15 3 }}` &rarr; `0`         |
 | `modBool`    | Boolean of modulus of two integers. Evaluates to `true` if result equals 0. | `{{ modBool 15 3 }}` &rarr; `true`  |
+| `math.Abs`   | Returns the absolute value of the given number.                             | `{{ math.Abs -2.1 }}` &rarr; `2.1`  |
 | `math.Ceil`  | Returns the least integer value greater than or equal to the given number.  | `{{ math.Ceil 2.1 }}` &rarr; `3`    |
 | `math.Floor` | Returns the greatest integer value less than or equal to the given number.  | `{{ math.Floor 1.9 }}` &rarr; `1`   |
 | `math.Log`   | Returns the natural logarithm of the given number.                          | `{{ math.Log 42 }}` &rarr; `3.737`  |
index 67aa95f41641d02709d959ab32a0122b6d7fbf79..8596ff647051e18b039479677f61339d8a21c86b 100644 (file)
@@ -31,6 +31,13 @@ func init() {
                        Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
                }
 
+               ns.AddMethodMapping(ctx.Abs,
+                       nil,
+                       [][2]string{
+                               {"{{ math.Abs -2.1 }}", "2.1"},
+                       },
+               )
+
                ns.AddMethodMapping(ctx.Add,
                        []string{"add"},
                        [][2]string{
index 67c6d06c5c5474279e9534f7b906eddd4b92ef4f..d73f212a658904ce8ca33be6deee291ee3051f02 100644 (file)
@@ -35,6 +35,16 @@ func New() *Namespace {
 // Namespace provides template functions for the "math" namespace.
 type Namespace struct{}
 
+// Abs returns the absolute value of n.
+func (ns *Namespace) Abs(n any) (float64, error) {
+       af, err := cast.ToFloat64E(n)
+       if err != nil {
+               return 0, errors.New("the math.Abs function requires a numeric argument")
+       }
+
+       return math.Abs(af), nil
+}
+
 // Add adds the multivalued addends n1 and n2 or more values.
 func (ns *Namespace) Add(inputs ...any) (any, error) {
        return ns.doArithmetic(inputs, '+')
index 3e83405fd602391e31166dc6e816f667470dfc64..fad86938df003b58b428f7c9923ecca787d1a32e 100644 (file)
@@ -63,6 +63,33 @@ func TestBasicNSArithmetic(t *testing.T) {
        }
 }
 
+func TestAbs(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+       ns := New()
+
+       for _, test := range []struct {
+               x      any
+               expect any
+       }{
+               {0.0, 0.0},
+               {1.5, 1.5},
+               {-1.5, 1.5},
+               {-2, 2.0},
+               {"abc", false},
+       } {
+               result, err := ns.Abs(test.x)
+
+               if b, ok := test.expect.(bool); ok && !b {
+                       c.Assert(err, qt.Not(qt.IsNil))
+                       continue
+               }
+
+               c.Assert(err, qt.IsNil)
+               c.Assert(result, qt.Equals, test.expect)
+       }
+}
+
 func TestCeil(t *testing.T) {
        t.Parallel()
        c := qt.New(t)