Commit f6a685a2 authored by Dave Pisek's avatar Dave Pisek

Add "isValidDate" function to date-utils

This commit moves the "isValidDate" function from within the
security security_dashboard folder to the shared date and time utils.
parent b83b085b
......@@ -566,6 +566,14 @@ export const getDateInPast = (date, daysInPast) =>
export const getDateInFuture = (date, daysInFuture) =>
new Date(newDate(date).setDate(date.getDate() + daysInFuture));
/**
* Checks if a given date-instance was created with a valid date
*
* @param {Date} date
* @returns boolean
*/
export const isValidDate = date => date instanceof Date && !Number.isNaN(date.getTime());
/*
* Appending T00:00:00 makes JS assume local time and prevents it from shifting the date
* to match the user's time zone. We want to display the date in server time for now, to
......
/* eslint-disable import/prefer-default-export */
import { getDayDifference } from '~/lib/utils/datetime_utility';
/**
* Checks if a given date instance has been created with a valid date
*
* @param date {Date}
* @returns {boolean}
*/
const isValidDate = date => !Number.isNaN(date.getTime());
import { getDayDifference, isValidDate } from '~/lib/utils/datetime_utility';
/**
* Checks if a given number of days is within a date range
......
......@@ -474,6 +474,23 @@ describe('getDateInFuture', () => {
});
});
describe('isValidDate', () => {
it.each`
valueToCheck | isValid
${new Date()} | ${true}
${new Date('December 17, 1995 03:24:00')} | ${true}
${new Date('1995-12-17T03:24:00')} | ${true}
${new Date('foo')} | ${false}
${5} | ${false}
${''} | ${false}
${false} | ${false}
${undefined} | ${false}
${null} | ${false}
`('returns $expectedReturnValue when called with $dateToCheck', ({ valueToCheck, isValid }) => {
expect(datetimeUtility.isValidDate(valueToCheck)).toBe(isValid);
});
});
describe('getDatesInRange', () => {
it('returns an empty array if 1st or 2nd argument is not a Date object', () => {
const d1 = new Date('2019-01-01');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment