Skip to content

Commit

Permalink
added additional error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie1santana committed Jul 18, 2024
1 parent f9640fa commit 45f92ba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
25 changes: 23 additions & 2 deletions __test__/showTimeAgo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ test('showTimeAgo Utility should not be NaN', () => {
expect(showTimeAgo(today)).not.toBeNaN();
});

test('showTimeAgo Utility should return null for no argument', () => {
expect(showTimeAgo()).toBeNull();
test('showTimeAgo Utility should throw an error for no argument', () => {
expect(() => showTimeAgo()).toThrow('Invalid date parameter: dateParam cannot be empty. It must be a valid ISO date string or a Date object.');
});

test('showTimeAgo Utility type should be a function object', () => {
Expand Down Expand Up @@ -97,3 +97,24 @@ test('Test Date for multiple hours ago (less than a day)', () => {
const hoursAgo = new Date(today.getTime() - 5 * 3600000); // 5 hours ago
expect(showTimeAgo(hoursAgo)).toContain('hours ago');
});

// Additional test cases for error handling
test('Test invalid date string', () => {
expect(() => showTimeAgo('invalid-date')).toThrow('Invalid date parameter: dateParam is not a valid ISO date string.');
});

test('Test null date', () => {
expect(() => showTimeAgo(null)).toThrow('Invalid date parameter: dateParam cannot be empty. It must be a valid ISO date string or a Date object.');
});

test('Test undefined date', () => {
expect(() => showTimeAgo(undefined)).toThrow('Invalid date parameter: dateParam cannot be empty. It must be a valid ISO date string or a Date object.');
});

test('Test array as date parameter', () => {
expect(() => showTimeAgo([])).toThrow('Invalid date parameter: dateParam must be a valid ISO date string or a Date object.');
});

test('Test object as date parameter', () => {
expect(() => showTimeAgo({})).toThrow('Invalid date parameter: dateParam must be a valid ISO date string or a Date object.');
});
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ function showtimeago(dateParam) {
return `${month} ${getOrdinalNum(day)}, ${year} at ${hours}:${minutes} ${ampm}`;
}

// Validate the date parameter
function validateDateParam(dateParam) {
if (dateParam === undefined || dateParam === null) {
throw new Error("Invalid date parameter: dateParam cannot be empty. It must be a valid ISO date string or a Date object.");
}
if (typeof dateParam === 'string') {
const date = new Date(dateParam);
if (isNaN(date.getTime())) {
throw new Error("Invalid date parameter: dateParam is not a valid ISO date string.");
}
return date;
}
if (typeof dateParam === 'object' && dateParam instanceof Date) {
return dateParam;
}
throw new Error("Invalid date parameter: dateParam must be a valid ISO date string or a Date object.");
}

// Main function to calculate the "time ago" string
function timeAgo(dateParam) {
if (!dateParam) return null;

const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam);
const date = validateDateParam(dateParam);
const now = new Date();
const DAY_IN_MS = 86400000; // 24 * 60 * 60 * 1000
const YEAR_IN_MS = 365.25 * DAY_IN_MS; // Account for leap years
Expand Down Expand Up @@ -92,7 +108,6 @@ function showtimeago(dateParam) {
}
}

// console.log(timeAgo(dateParam)) test
return timeAgo(dateParam);

} catch (error) {
Expand All @@ -101,5 +116,4 @@ function showtimeago(dateParam) {
}
}

// showtimeago("2022-06-20T13:42:29-05:00")
module.exports = showtimeago;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "showtimeago",
"version": "4.0.1",
"version": "4.0.2",
"description": "ShowTimeAgo is a utility that provides a human-readable format of how long ago a date was with zero configuration.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 45f92ba

Please sign in to comment.