58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
export interface ParsedTask {
|
|
title: string;
|
|
priority?: 'low' | 'medium' | 'high';
|
|
dueDate?: Date;
|
|
labelName?: string;
|
|
}
|
|
|
|
export const parseTaskInput = (input: string): ParsedTask => {
|
|
let title = input;
|
|
let priority: ParsedTask['priority'] | undefined;
|
|
let dueDate: Date | undefined;
|
|
let labelName: string | undefined;
|
|
|
|
// Parse Priority (!high, !medium, !low)
|
|
const priorityMatch = title.match(/!(high|medium|low)/i);
|
|
if (priorityMatch) {
|
|
priority = priorityMatch[1].toLowerCase() as ParsedTask['priority'];
|
|
title = title.replace(priorityMatch[0], '').trim();
|
|
}
|
|
|
|
// Parse Label (#work, #personal)
|
|
const labelMatch = title.match(/#(\w+)/);
|
|
if (labelMatch) {
|
|
labelName = labelMatch[1];
|
|
title = title.replace(labelMatch[0], '').trim();
|
|
}
|
|
|
|
// Parse Date (tomorrow, today, next friday) - Simple heuristic
|
|
// Note: For production capability, use 'chrono-node'
|
|
const today = new Date();
|
|
const tomorrow = new Date(today);
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
const matchTomorrow = title.match(/\b(tomorrow|morgen)\b/i);
|
|
const matchToday = title.match(/\b(today|heute)\b/i);
|
|
const matchNextWeek = title.match(/\b(next week|nächste woche)\b/i);
|
|
|
|
if (matchTomorrow) {
|
|
dueDate = tomorrow;
|
|
title = title.replace(matchTomorrow[0], '').trim();
|
|
} else if (matchToday) {
|
|
dueDate = today;
|
|
title = title.replace(matchToday[0], '').trim();
|
|
} else if (matchNextWeek) {
|
|
const nextWeek = new Date(today);
|
|
nextWeek.setDate(today.getDate() + 7);
|
|
dueDate = nextWeek;
|
|
title = title.replace(matchNextWeek[0], '').trim();
|
|
}
|
|
|
|
return {
|
|
title,
|
|
priority,
|
|
dueDate,
|
|
labelName
|
|
};
|
|
};
|