1
1
import { Dictionary , set } from "lodash" ;
2
- import { DateTime } from "luxon" ;
2
+ import { DateTime , Interval } from "luxon" ;
3
3
import { DeepPartial } from "utility-types" ;
4
4
5
5
import { Task } from "@/data/task" ;
6
6
7
7
export function parseEmojis ( text : string ) : DeepPartial < Task > {
8
8
const matches = [ ...text . matchAll ( EMOJI_REGEXP ) , / $ / . exec ( text ) as RegExpExecArray ] ;
9
9
10
- const taskParts : DeepPartial < Task > = {
11
- description : text . slice ( 0 , matches [ 0 ] . index ) . trim ( ) ,
12
- dates : { } ,
13
- } ;
10
+ const taskHeader = text . slice ( 0 , matches [ 0 ] . index ) . trim ( ) ;
11
+ const { description, times } = parseTaskHeader ( taskHeader ) ;
12
+ const taskParts : DeepPartial < Task > = { description, times, dates : { } } ;
14
13
15
14
for ( let i = 0 ; i <= matches . length - 2 ; ++ i ) {
16
15
const [ start , stop ] = matches . slice ( i , i + 2 ) ;
@@ -33,6 +32,22 @@ export function parseEmojis(text: string): DeepPartial<Task> {
33
32
return taskParts ;
34
33
}
35
34
35
+ function parseTaskHeader ( header : string ) {
36
+ const [ isoTime , description ] = header . split ( / \s + / , 2 ) ;
37
+
38
+ const interval = Interval . fromISO ( isoTime ) ;
39
+ if ( interval . isValid ) {
40
+ return { description, times : { start : interval . start , end : interval . end } } ;
41
+ }
42
+
43
+ const time = DateTime . fromISO ( isoTime ) ;
44
+ if ( time . isValid ) {
45
+ return { description, times : { start : time } } ;
46
+ }
47
+
48
+ return { description : header } ;
49
+ }
50
+
36
51
const TASK_PATH_BY_EMOJI : Readonly < Dictionary < string > > = {
37
52
"❌" : "dates.cancelled" ,
38
53
"➕" : "dates.created" ,
0 commit comments