From ed42f9d5083fad8bd53b792dbc5f879ee804efd8 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Thu, 9 Feb 2023 17:53:58 +0000 Subject: [PATCH] refactor: expirationInput checks if predefined date --- src/lib/helpers/date.ts | 13 ++++++ .../overview/keys/expirationInput.svelte | 45 ++++++++++++------- tests/unit/helpers/date.test.ts | 33 +++++++++++++- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/lib/helpers/date.ts b/src/lib/helpers/date.ts index 391abb3e7..82031c727 100644 --- a/src/lib/helpers/date.ts +++ b/src/lib/helpers/date.ts @@ -22,3 +22,16 @@ export const toLocaleDateTime = (datetime: string | number) => { return date.toLocaleDateString('en', options); }; + +export const isSameDay = (date1: Date, date2: Date) => { + return ( + date1.getFullYear() === date2.getFullYear() && + date1.getMonth() === date2.getMonth() && + date1.getDate() === date2.getDate() + ); +}; + +// Function that checks if a string is a valid date +export const isValidDate = (date: string) => { + return !isNaN(new Date(date).getTime()); +}; diff --git a/src/routes/console/project-[project]/overview/keys/expirationInput.svelte b/src/routes/console/project-[project]/overview/keys/expirationInput.svelte index 55fca9e05..532f53fd7 100644 --- a/src/routes/console/project-[project]/overview/keys/expirationInput.svelte +++ b/src/routes/console/project-[project]/overview/keys/expirationInput.svelte @@ -1,6 +1,7 @@ - - + ]; + + export let value: string | null = null; + + function initExpirationSelect() { + if (value === null || !isValidDate(value)) return null; + + let result = 'custom'; + for (const option of options) { + if (!isValidDate(option.value)) continue; + + if (isSameDay(new Date(option.value), new Date(value))) { + result = option.value; + break; + } + } + + return result; + } + let expirationSelect = initExpirationSelect(); + let expirationCustom: string | null = value ?? null; + + $: { + value = expirationSelect === 'custom' ? expirationCustom : expirationSelect; + } + + + {#if expirationSelect === 'custom'} {/if} diff --git a/tests/unit/helpers/date.test.ts b/tests/unit/helpers/date.test.ts index 4b67f4f3d..99ef7e1f6 100644 --- a/tests/unit/helpers/date.test.ts +++ b/tests/unit/helpers/date.test.ts @@ -1,5 +1,5 @@ import '@testing-library/jest-dom'; -import { toLocaleDate, toLocaleDateTime } from '$lib/helpers/date'; +import { toLocaleDate, toLocaleDateTime, isSameDay, isValidDate } from '$lib/helpers/date'; describe('local date', () => { [ @@ -22,3 +22,34 @@ describe('local date time', () => { }); }); }); + +describe('is same day', () => { + const entries: Array<[string, string, boolean]> = [ + ['2022-11-15 08:26:28', '2022-11-15 08:26:28', true], + ['2022-11-15 08:26:28', '2022-11-16 08:26:28', false], + ['2022-11-15 08:26:28', '2022-11-15 08:26:29', true], + ['2022-11-15 08:26:28', '2022-11-15 08:26:27', true] + ]; + + entries.forEach(([value1, value2, expected]) => { + it(`${value1} ${value2}`, () => { + expect(isSameDay(new Date(value1), new Date(value2))).toBe(expected); + }); + }); +}); + +describe('is valid date', () => { + const entries: Array<[string, boolean]> = [ + ['2022-11-15 08:26:28', true], + ['2022-11-15 08:26:28', true], + ['abc', false], + ['2022-11-15', true], + ['123.123.123', false] + ]; + + entries.forEach(([value, expected]) => { + it(value, () => { + expect(isValidDate(value)).toBe(expected); + }); + }); +});