-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildin_math.go
118 lines (103 loc) · 3.57 KB
/
buildin_math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package template
import (
"fmt"
"log"
"github.com/kaptinlin/filter"
)
func init() {
// Register all math filters
filtersToRegister := map[string]FilterFunc{
"abs": absFilter,
"atLeast": atLeastFilter,
"atMost": atMostFilter,
"round": roundFilter,
"floor": floorFilter,
"ceil": ceilFilter,
"plus": plusFilter,
"minus": minusFilter,
"times": timesFilter,
"divide": divideFilter,
"modulo": moduloFilter,
}
for name, filterFunc := range filtersToRegister {
if err := RegisterFilter(name, filterFunc); err != nil {
log.Printf("Error registering filter %s: %v", name, err)
}
}
}
// absFilter calculates the absolute value of a number.
func absFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Abs(value)
}
// atLeastFilter ensures the number is at least as large as the minimum value provided.
func atLeastFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: atLeast filter requires one argument", ErrInsufficientArgs)
}
min := args[0]
return filter.AtLeast(value, min)
}
// atMostFilter ensures the number is no larger than the maximum value provided.
func atMostFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: atMost filter requires one argument", ErrInsufficientArgs)
}
max := args[0]
return filter.AtMost(value, max)
}
// roundFilter rounds the input to the specified number of decimal places.
func roundFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: round filter requires one argument for precision", ErrInsufficientArgs)
}
precision := args[0]
return filter.Round(value, precision)
}
// floorFilter rounds the input down to the nearest whole number.
func floorFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Floor(value)
}
// ceilFilter rounds the input up to the nearest whole number.
func ceilFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Ceil(value)
}
// plusFilter adds two numbers.
func plusFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: plus filter requires one argument", ErrInsufficientArgs)
}
addend := args[0]
return filter.Plus(value, addend)
}
// minusFilter subtracts the second value from the first.
func minusFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: minus filter requires one argument", ErrInsufficientArgs)
}
subtrahend := args[0]
return filter.Minus(value, subtrahend)
}
// timesFilter multiplies the first value by the second.
func timesFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: times filter requires one argument", ErrInsufficientArgs)
}
multiplier := args[0]
return filter.Times(value, multiplier)
}
// divideFilter divides the first value by the second.
func divideFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: divide filter requires one argument", ErrInsufficientArgs)
}
divisor := args[0]
return filter.Divide(value, divisor)
}
// moduloFilter returns the remainder of the division of the first value by the second.
func moduloFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: modulo filter requires one argument", ErrInsufficientArgs)
}
modulus := args[0]
return filter.Modulo(value, modulus)
}