@@ -5,6 +5,32 @@ import { ISupernovaTask } from "@supernova/types";
5
5
export const START_AT_SLATE_TYPE = "startAt" ;
6
6
export const EXP_DUR_SLATE_TYPE = "expectedDuration" ;
7
7
8
+ /**
9
+ * gets the regex for expected duration
10
+ * @returns the regex for expected duration
11
+ */
12
+ export function getExpectedDurationRegex ( ) : RegExp {
13
+ return new RegExp (
14
+ / \b f o r \s * ( \d + ) \s * (?: ( m i n s ? | m | m i n u t e s ? ) | ( h o u r s ? | h r s ? | h ) ) \s * \b \s * / gi
15
+ ) ;
16
+ }
17
+
18
+ /**
19
+ * gets the regex for start at
20
+ * @returns the regex for start at
21
+ */
22
+ export function getStartAtRegex ( ) : RegExp {
23
+ return new RegExp (
24
+ / \b (?: s t a r t a t | a t | f r o m ) \s + ( \d { 1 , 2 } (?: : \d { 2 } ) ? \s * (?: [ A P a p ] ? [ M m ] ? ) ) ? \b / gi
25
+ ) ;
26
+ }
27
+
28
+ export function getDateRegex ( ) : RegExp {
29
+ return new RegExp (
30
+ / \b (?: t m r | t o m | t o d a y | i n ( \d + ) d a y s | n e x t w e e k | i n ( \d + ) w e e k s | t o m o r r o w ) \b / gi
31
+ ) ;
32
+ }
33
+
8
34
/**
9
35
* Extracts the expected duration from a text string.
10
36
* @param text The text to extract the expected duration from.
@@ -19,9 +45,7 @@ export function extractExpectedDuration(
19
45
text : string
20
46
) : { value : number ; unit : "m" | "h" ; match : RegExpExecArray } | null {
21
47
// TODO: need to fix this local scope problem with the regexs somehow
22
- const expectedDurationRegex = new RegExp (
23
- / \b f o r \s * ( \d + ) \s * (?: ( m i n s ? | m | m i n u t e s ? ) | ( h o u r s ? | h r s ? | h ) ) \s * \b \s * / gi
24
- ) ;
48
+ const expectedDurationRegex = getExpectedDurationRegex ( ) ;
25
49
const match = expectedDurationRegex . exec ( text ) ;
26
50
if ( match === null ) {
27
51
return null ;
@@ -47,9 +71,7 @@ export function extractStartAt(
47
71
text : string
48
72
) : { value : Date ; match : RegExpExecArray } | null {
49
73
// TODO: need to fix this local scope problem with the regexs somehow
50
- const startAtRegex = new RegExp (
51
- / \b (?: s t a r t a t | a t | f r o m ) \s + ( \d { 1 , 2 } (?: : \d { 2 } ) ? \s * (?: [ A P a p ] ? [ M m ] ? ) ) ? \b / gi
52
- ) ;
74
+ const startAtRegex = getStartAtRegex ( ) ;
53
75
const match = startAtRegex . exec ( text ) ;
54
76
if ( match === null || match [ 1 ] === undefined ) {
55
77
return null ;
@@ -83,6 +105,36 @@ export function extractStartAt(
83
105
return { value : date , match } ;
84
106
}
85
107
108
+ export function extractDate (
109
+ text : string
110
+ ) : { value : Date ; match : RegExpExecArray } | null {
111
+ const dateRegex = getDateRegex ( ) ;
112
+ const match = dateRegex . exec ( text ) ;
113
+ if ( match === null ) {
114
+ return null ;
115
+ }
116
+ console . log ( match ) ;
117
+ const date = new Date ( ) ;
118
+ switch ( match [ 0 ] ) {
119
+ case "tomorrow" :
120
+ case "tmr" :
121
+ case "tom" :
122
+ date . setDate ( date . getDate ( ) + 1 ) ;
123
+ break ;
124
+ case "today" :
125
+ date . setDate ( date . getDate ( ) ) ;
126
+ break ;
127
+ case "next week" :
128
+ date . setDate ( date . getDate ( ) + 7 ) ;
129
+ break ;
130
+ default :
131
+ const days = parseInt ( match [ 1 ] ) ;
132
+ date . setDate ( date . getDate ( ) + days ) ;
133
+ break ;
134
+ }
135
+ return { value : date , match } ;
136
+ }
137
+
86
138
/**
87
139
* Creates Slate ranges from a regex.
88
140
* @param regex the regex to create ranges from
0 commit comments