mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
handling iso date strings
This commit is contained in:
@@ -33,9 +33,32 @@ export enum DayOfWeek {
|
||||
}
|
||||
export const localCourseYamlUtils = {
|
||||
parseSettingYaml: (settingsString: string): LocalCourseSettings => {
|
||||
return parse(settingsString);
|
||||
const settings = parse(settingsString);
|
||||
return lowercaseFirstLetter(settings)
|
||||
},
|
||||
settingsToYaml: (settings: LocalCourseSettings) => {
|
||||
return stringify(settings);
|
||||
},
|
||||
};
|
||||
|
||||
function lowercaseFirstLetter<T>(obj: T): T {
|
||||
if (obj === null || typeof obj !== 'object')
|
||||
return obj as T;
|
||||
|
||||
if (Array.isArray(obj))
|
||||
return obj.map(lowercaseFirstLetter) as unknown as T;
|
||||
|
||||
const result: Record<string, any> = {};
|
||||
Object.keys(obj).forEach((key) => {
|
||||
const value = (obj as Record<string, any>)[key];
|
||||
const newKey = key.charAt(0).toLowerCase() + key.slice(1);
|
||||
|
||||
if (value && typeof value === 'object') {
|
||||
result[newKey] = lowercaseFirstLetter(value);
|
||||
} else {
|
||||
result[newKey] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return result as T;
|
||||
}
|
||||
@@ -15,4 +15,9 @@ describe("Can properly handle expected date formats", () => {
|
||||
const dateObject = getDateFromString(dateString)
|
||||
expect(dateObject).not.toBeUndefined()
|
||||
})
|
||||
it("can use ISO format", () =>{
|
||||
const dateString = "2024-08-26T00:00:00.0000000"
|
||||
const dateObject = getDateFromString(dateString)
|
||||
expect(dateObject).not.toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,7 @@
|
||||
|
||||
const _getDateFromAMPM = (datePart: string, timePartWithMeridian: string): Date | undefined => {
|
||||
const _getDateFromAMPM = (
|
||||
datePart: string,
|
||||
timePartWithMeridian: string
|
||||
): Date | undefined => {
|
||||
const [day, month, year] = datePart.split("/").map(Number);
|
||||
const [timePart, meridian] = timePartWithMeridian.split(" ");
|
||||
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
||||
@@ -18,7 +20,10 @@ const _getDateFromAMPM = (datePart: string, timePartWithMeridian: string): Date
|
||||
return isNaN(date.getTime()) ? undefined : date;
|
||||
};
|
||||
|
||||
const _getDateFromMilitary = (datePart: string, timePart: string): Date | undefined => {
|
||||
const _getDateFromMilitary = (
|
||||
datePart: string,
|
||||
timePart: string
|
||||
): Date | undefined => {
|
||||
const [day, month, year] = datePart.split("/").map(Number);
|
||||
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
||||
|
||||
@@ -26,14 +31,20 @@ const _getDateFromMilitary = (datePart: string, timePart: string): Date | undefi
|
||||
return isNaN(date.getTime()) ? undefined : date;
|
||||
};
|
||||
|
||||
const _getDateFromISO = (value: string): Date | undefined => {
|
||||
const date = new Date(value);
|
||||
return isNaN(date.getTime()) ? undefined : date;
|
||||
};
|
||||
|
||||
export const getDateFromString = (value: string): Date | undefined => {
|
||||
// Regex for AM/PM format: "M/D/YYYY h:mm:ss AM/PM"
|
||||
const ampmDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}\s{1}[APap][Mm]$/;
|
||||
const ampmDateRegex =
|
||||
/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}\s{1}[APap][Mm]$/; //"M/D/YYYY h:mm:ss AM/PM"
|
||||
const militaryDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}$/; //"MM/DD/YYYY HH:mm:ss"
|
||||
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)$/; //"2024-08-26T00:00:00.0000000"
|
||||
|
||||
// Regex for military time format: "MM/DD/YYYY HH:mm:ss"
|
||||
const militaryDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}$/;
|
||||
|
||||
if (ampmDateRegex.test(value)) {
|
||||
if (isoDateRegex.test(value)) {
|
||||
return _getDateFromISO(value);
|
||||
} else if (ampmDateRegex.test(value)) {
|
||||
const [datePart, timePartWithMeridian] = value.split(/[\s\u202F]+/);
|
||||
return _getDateFromAMPM(datePart, timePartWithMeridian);
|
||||
} else if (militaryDateRegex.test(value)) {
|
||||
@@ -45,7 +56,11 @@ export const getDateFromString = (value: string): Date | undefined => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const getDateFromStringOrThrow = (value: string): Date => {
|
||||
const d = getDateFromString(value);
|
||||
if (!d) throw Error(`Invalid date format, ${value}`);
|
||||
return d;
|
||||
};
|
||||
|
||||
export const verifyDateStringOrUndefined = (
|
||||
value: string
|
||||
|
||||
Reference in New Issue
Block a user