-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb47a0c
commit c72db38
Showing
18 changed files
with
1,213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package nl | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/olebedev/when/rules" | ||
) | ||
|
||
func CasualDate(s rules.Strategy) rules.Rule { | ||
overwrite := s == rules.Override | ||
|
||
return &rules.F{ | ||
RegExp: regexp.MustCompile("(?i)(?:\\W|^)(nu|vandaag|vanavond|vannacht|afgelopen\\s*nacht|morgen|gister|gisteren)(ochtend|morgen|middag|avond)?(?:\\W|$)"), | ||
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { | ||
lower := strings.ToLower(strings.TrimSpace(m.String())) | ||
|
||
if regexp.MustCompile("ochtend|\\s*morgen|middag|avond").MatchString(lower) { | ||
switch { | ||
case strings.Contains(lower, "ochtend"), regexp.MustCompile("(?i)(?:\\W|^)(\\s*morgen)(?:\\W|$)").MatchString(lower): | ||
if o.Morning != 0 { | ||
c.Hour = &o.Morning | ||
} else { | ||
c.Hour = pointer.ToInt(8) | ||
} | ||
case strings.Contains(lower, "middag"): | ||
if o.Afternoon != 0 { | ||
c.Hour = &o.Afternoon | ||
} else { | ||
c.Hour = pointer.ToInt(15) | ||
} | ||
case strings.Contains(lower, "avond"): | ||
if o.Evening != 0 { | ||
c.Hour = &o.Evening | ||
} else { | ||
c.Hour = pointer.ToInt(18) | ||
} | ||
} | ||
} | ||
|
||
switch { | ||
case strings.Contains(lower, "vannacht"): | ||
if c.Hour == nil && c.Minute == nil || overwrite { | ||
c.Hour = pointer.ToInt(23) | ||
c.Minute = pointer.ToInt(0) | ||
} | ||
case strings.Contains(lower, "vandaag"): | ||
// c.Hour = pointer.ToInt(18) | ||
case strings.Contains(lower, "morgen"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration += time.Hour * 24 | ||
} | ||
case strings.Contains(lower, "gister"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration -= time.Hour * 24 | ||
} | ||
case strings.Contains(lower, "afgelopen nacht"): | ||
if (c.Hour == nil && c.Duration == 0) || overwrite { | ||
c.Hour = pointer.ToInt(23) | ||
c.Duration -= time.Hour * 24 | ||
} | ||
} | ||
|
||
return true, nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package nl_test | ||
|
||
import ( | ||
"github.com/olebedev/when/rules/nl" | ||
"testing" | ||
"time" | ||
|
||
"github.com/olebedev/when" | ||
"github.com/olebedev/when/rules" | ||
) | ||
|
||
func TestCasualDate(t *testing.T) { | ||
fixt := []Fixture{ | ||
{"De deadline is nu, ok", 15, "nu", 0}, | ||
{"De deadline is vandaag", 15, "vandaag", 0}, | ||
{"De deadline is vannacht", 15, "vannacht", 23 * time.Hour}, | ||
{"De deadline is morgenavond", 15, "morgenavond", (18 + 24) * time.Hour}, | ||
{"De deadline is gisteravond", 15, "gisteravond", -((24 - 18) * time.Hour)}, | ||
{"De deadline is gisteren", 15, "gisteren", -(time.Hour * 24)}, | ||
} | ||
|
||
w := when.New(nil) | ||
w.Add(nl.CasualDate(rules.Skip)) | ||
|
||
ApplyFixtures(t, "nl.CasualDate", w, fixt) | ||
} | ||
|
||
func TestCasualTime(t *testing.T) { | ||
fixt := []Fixture{ | ||
{"De deadline was deze morgen", 16, "deze morgen", 8 * time.Hour}, | ||
{"De deadline was tussen de middag", 16, "tussen de middag", 12 * time.Hour}, | ||
{"De deadline was deze middag", 16, "deze middag", 15 * time.Hour}, | ||
{"De deadline was deze avond", 16, "deze avond", 18 * time.Hour}, | ||
{"De deadline is donderdagavond", 15, "donderdagavond", (18 + 24) * time.Hour}, | ||
{"De deadline is vrijdagavond", 15, "vrijdagavond", (18 + 24*2) * time.Hour}, | ||
} | ||
|
||
w := when.New(nil) | ||
w.Add(nl.CasualTime(rules.Skip)) | ||
|
||
ApplyFixtures(t, "nl.CasualTime", w, fixt) | ||
} | ||
|
||
func TestCasualDateCasualTime(t *testing.T) { | ||
fixt := []Fixture{ | ||
{"De deadline is morgenmiddag", 15, "morgenmiddag", (15 + 24) * time.Hour}, | ||
{"De deadline is morgenavond", 15, "morgenavond", (18 + 24) * time.Hour}, | ||
} | ||
|
||
w := when.New(nil) | ||
w.Add( | ||
nl.CasualDate(rules.Skip), | ||
nl.CasualTime(rules.Override), | ||
) | ||
|
||
ApplyFixtures(t, "nl.CasualDate|nl.CasualTime", w, fixt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package nl | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/olebedev/when/rules" | ||
) | ||
|
||
func CasualTime(s rules.Strategy) rules.Rule { | ||
overwrite := s == rules.Override | ||
|
||
return &rules.F{ | ||
RegExp: regexp.MustCompile(`(?i)(?:\W|^)((deze|tussen de |maandag|dinsdag|woensdag|donderdag|vrijdag|zaterdag|zondag| )\s*(ochtend|morgen|middag|avond))`), | ||
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { | ||
|
||
lower := strings.ToLower(strings.TrimSpace(m.String())) | ||
|
||
if (c.Weekday != nil || c.Hour != nil || c.Minute != nil) && !overwrite { | ||
return false, nil | ||
} | ||
|
||
if regexp.MustCompile("(maandag|dinsdag|woensdag|donderdag|vrijdag|zaterdag|zondag)").MatchString(lower) { | ||
weekday := -1 | ||
|
||
switch { | ||
case strings.Contains(lower, "maandag"): | ||
weekday = 1 | ||
case strings.Contains(lower, "dinsdag"): | ||
weekday = 2 | ||
case strings.Contains(lower, "woensdag"): | ||
weekday = 3 | ||
case strings.Contains(lower, "donderdag"): | ||
weekday = 4 | ||
case strings.Contains(lower, "vrijdag"): | ||
weekday = 5 | ||
case strings.Contains(lower, "zaterdag"): | ||
weekday = 6 | ||
case strings.Contains(lower, "zondag"): | ||
weekday = 7 | ||
} | ||
|
||
if weekday != -1 { | ||
c.Duration += time.Hour * 24 * time.Duration((weekday+7-(int(ref.Weekday())))%7) | ||
} | ||
} | ||
|
||
switch { | ||
case strings.Contains(lower, "middag") && !strings.Contains(lower, "tussen de middag"): | ||
if o.Afternoon != 0 { | ||
c.Hour = &o.Afternoon | ||
} else { | ||
c.Hour = pointer.ToInt(15) | ||
} | ||
c.Minute = pointer.ToInt(0) | ||
case strings.Contains(lower, "avond"): | ||
if o.Evening != 0 { | ||
c.Hour = &o.Evening | ||
} else { | ||
c.Hour = pointer.ToInt(18) | ||
} | ||
c.Minute = pointer.ToInt(0) | ||
case strings.Contains(lower, "ochtend"), strings.Contains(lower, "morgen"): | ||
if o.Morning != 0 { | ||
c.Hour = &o.Morning | ||
} else { | ||
c.Hour = pointer.ToInt(8) | ||
} | ||
c.Minute = pointer.ToInt(0) | ||
case strings.Contains(lower, "tussen de middag"): | ||
c.Hour = pointer.ToInt(12) | ||
c.Minute = pointer.ToInt(0) | ||
} | ||
|
||
return true, nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package nl | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/olebedev/when/rules" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func Deadline(s rules.Strategy) rules.Rule { | ||
overwrite := s == rules.Override | ||
|
||
return &rules.F{ | ||
RegExp: regexp.MustCompile( | ||
"(?i)(?:\\W|^)(binnen|in|over|na)\\s*" + | ||
"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|een(?:\\s*(paar|half|halve))?)\\s*" + | ||
"(seconden?|minuut|minuten|uur|uren|dag|dagen|week|weken|maand|maanden|jaar|jaren)\\s*" + | ||
"(?:\\W|$)"), | ||
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { | ||
numStr := strings.TrimSpace(m.Captures[1]) | ||
|
||
var num int | ||
var err error | ||
|
||
if n, ok := INTEGER_WORDS[numStr]; ok { | ||
num = n | ||
} else if numStr == "een" { | ||
num = 1 | ||
} else if strings.Contains(numStr, "paar") { | ||
num = 3 | ||
} else if strings.Contains(numStr, "half") || strings.Contains(numStr, "halve") { | ||
// pass | ||
} else { | ||
num, err = strconv.Atoi(numStr) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "convert '%s' to int", numStr) | ||
} | ||
} | ||
|
||
exponent := strings.TrimSpace(m.Captures[3]) | ||
|
||
if !strings.Contains(numStr, "half") && !strings.Contains(numStr, "halve") { | ||
switch { | ||
case strings.Contains(exponent, "second"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = time.Duration(num) * time.Second | ||
} | ||
case strings.Contains(exponent, "min"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = time.Duration(num) * time.Minute | ||
} | ||
case strings.Contains(exponent, "uur"), strings.Contains(exponent, "uren"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = time.Duration(num) * time.Hour | ||
} | ||
case strings.Contains(exponent, "dag"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = time.Duration(num) * 24 * time.Hour | ||
} | ||
case strings.Contains(exponent, "week"), strings.Contains(exponent, "weken"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = time.Duration(num) * 7 * 24 * time.Hour | ||
} | ||
case strings.Contains(exponent, "maand"): | ||
if c.Month == nil || overwrite { | ||
c.Month = pointer.ToInt((int(ref.Month()) + num) % 12) | ||
} | ||
case strings.Contains(exponent, "jaar"): | ||
if c.Year == nil || overwrite { | ||
c.Year = pointer.ToInt(ref.Year() + num) | ||
} | ||
} | ||
} else { | ||
switch { | ||
case strings.Contains(exponent, "uur"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = 30 * time.Minute | ||
} | ||
case strings.Contains(exponent, "dag"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = 12 * time.Hour | ||
} | ||
case strings.Contains(exponent, "week"): | ||
if c.Duration == 0 || overwrite { | ||
c.Duration = 7 * 12 * time.Hour | ||
} | ||
case strings.Contains(exponent, "maand"): | ||
if c.Duration == 0 || overwrite { | ||
// 2 weeks | ||
c.Duration = 14 * 24 * time.Hour | ||
} | ||
case strings.Contains(exponent, "jaar"): | ||
if c.Month == nil || overwrite { | ||
c.Month = pointer.ToInt((int(ref.Month()) + 6) % 12) | ||
} | ||
} | ||
} | ||
|
||
return true, nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package nl_test | ||
|
||
import ( | ||
"github.com/olebedev/when/rules/nl" | ||
"testing" | ||
"time" | ||
|
||
"github.com/olebedev/when" | ||
"github.com/olebedev/when/rules" | ||
) | ||
|
||
func TestDeadline(t *testing.T) { | ||
fixt := []Fixture{ | ||
{"binnen een half uur", 0, "binnen een half uur", time.Hour / 2}, | ||
{"binnen 1 uur", 0, "binnen 1 uur", time.Hour}, | ||
{"in 5 minuten", 0, "in 5 minuten", time.Minute * 5}, | ||
{"Binnen 5 minuten ga ik naar huis", 0, "Binnen 5 minuten", time.Minute * 5}, | ||
{"we moeten binnen 10 dagen iets doen", 10, "binnen 10 dagen", 10 * 24 * time.Hour}, | ||
{"we moeten binnen vijf dagen iets doen", 10, "binnen vijf dagen", 5 * 24 * time.Hour}, | ||
{"we moeten over 5 dagen iets doen", 10, "over 5 dagen", 5 * 24 * time.Hour}, | ||
{"In 5 seconde moet een auto verplaatsen", 0, "In 5 seconde", 5 * time.Second}, | ||
{"binnen twee weken", 0, "binnen twee weken", 14 * 24 * time.Hour}, | ||
{"binnen een maand", 0, "binnen een maand", 31 * 24 * time.Hour}, | ||
{"na een maand", 0, "na een maand", 31 * 24 * time.Hour}, | ||
{"binnen een paar maanden", 0, "binnen een paar maanden", 91 * 24 * time.Hour}, | ||
{"binnen een jaar", 0, "binnen een jaar", 366 * 24 * time.Hour}, | ||
{"in een week", 0, "in een week", 7 * 24 * time.Hour}, | ||
} | ||
|
||
w := when.New(nil) | ||
w.Add(nl.Deadline(rules.Skip)) | ||
|
||
ApplyFixtures(t, "nl.Deadline", w, fixt) | ||
} |
Oops, something went wrong.