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

Support strings containing "from now" #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 15 additions & 3 deletions rules/en/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ import (
"github.com/pkg/errors"
)

var (
pointInTime = "(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" +
"(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?)\\s*"
)

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

return &rules.F{
RegExp: regexp.MustCompile(
"(?i)(?:\\W|^)(within|in)\\s*" +
"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" +
"(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?)\\s*" +
"(?i)(?:\\W|^)" +
"(within|in)\\s*" + pointInTime + "|" + pointInTime + "\\s+(from\\s+now)" +
"(?:\\W|$)"),
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {

numStr := strings.TrimSpace(m.Captures[1])

if numStr == "" {
numStr = strings.TrimSpace(m.Captures[3])
}

var num int
var err error

Expand All @@ -44,6 +52,10 @@ func Deadline(s rules.Strategy) rules.Rule {

exponent := strings.TrimSpace(m.Captures[2])

if exponent == "" {
exponent = strings.TrimSpace(m.Captures[4])
}

if !strings.Contains(numStr, "half") {
switch {
case strings.Contains(exponent, "second"):
Expand Down
14 changes: 14 additions & 0 deletions rules/en/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,24 @@ func TestDeadline(t *testing.T) {
{"within a few months", 0, "within a few months", 91 * 24 * time.Hour},
{"within one year", 0, "within one year", 366 * 24 * time.Hour},
{"in a week", 0, "in a week", 7 * 24 * time.Hour},
{"7 days from now", 0, "7 days from now", 7 * 24 * time.Hour},
{"a week from now", 0, "a week from now", 7 * 24 * time.Hour},
}

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

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

func TestDeadlineNil(t *testing.T) {
fixt := []Fixture{
{"5 minutes", 0, "", 0},
{"a month", 0, "", 0},
}

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

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