-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildin_number.go
36 lines (30 loc) · 918 Bytes
/
buildin_number.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
package template
import (
"fmt"
"log"
"github.com/kaptinlin/filter"
)
func init() {
// Register number-related filters
filtersToRegister := map[string]FilterFunc{
"number": numberFilter,
"bytes": bytesFilter,
}
for name, filterFunc := range filtersToRegister {
if err := RegisterFilter(name, filterFunc); err != nil {
log.Printf("Error registering filter %s: %v", name, err)
}
}
}
// numberFilter formats a numeric value according to the specified format string.
func numberFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: number filter requires a format string", ErrInsufficientArgs)
}
format := args[0]
return filter.Number(value, format)
}
// bytesFilter converts a numeric value into a human-readable byte format.
func bytesFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Bytes(value)
}