Skip to content
Open
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
15 changes: 13 additions & 2 deletions rules/en/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ import (
"github.com/pkg/errors"
)

func Deadline(s rules.Strategy) rules.Rule {
// DeadlineOptions configures the Deadline rule.
type DeadlineOptions struct {
// Aggressive makes the "in"/"within" prefix optional, so "2 hours" matches too.
Aggressive bool
}

func Deadline(s rules.Strategy, opts ...DeadlineOptions) rules.Rule {
overwrite := s == rules.Override

prefix := "(within|in)"
if len(opts) > 0 && opts[0].Aggressive {
prefix += "?"
}

return &rules.F{
RegExp: regexp.MustCompile(
"(?i)(?:\\W|^)(within|in)\\s*" +
"(?i)(?:\\W|^)" + prefix + "\\s*" +
"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" +
"(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?)\\s*" +
"(?:\\W|$)"),
Expand Down
20 changes: 20 additions & 0 deletions rules/en/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,23 @@ func TestDeadline(t *testing.T) {

ApplyFixtures(t, "en.Deadline", w, fixt)
}

func TestDeadlineAggressive(t *testing.T) {
fixt := []Fixture{
{"2 hours", 0, "2 hours", 2 * time.Hour},
{"remind me 4 days later", 10, "4 days", 4 * 24 * time.Hour},
{"a week", 0, "a week", 7 * 24 * time.Hour},
{"within half an hour", 0, "within half an hour", time.Hour / 2},
{"call me back in 5 minutes", 13, "in 5 minutes", 5 * time.Minute},
}

w := when.New(nil)
w.Add(en.Deadline(rules.Skip, en.DeadlineOptions{Aggressive: true}))

ApplyFixtures(t, "en.Deadline aggressive", w, fixt)

w = when.New(nil)
w.Add(en.Deadline(rules.Skip))

ApplyFixturesNil(t, "en.Deadline", w, []Fixture{{"2 hours", 0, "", 0}})
}
Loading