mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-07 21:37:04 +09:00
GO-4635 Improve date suggestion
This commit is contained in:
parent
28e7425b63
commit
425dcfe2c5
4 changed files with 48 additions and 9 deletions
|
@ -21,6 +21,20 @@ import (
|
|||
"github.com/anyproto/anytype-heart/util/pbtypes"
|
||||
)
|
||||
|
||||
var literals []string
|
||||
|
||||
func init() {
|
||||
literals = []string{"today", "now", "yesterday", "tomorrow"}
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
literals = append(literals, strings.ToLower(time.Weekday(i).String()))
|
||||
}
|
||||
|
||||
for i := 1; i < 12; i++ {
|
||||
literals = append(literals, strings.ToLower(time.Month(i).String()))
|
||||
}
|
||||
}
|
||||
|
||||
func EnrichRecordsWithDateSuggestions(
|
||||
ctx context.Context,
|
||||
spaceId, fullText string,
|
||||
|
@ -92,10 +106,19 @@ func suggestDateForSearch(now time.Time, raw string) time.Time {
|
|||
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
}
|
||||
|
||||
if len(raw) > 1 {
|
||||
for _, variant := range literals {
|
||||
if strings.Contains(variant, strings.ToLower(raw)) {
|
||||
raw = variant
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suggesters := []func() time.Time{
|
||||
func() time.Time {
|
||||
var exprType naturaldate.ExprType
|
||||
t, exprType, err := naturaldate.Parse(raw, now)
|
||||
t, exprType, err := naturaldate.Parse(raw, now, naturaldate.WithDirection(naturaldate.Future))
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,22 @@ func Test_suggestDateForSearch(t *testing.T) {
|
|||
raw: "today",
|
||||
want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc),
|
||||
},
|
||||
{
|
||||
raw: "toda",
|
||||
want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc),
|
||||
},
|
||||
{
|
||||
raw: "to",
|
||||
want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc),
|
||||
},
|
||||
{
|
||||
raw: "yeste",
|
||||
want: time.Date(2022, 5, 17, 0, 0, 0, 0, loc),
|
||||
},
|
||||
{
|
||||
raw: "NoVem",
|
||||
want: time.Date(2022, 11, 18, 14, 56, 33, 0, loc),
|
||||
},
|
||||
{
|
||||
raw: "10 days from now",
|
||||
want: time.Date(2022, 5, 28, 14, 56, 33, 0, loc),
|
||||
|
|
|
@ -10,8 +10,8 @@ import (
|
|||
const (
|
||||
shortDateIdLayout = "2006-01-02"
|
||||
dateIdLayout = "2006-01-02-15-04-05Z-0700"
|
||||
shortDateNameLayout = "Mon, Jan 02, 2006"
|
||||
dateNameLayout = "Mon, Jan 02, 2006 3:04 PM"
|
||||
shortDateNameLayout = "Mon, Jan 2, 2006"
|
||||
dateNameLayout = "Mon, Jan 2, 2006 3:04 PM"
|
||||
)
|
||||
|
||||
type DateObject interface {
|
||||
|
|
|
@ -17,17 +17,17 @@ func TestNewDateObject(t *testing.T) {
|
|||
{
|
||||
ts: time.Date(2024, time.November, 7, 12, 25, 59, 0, time.UTC),
|
||||
expectedId: "_date_2024-11-07-12-25-59Z_0000",
|
||||
expectedName: "Thu, Nov 07, 2024 12:25 PM",
|
||||
expectedName: "Thu, Nov 7, 2024 12:25 PM",
|
||||
},
|
||||
{
|
||||
ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.UTC),
|
||||
expectedId: "_date_1998-01-01-00-01-01Z_0000",
|
||||
expectedName: "Thu, Jan 01, 1998 12:01 AM",
|
||||
expectedName: "Thu, Jan 1, 1998 12:01 AM",
|
||||
},
|
||||
{
|
||||
ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.FixedZone("UTC", +4*60*60)),
|
||||
expectedId: "_date_1998-01-01-00-01-01Z_0400",
|
||||
expectedName: "Thu, Jan 01, 1998 12:01 AM",
|
||||
expectedName: "Thu, Jan 1, 1998 12:01 AM",
|
||||
},
|
||||
{
|
||||
ts: time.Date(2124, time.December, 25, 23, 34, 0, 0, time.UTC),
|
||||
|
@ -56,17 +56,17 @@ func TestNewDateObject(t *testing.T) {
|
|||
{
|
||||
ts: time.Date(2024, time.November, 7, 12, 25, 59, 0, time.UTC),
|
||||
expectedId: "_date_2024-11-07",
|
||||
expectedName: "Thu, Nov 07, 2024",
|
||||
expectedName: "Thu, Nov 7, 2024",
|
||||
},
|
||||
{
|
||||
ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.UTC),
|
||||
expectedId: "_date_1998-01-01",
|
||||
expectedName: "Thu, Jan 01, 1998",
|
||||
expectedName: "Thu, Jan 1, 1998",
|
||||
},
|
||||
{
|
||||
ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.FixedZone("UTC", +4*60*60)),
|
||||
expectedId: "_date_1998-01-01",
|
||||
expectedName: "Thu, Jan 01, 1998",
|
||||
expectedName: "Thu, Jan 1, 1998",
|
||||
},
|
||||
{
|
||||
ts: time.Date(2124, time.December, 25, 23, 34, 0, 0, time.UTC),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue