Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include get-date helper function #262

Merged
merged 8 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion internal/adapter/handlebars/handlebars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (

func Init(supportsUTF8 bool, logger util.Logger) {
helpers.RegisterConcat()
helpers.RegisterSubstring()
helpers.RegisterDate(logger)
helpers.RegisterGetDate(logger)
helpers.RegisterJoin()
helpers.RegisterJSON(logger)
helpers.RegisterList(supportsUTF8)
helpers.RegisterPrepend(logger)
helpers.RegisterShell(logger)
helpers.RegisterSubstring()
}

// Template renders a parsed handlebars template.
Expand Down
8 changes: 8 additions & 0 deletions internal/adapter/handlebars/handlebars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ func TestDateHelper(t *testing.T) {
testString(t, "{{date now 'elapsed'}}", context, "13 years ago")
}

func TestGetDateHelper(t *testing.T) {
context := map[string]interface{}{"now": time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)}
testString(t, "{{get-date \"last week\"}}", context, "2022-09-28 00:00:00 +0000 UTC")
testString(t, "{{get-date \"next week\"}}", context, "2022-10-12 00:00:00 +0000 UTC")
testString(t, "{{date (get-date \"next week\") \"%Y-W%W\"}}", context, "2022-W41")
testString(t, "{{date (get-date \"last week\") \"%Y-W%W\"}}", context, "2022-W39")
}

func TestShellHelper(t *testing.T) {
// block is passed as piped input
testString(t,
Expand Down
23 changes: 23 additions & 0 deletions internal/adapter/handlebars/helpers/getdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helpers

import (
"time"

"github.com/aymerick/raymond"
"github.com/mickael-menu/zk/internal/util"
"github.com/pkg/errors"
naturaldate "github.com/tj/go-naturaldate"
)

// RegisterGetDate registers the {{getdate}} template helper to use the `naturaldate` package to generate time.Time based on language strings.
// This can be used in combination with the `date` helper to generate dates in the user's language.
// {{date (get-date "last week") "timestamp"}}
func RegisterGetDate(logger util.Logger) {
raymond.RegisterHelper("get-date", func(natural string) time.Time {
date, err := naturaldate.Parse(natural, time.Now())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this in my first review, but you can use the helper TimeFromNatural() (which is part of this project) instead of using directly naturaldate.Parse(). This way it will be able to parse more formats.

if err != nil {
logger.Err(errors.Wrap(err, "the {{get-date}} template helper failed to parse the date"))
}
return date
})
}
2 changes: 1 addition & 1 deletion internal/util/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package date
import (
"time"

"github.com/tj/go-naturaldate"
naturaldate "github.com/tj/go-naturaldate"
)

// Provider returns a date instance.
Expand Down