feat(rules): add the aggressive deadline option to nl and br
This commit is contained in:
1 parent
b80ddbf523
commit
c2384ff7ad
4 files changed
+66
-4
No files matched your search
+13
-2
@@ -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 "dentro de"/"em" prefix optional, so "3 dias" matches too.
|
||||||
|
Aggressive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func Deadline(s rules.Strategy, opts ...DeadlineOptions) rules.Rule {
|
||||||
overwrite := s == rules.Override
|
overwrite := s == rules.Override
|
||||||
|
|
||||||
|
prefix := "(dentro\\sde|em)"
|
||||||
|
if len(opts) > 0 && opts[0].Aggressive {
|
||||||
|
prefix += "?"
|
||||||
|
}
|
||||||
|
|
||||||
return &rules.F{
|
return &rules.F{
|
||||||
RegExp: regexp.MustCompile(
|
RegExp: regexp.MustCompile(
|
||||||
"(?i)(?:\\W|^)(dentro\\sde|em)\\s*" +
|
"(?i)(?:\\W|^)" + prefix + "\\s*" +
|
||||||
"(?:(" + INTEGER_WORDS_PATTERN + "|[0-9]+|(?:\\s*pouc[oa](?:s|)?|algu(?:mas|m|ns)?|mei[oa]?))\\s*" +
|
"(?:(" + INTEGER_WORDS_PATTERN + "|[0-9]+|(?:\\s*pouc[oa](?:s|)?|algu(?:mas|m|ns)?|mei[oa]?))\\s*" +
|
||||||
"(segundos?|min(?:uto)?s?|horas?|dias?|semanas?|mês|meses|anos?)\\s*)" +
|
"(segundos?|min(?:uto)?s?|horas?|dias?|semanas?|mês|meses|anos?)\\s*)" +
|
||||||
"(?:\\W|$)"),
|
"(?:\\W|$)"),
|
||||||
|
|||||||
@@ -32,3 +32,23 @@ func TestDeadline(t *testing.T) {
|
|||||||
|
|
||||||
ApplyFixtures(t, "br.Deadline", w, fixt)
|
ApplyFixtures(t, "br.Deadline", w, fixt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeadlineAggressive(t *testing.T) {
|
||||||
|
fixt := []Fixture{
|
||||||
|
{"3 dias", 0, "3 dias", 3 * 24 * time.Hour},
|
||||||
|
{"me lembre 4 dias depois", 10, "4 dias", 4 * 24 * time.Hour},
|
||||||
|
{"uma semana", 0, "uma semana", 7 * 24 * time.Hour},
|
||||||
|
{"dentro de meia hora", 0, "dentro de meia hora", time.Hour / 2},
|
||||||
|
{"me ligue em 5 minutos", 9, "em 5 minutos", 5 * time.Minute},
|
||||||
|
}
|
||||||
|
|
||||||
|
w := when.New(nil)
|
||||||
|
w.Add(br.Deadline(rules.Skip, br.DeadlineOptions{Aggressive: true}))
|
||||||
|
|
||||||
|
ApplyFixtures(t, "br.Deadline aggressive", w, fixt)
|
||||||
|
|
||||||
|
w = when.New(nil)
|
||||||
|
w.Add(br.Deadline(rules.Skip))
|
||||||
|
|
||||||
|
ApplyFixturesNil(t, "br.Deadline", w, []Fixture{{"3 dias", 0, "", 0}})
|
||||||
|
}
|
||||||
+13
-2
@@ -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 "binnen"/"in"/"over"/"na" prefix optional, so "3 dagen" matches too.
|
||||||
|
Aggressive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func Deadline(s rules.Strategy, opts ...DeadlineOptions) rules.Rule {
|
||||||
overwrite := s == rules.Override
|
overwrite := s == rules.Override
|
||||||
|
|
||||||
|
prefix := "(binnen|in|over|na)"
|
||||||
|
if len(opts) > 0 && opts[0].Aggressive {
|
||||||
|
prefix += "?"
|
||||||
|
}
|
||||||
|
|
||||||
return &rules.F{
|
return &rules.F{
|
||||||
RegExp: regexp.MustCompile(
|
RegExp: regexp.MustCompile(
|
||||||
"(?i)(?:\\W|^)(binnen|in|over|na)\\s*" +
|
"(?i)(?:\\W|^)" + prefix + "\\s*" +
|
||||||
"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|een(?:\\s*(paar|half|halve))?)\\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*" +
|
"(seconden?|minuut|minuten|uur|uren|dag|dagen|week|weken|maand|maanden|jaar|jaren)\\s*" +
|
||||||
"(?:\\W|$)"),
|
"(?:\\W|$)"),
|
||||||
|
|||||||
@@ -32,3 +32,23 @@ func TestDeadline(t *testing.T) {
|
|||||||
|
|
||||||
ApplyFixtures(t, "nl.Deadline", w, fixt)
|
ApplyFixtures(t, "nl.Deadline", w, fixt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeadlineAggressive(t *testing.T) {
|
||||||
|
fixt := []Fixture{
|
||||||
|
{"3 dagen", 0, "3 dagen", 3 * 24 * time.Hour},
|
||||||
|
{"herinner me 4 dagen later", 12, "4 dagen", 4 * 24 * time.Hour},
|
||||||
|
{"een week", 0, "een week", 7 * 24 * time.Hour},
|
||||||
|
{"binnen een half uur", 0, "binnen een half uur", time.Hour / 2},
|
||||||
|
{"bel me over 5 minuten terug", 7, "over 5 minuten", 5 * time.Minute},
|
||||||
|
}
|
||||||
|
|
||||||
|
w := when.New(nil)
|
||||||
|
w.Add(nl.Deadline(rules.Skip, nl.DeadlineOptions{Aggressive: true}))
|
||||||
|
|
||||||
|
ApplyFixtures(t, "nl.Deadline aggressive", w, fixt)
|
||||||
|
|
||||||
|
w = when.New(nil)
|
||||||
|
w.Add(nl.Deadline(rules.Skip))
|
||||||
|
|
||||||
|
ApplyFixturesNil(t, "nl.Deadline", w, []Fixture{{"3 dagen", 0, "", 0}})
|
||||||
|
}
|
||||||
Reference in new issue
Block a user