Skip to content

Commit 34c5667

Browse files
artem-sidorenkobep
authored andcommitted
tpl/math: Add log function
It might be very useful for building tag clouds.
1 parent 41805dc commit 34c5667

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

‎docs/content/templates/functions.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ favicon.ico: {{.Width}} x {{.Height}}
432432
<td><code>{{div 6 3}}</code> → 2</td>
433433
</tr>
434434

435+
<tr>
436+
<td><code>math.Log</code></td>
437+
<td>Natural logarithm of one float.</td>
438+
<td><code>{{math.Log 1.0}}</code> → 0</td>
439+
</tr>
440+
435441
<tr>
436442
<td><code>mod</code></td>
437443
<td>Modulus of two integers.</td>
@@ -714,7 +720,7 @@ e.g.
714720

715721
* `{{ "this is a text" | truncate 10 " ..." }}``this is a ...`
716722
* `{{ "<em>Keep my HTML</em>" | safeHTML | truncate 10 }}``<em>Keep my …</em>`
717-
* `{{ "With [Markdown](#markdown) inside." | markdownify | truncate 10 }}``With <a href='#markdown'>Markdown …</a>`
723+
* `{{ "With [Markdown](#markdown) inside." | markdownify | truncate 10 }}``With <a href='#markdown'>Markdown …</a>`
718724

719725
### split
720726

‎tpl/math/init.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func init() {
4343
},
4444
)
4545

46+
ns.AddMethodMapping(ctx.Log,
47+
nil,
48+
[][2]string{
49+
{"{{math.Log 1}}", "0"},
50+
},
51+
)
52+
4653
ns.AddMethodMapping(ctx.Mod,
4754
[]string{"mod"},
4855
[][2]string{

‎tpl/math/math.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ package math
1515

1616
import (
1717
"errors"
18+
"math"
1819
"reflect"
20+
21+
"github.com/spf13/cast"
1922
)
2023

2124
// New returns a new instance of the math-namespaced template functions.
@@ -34,6 +37,16 @@ func (ns *Namespace) Div(a, b interface{}) (interface{}, error) {
3437
return DoArithmetic(a, b, '/')
3538
}
3639

40+
func (ns *Namespace) Log(a interface{}) (float64, error) {
41+
af, err := cast.ToFloat64E(a)
42+
43+
if err != nil {
44+
return 0, errors.New("Log operator can't be used with non integer or float value")
45+
}
46+
47+
return math.Log(af), nil
48+
}
49+
3750
// Mod returns a % b.
3851
func (ns *Namespace) Mod(a, b interface{}) (int64, error) {
3952
av := reflect.ValueOf(a)

‎tpl/math/math_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package math
1515

1616
import (
1717
"fmt"
18+
"math"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -142,6 +143,42 @@ func TestDoArithmetic(t *testing.T) {
142143
}
143144
}
144145

146+
func TestLog(t *testing.T) {
147+
t.Parallel()
148+
149+
ns := New()
150+
151+
for i, test := range []struct {
152+
a interface{}
153+
expect interface{}
154+
}{
155+
{1, float64(0)},
156+
{3, float64(1.0986)},
157+
{0, float64(math.Inf(-1))},
158+
{1.0, float64(0)},
159+
{3.1, float64(1.1314)},
160+
{"abc", false},
161+
} {
162+
errMsg := fmt.Sprintf("[%d] %v", i, test)
163+
164+
result, err := ns.Log(test.a)
165+
166+
if b, ok := test.expect.(bool); ok && !b {
167+
require.Error(t, err, errMsg)
168+
continue
169+
}
170+
171+
// we compare only 4 digits behind point if its a real float
172+
// otherwise we usually get different float values on the last positions
173+
if result != math.Inf(-1) {
174+
result = float64(int(result*10000)) / 10000
175+
}
176+
177+
require.NoError(t, err, errMsg)
178+
assert.Equal(t, test.expect, result, errMsg)
179+
}
180+
}
181+
145182
func TestMod(t *testing.T) {
146183
t.Parallel()
147184

0 commit comments

Comments
 (0)