Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ templating.

## Strings

| Name | Arguments | Returns | Notes |
| ------------- | ------------- | -------- | -------- |
| title | string |[strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. |
| match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. |
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |
| since | time.Time | [time.Duration](https://pkg.go.dev/time#Since), returns the duration of how much time passed from the provided time till the current system time. |
| humanizeDuration | number or string | Returns a human-readable string representing the duration, and the error if it happened. |
| Name | Arguments | Returns | Notes |
|-------------------|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -------- |
| title | string | [strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. |
| match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. |
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |
| since | time.Time | [time.Duration](https://pkg.go.dev/time#Since), returns the duration of how much time passed from the provided time till the current system time. |
| humanizeDuration | number or string | Returns a human-readable string representing the duration, and the error if it happened. |
| humanizeTimestamp | number or string | Returns a human-readable string representing the Unix timestamp provided as an argument. |
5 changes: 3 additions & 2 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ var DefaultFuncs = FuncMap{
}
return t.In(loc), nil
},
"since": time.Since,
"humanizeDuration": commonTemplates.HumanizeDuration,
"since": time.Since,
"humanizeDuration": commonTemplates.HumanizeDuration,
"humanizeTimestamp": commonTemplates.HumanizeTimestamp,
}

// Pair is a key/value string pair.
Expand Down
29 changes: 29 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package template

import (
tmplhtml "html/template"
"math"
"net/url"
"sync"
"testing"
Expand Down Expand Up @@ -556,6 +557,34 @@ func TestTemplateFuncs(t *testing.T) {
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []uint{0, 1, 1234567},
exp: "0s:1s:14d 6h 56m 7s:",
}, {
title: "Template using HumanizeTimestamp - int.",
in: "{{ range . }}{{ humanizeTimestamp . }}:{{ end }}",
data: []int64{0, -1, 1, 1234567, 9223372036},
exp: "1970-01-01 00:00:00 +0000 UTC:1969-12-31 23:59:59 +0000 UTC:1970-01-01 00:00:01 +0000 UTC:1970-01-15 06:56:07 +0000 UTC:2262-04-11 23:47:16 +0000 UTC:",
}, {
title: "Template using HumanizeTimestamp - uint.",
in: "{{ range . }}{{ humanizeTimestamp . }}:{{ end }}",
data: []uint64{0, 1, 1234567, 9223372036},
exp: "1970-01-01 00:00:00 +0000 UTC:1970-01-01 00:00:01 +0000 UTC:1970-01-15 06:56:07 +0000 UTC:2262-04-11 23:47:16 +0000 UTC:",
}, {
title: "Template using HumanizeTimestamp - int with error.",
in: "{{ range . }}{{ humanizeTimestamp . }}:{{ end }}",
data: []int64{math.MinInt64, math.MaxInt64},
expErr: `template: :1:16: executing "" at <humanizeTimestamp .>: error calling humanizeTimestamp: -9.223372036854776e+18 cannot be represented as a nanoseconds timestamp since it overflows int64`,
}, {
title: "Template using HumanizeTimestamp - uint with error.",
in: "{{ range . }}{{ humanizeTimestamp . }}:{{ end }}",
data: []uint64{math.MaxUint64},
expErr: `template: :1:16: executing "" at <humanizeTimestamp .>: error calling humanizeTimestamp: 1.8446744073709552e+19 cannot be represented as a nanoseconds timestamp since it overflows int64`,
}, {
title: "Template using HumanizeTimestamp - model.SampleValue input - float64",
in: "{{ 1435065584.128 | humanizeTimestamp }}",
exp: "2015-06-23 13:19:44.128 +0000 UTC",
}, {
title: "Template using HumanizeTimestamp - model.SampleValue input - string.",
in: `{{ "1435065584.128" | humanizeTimestamp }}`,
exp: "2015-06-23 13:19:44.128 +0000 UTC",
}, {
title: "Template using since",
in: "{{ . | since | humanizeDuration }}",
Expand Down