-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support _updatedAt from multiple formats
- Loading branch information
Showing
3 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DateTime } from 'luxon'; | ||
import { getUpdatedDate } from '../../src/imports/utils/dateUtils'; | ||
|
||
describe('getUpdatedDate', () => { | ||
const testISOString = '2024-02-20T10:30:00.000Z'; | ||
const testDate = new Date(testISOString); | ||
const expectedDateTime = DateTime.fromISO(testISOString); | ||
|
||
it('should handle undefined input', () => { | ||
const result = getUpdatedDate(undefined); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should parse Date object correctly', () => { | ||
const result = getUpdatedDate(testDate); | ||
expect(result?.toISO()).toBe(expectedDateTime.toISO()); | ||
}); | ||
|
||
it('should parse ISO string correctly', () => { | ||
const result = getUpdatedDate(testISOString); | ||
expect(result?.toISO()).toBe(expectedDateTime.toISO()); | ||
}); | ||
|
||
it('should parse MongoDB style object with Date correctly', () => { | ||
const result = getUpdatedDate({ $date: testDate }); | ||
expect(result?.toISO()).toBe(expectedDateTime.toISO()); | ||
}); | ||
|
||
it('should parse MongoDB style object with ISO string correctly', () => { | ||
const result = getUpdatedDate({ $date: testISOString }); | ||
expect(result?.toISO()).toBe(expectedDateTime.toISO()); | ||
}); | ||
|
||
it('should handle invalid date string', () => { | ||
const result = getUpdatedDate('invalid-date'); | ||
expect(result?.isValid).toBe(false); | ||
}); | ||
|
||
it('should handle null value', () => { | ||
// @ts-expect-error Testing null input even though types don't allow it | ||
const result = getUpdatedDate(null); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should handle empty object', () => { | ||
// @ts-expect-error Testing invalid object input | ||
const result = getUpdatedDate({}); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DateTime } from 'luxon'; | ||
import { logger } from './logger'; | ||
|
||
interface DateWithDate { | ||
$date: string | Date; | ||
} | ||
|
||
/** | ||
* Parses a date value that can come in different formats into a DateTime object | ||
* @param dateValue - The date value to parse which can be a string, Date object, object with $date property or undefined | ||
* @returns DateTime object representing the parsed date or undefined if input is invalid | ||
*/ | ||
export function getUpdatedDate(dateValue: string | Date | DateWithDate | undefined): DateTime | undefined { | ||
try { | ||
if (!dateValue) { | ||
return undefined; | ||
} | ||
|
||
if (dateValue instanceof Date) { | ||
return DateTime.fromJSDate(dateValue); | ||
} | ||
|
||
if (typeof dateValue === 'string') { | ||
return DateTime.fromISO(dateValue); | ||
} | ||
|
||
if ('$date' in dateValue) { | ||
const mongoDate = dateValue.$date; | ||
return mongoDate instanceof Date ? DateTime.fromJSDate(mongoDate) : DateTime.fromISO(mongoDate); | ||
} | ||
|
||
return undefined; | ||
} catch (error) { | ||
logger.error(error, 'Error parsing _updatedAt date'); | ||
return undefined; | ||
} | ||
} |