feat(rules): make in/within optional in the en deadline rule

This commit is contained in:
dualfroz committed 2026-10-02 16:04:11 +02:00
1 parent 751720c04d
commit 52f50c7639
2 files changed
+33 -2

No files matched your search

+13 -2
View File
@@ -11,12 +11,23 @@ import (
"github.com/pkg/errors" "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 overwrite := s == rules.Override
prefix := "(within|in)"
if len(opts) > 0 && opts[0].Aggressive {
prefix += "?"
}
return &rules.F{ return &rules.F{
RegExp: regexp.MustCompile( RegExp: regexp.MustCompile(
"(?i)(?:\\W|^)(within|in)\\s*" + "(?i)(?:\\W|^)" + prefix + "\\s*" +
"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" + "(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" +
"(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?)\\s*" + "(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?)\\s*" +
"(?:\\W|$)"), "(?:\\W|$)"),
+20
View File
@@ -31,3 +31,23 @@ func TestDeadline(t *testing.T) {
ApplyFixtures(t, "en.Deadline", w, fixt) 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}})
}