Commit 69132906 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch...

Merge branch '208262-follow-up-from-add-unscanned-projects-vuex-module-move-helper-into-shared-utils-folder' into 'master'

Add "isValidDate" function to date-utils

Closes #208262

See merge request gitlab-org/gitlab!26461
parents 121e0b85 f6a685a2
...@@ -566,6 +566,14 @@ export const getDateInPast = (date, daysInPast) => ...@@ -566,6 +566,14 @@ export const getDateInPast = (date, daysInPast) =>
export const getDateInFuture = (date, daysInFuture) => export const getDateInFuture = (date, daysInFuture) =>
new Date(newDate(date).setDate(date.getDate() + 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 * 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 * 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 */ /* eslint-disable import/prefer-default-export */
import { getDayDifference } from '~/lib/utils/datetime_utility'; import { getDayDifference, isValidDate } 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());
/** /**
* Checks if a given number of days is within a date range * Checks if a given number of days is within a date range
......
...@@ -474,6 +474,23 @@ describe('getDateInFuture', () => { ...@@ -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', () => { describe('getDatesInRange', () => {
it('returns an empty array if 1st or 2nd argument is not a Date object', () => { it('returns an empty array if 1st or 2nd argument is not a Date object', () => {
const d1 = new Date('2019-01-01'); 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