Skip to content

Commit

Permalink
feat: add common nl expressions for day in last or next week
Browse files Browse the repository at this point in the history
  • Loading branch information
iamreinder committed Dec 1, 2023
1 parent e229050 commit 14f6714
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rules/nl/nl_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package nl_test

import (
"github.com/olebedev/when/rules/nl"
"testing"
"time"

"github.com/olebedev/when/rules/nl"

"github.com/olebedev/when"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -51,12 +52,15 @@ func TestAll(t *testing.T) {

// complex cases
fixt := []Fixture{
{"vorige week zondag om 10:00", 0, "vorige week zondag om 10:00", ((-3 * 24) + 10) * time.Hour},
{"vanavond om 23:10", 0, "vanavond om 23:10", (23 * time.Hour) + (10 * time.Minute)},
{"op vrijdagmiddag", 3, "vrijdagmiddag", ((2 * 24) + 15) * time.Hour},
{"komende dinsdag om 14:00", 0, "komende dinsdag om 14:00", ((6 * 24) + 14) * time.Hour},
{"komende dinsdag 2 uur 's middags", 0, "komende dinsdag 2 uur 's middags", ((6 * 24) + 14) * time.Hour},
{"komende woensdag om 14:25", 0, "komende woensdag om 14:25", (((7 * 24) + 14) * time.Hour) + (25 * time.Minute)},
{"om 11 uur afgelopen dinsdag", 3, "11 uur afgelopen dinsdag", -13 * time.Hour},
{"volgende week dinsdag om 18:15", 0, "volgende week dinsdag om 18:15", (((6 * 24) + 18) * time.Hour) + (15 * time.Minute)},
{"volgende week vrijdag", 0, "volgende week vrijdag", (9 * 24) * time.Hour},
}

ApplyFixtures(t, "nl.All...", w, fixt)
Expand Down
26 changes: 22 additions & 4 deletions rules/nl/weekday.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Weekday(s rules.Strategy) rules.Rule {
RegExp: regexp.MustCompile("(?i)" +
"(?:\\W|^)" +
"(?:op\\s*?)?" +
"(?:(deze|vorige|afgelopen|volgende|komende)\\s*)?" +
"(?:(deze|vorige|vorige week|afgelopen|volgende|volgende week|komende|komende week)\\s*)?" +
"(" + WEEKDAY_OFFSET_PATTERN[3:] + // skip '(?:'
"(?:\\s*(deze|vorige|afgelopen|volgende|komende)\\s*week)?" +
"(?:\\W|$)",
Expand All @@ -40,6 +40,18 @@ func Weekday(s rules.Strategy) rules.Rule {

// Switch:
switch {
case strings.Contains(norm, "vorige week"):
if dayInt == 6 {
dayInt = -1
}
diff := int(ref.Weekday()) - dayInt
if diff != 0 && dayInt <= 0 {
c.Duration = -time.Duration(diff) * 24 * time.Hour
} else if diff != 0 {
c.Duration = -time.Duration(7+diff) * 24 * time.Hour
} else {
c.Duration = -(7 * 24 * time.Hour)
}
case strings.Contains(norm, "afgelopen") || strings.Contains(norm, "vorige"):
diff := int(ref.Weekday()) - dayInt
if diff > 0 {
Expand All @@ -49,10 +61,16 @@ func Weekday(s rules.Strategy) rules.Rule {
} else {
c.Duration = -(7 * 24 * time.Hour)
}
case strings.Contains(norm, "volgende week"):
if dayInt == 0 {
dayInt = 7
}
diff := dayInt - int(ref.Weekday())
c.Duration = time.Duration(7+diff) * 24 * time.Hour
case strings.Contains(norm, "volgende"), strings.Contains(norm, "komende"):
diff := dayInt - int(ref.Weekday())
if diff > 0 {
c.Duration = time.Duration(diff*24) * time.Hour
c.Duration = time.Duration(diff) * 24 * time.Hour
} else if diff < 0 {
c.Duration = time.Duration(7+diff) * 24 * time.Hour
} else {
Expand All @@ -62,7 +80,7 @@ func Weekday(s rules.Strategy) rules.Rule {
if int(ref.Weekday()) < dayInt {
diff := dayInt - int(ref.Weekday())
if diff > 0 {
c.Duration = time.Duration(diff*24) * time.Hour
c.Duration = time.Duration(diff) * 24 * time.Hour
} else if diff < 0 {
c.Duration = time.Duration(7+diff) * 24 * time.Hour
} else {
Expand All @@ -71,7 +89,7 @@ func Weekday(s rules.Strategy) rules.Rule {
} else if int(ref.Weekday()) > dayInt {
diff := int(ref.Weekday()) - dayInt
if diff > 0 {
c.Duration = -time.Duration(diff*24) * time.Hour
c.Duration = -time.Duration(diff) * 24 * time.Hour
} else if diff < 0 {
c.Duration = -time.Duration(7+diff) * 24 * time.Hour
} else {
Expand Down
21 changes: 19 additions & 2 deletions rules/nl/weekday_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package nl_test

import (
"github.com/olebedev/when/rules/nl"
"testing"
"time"

"github.com/olebedev/when/rules/nl"

"github.com/olebedev/when"
"github.com/olebedev/when/rules"
)

func TestWeekday(t *testing.T) {
// current is Friday
// current is Wednesday
fixt := []Fixture{
// past week
{"vorige week maandag", 0, "vorige week maandag", -(9 * 24 * time.Hour)},
{"vorige week dinsdag", 0, "vorige week dinsdag", -(8 * 24 * time.Hour)},
{"vorige week woensdag", 0, "vorige week woensdag", -(7 * 24 * time.Hour)},
{"vorige week donderdag", 0, "vorige week donderdag", -(6 * 24 * time.Hour)},
{"vorige week vrijdag", 0, "vorige week vrijdag", -(5 * 24 * time.Hour)},
{"vorige week zaterdag", 0, "vorige week zaterdag", -(4 * 24 * time.Hour)},
{"vorige week zondag", 0, "vorige week zondag", -(3 * 24 * time.Hour)},
// past/last
{"doe het voor afgelopen maandag", 13, "afgelopen maandag", -(2 * 24 * time.Hour)},
{"afgelopen zaterdag", 0, "afgelopen zaterdag", -(4 * 24 * time.Hour)},
{"afgelopen vrijdag", 0, "afgelopen vrijdag", -(5 * 24 * time.Hour)},
{"afgelopen woensdag", 0, "afgelopen woensdag", -(7 * 24 * time.Hour)},
{"afgelopen dinsdag", 0, "afgelopen dinsdag", -(24 * time.Hour)},
// next week
{"volgende week maandag", 0, "volgende week maandag", 5 * 24 * time.Hour},
{"volgende week dinsdag", 0, "volgende week dinsdag", 6 * 24 * time.Hour},
{"volgende week woensdag", 0, "volgende week woensdag", 7 * 24 * time.Hour},
{"volgende week donderdag", 0, "volgende week donderdag", 8 * 24 * time.Hour},
{"volgende week vrijdag", 0, "volgende week vrijdag", 9 * 24 * time.Hour},
{"volgende week zaterdag", 0, "volgende week zaterdag", 10 * 24 * time.Hour},
{"volgende week zondag", 0, "volgende week zondag", 11 * 24 * time.Hour},
// next
{"komende dinsdag", 0, "komende dinsdag", 6 * 24 * time.Hour},
{"stuur me een bericht komende woensdag", 21, "komende woensdag", 7 * 24 * time.Hour},
Expand Down

0 comments on commit 14f6714

Please sign in to comment.