consolodating date logic

This commit is contained in:
2024-08-24 15:46:21 -06:00
parent 446889206f
commit 368a438daa
6 changed files with 34 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
import { extractLabelValue } from "../assignmnet/utils/markdownUtils";
import { IModuleItem } from "../IModuleItem";
import { verifyDateOrThrow } from "../timeUtils";
export interface LocalCoursePage extends IModuleItem {
name: string;
@@ -9,9 +10,7 @@ export interface LocalCoursePage extends IModuleItem {
export const localPageMarkdownUtils = {
toMarkdown: (page: LocalCoursePage) => {
const printableDueDate = new Date(page.dueAt)
.toISOString()
.replace("\u202F", " ");
const printableDueDate = verifyDateOrThrow(page.dueAt, "page DueDateForOrdering")
const settingsMarkdown = `Name: ${page.name}\nDueDateForOrdering: ${printableDueDate}\n---\n`;
return settingsMarkdown + page.text;
},
@@ -20,17 +19,13 @@ export const localPageMarkdownUtils = {
const rawSettings = pageMarkdown.split("---")[0];
const name = extractLabelValue(rawSettings, "Name");
const rawDate = extractLabelValue(rawSettings, "DueDateForOrdering");
const parsedDate = new Date(rawDate);
if (isNaN(parsedDate.getTime())) {
throw new Error(`could not parse due date: ${rawDate}`);
}
const dueAt = verifyDateOrThrow(rawDate, "page DueDateForOrdering");
const text = pageMarkdown.split("---\n")[1];
const page: LocalCoursePage = {
name,
dueAt: parsedDate.toISOString(),
dueAt,
text,
};
return page;

View File

@@ -3,7 +3,7 @@
export const getDateFromString = (value: string) => {
// may need to check for other formats
const validDateRegex =
/\d{2}\/\d{2}\/\d{4} [0-2][0-9]|[0-5][0-9]|[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/;
/\d{2}\/\d{2}\/\d{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/;
if (!validDateRegex.test(value)) {
return undefined;
}