From efeab6ef26098e062db26aae9f200b70b88faa60 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Mon, 18 Mar 2024 16:16:31 +0000 Subject: [PATCH 1/2] refactor: add and use `parseDate` --- Conversions/DateDayDifference.js | 28 ++++------------- Conversions/DateToDay.js | 15 ++++------ Timing-Functions/ParseDate.js | 27 +++++++++++++++++ Timing-Functions/test/ParseDate.test.js | 40 +++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 Timing-Functions/ParseDate.js create mode 100644 Timing-Functions/test/ParseDate.test.js diff --git a/Conversions/DateDayDifference.js b/Conversions/DateDayDifference.js index fef242568d..726a634253 100644 --- a/Conversions/DateDayDifference.js +++ b/Conversions/DateDayDifference.js @@ -7,6 +7,7 @@ */ import { isLeapYear } from '../Maths/LeapYear' +import { parseDate } from '../Timing-Functions/ParseDate' const DateToDay = (dd, mm, yyyy) => { return ( @@ -20,32 +21,13 @@ const DateToDay = (dd, mm, yyyy) => { ) } -const CheckDayAndMonth = (inDay, inMonth) => { - if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) { - throw new TypeError('Date is not valid.') - } -} - const DateDayDifference = (date1, date2) => { - // firstly, check that both input are string or not. - if (typeof date1 !== 'string' || typeof date2 !== 'string') { - throw new TypeError('Argument is not a string.') - } - // extract the first date - const [firstDateDay, firstDateMonth, firstDateYear] = date1 - .split('/') - .map((ele) => Number(ele)) - // extract the second date - const [secondDateDay, secondDateMonth, secondDateYear] = date2 - .split('/') - .map((ele) => Number(ele)) - // check the both data are valid or not. - CheckDayAndMonth(firstDateDay, firstDateMonth) - CheckDayAndMonth(secondDateDay, secondDateMonth) + const firstDate = parseDate(date1) + const secondDate = parseDate(date2) return Math.abs( - DateToDay(secondDateDay, secondDateMonth, secondDateYear) - - DateToDay(firstDateDay, firstDateMonth, firstDateYear) + DateToDay(secondDate.day, secondDate.month, secondDate.year) - + DateToDay(firstDate.day, firstDate.month, firstDate.year) ) } diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index f360c9c737..96ec01e82d 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -12,6 +12,8 @@ Algorithm & Explanation : https://en.wikipedia.org/wiki/Zeller%27s_congruence */ +import { parseDate } from '../Timing-Functions/ParseDate' + // Array holding name of the day: Saturday - Sunday - Friday => 0 - 1 - 6 const daysNameArr = [ 'Saturday', @@ -25,15 +27,10 @@ const daysNameArr = [ const DateToDay = (date) => { // firstly, check that input is a string or not. - if (typeof date !== 'string') { - throw new TypeError('Argument is not a string.') - } - // extract the date - let [day, month, year] = date.split('/').map((x) => Number(x)) - // check the data are valid or not. - if (day < 1 || day > 31 || month > 12 || month < 1) { - throw new TypeError('Date is not valid.') - } + const dateStruct = parseDate(date) + let year = dateStruct.year + let month = dateStruct.month + let day = dateStruct.day // In case of Jan and Feb: // Year: we consider it as previous year diff --git a/Timing-Functions/ParseDate.js b/Timing-Functions/ParseDate.js new file mode 100644 index 0000000000..7821264de5 --- /dev/null +++ b/Timing-Functions/ParseDate.js @@ -0,0 +1,27 @@ +import { getMonthDays } from './GetMonthDays' + +function checkDate(date) { + if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) { + throw new Error('Indalid day value.') + } +} + +function parseDate(dateString) { + const regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ + + const match = dateString.match(regex) + + if (!match) { + throw new Error("Invalid date format. Please use 'dd/mm/yyyy'.") + } + + const res = { + day: parseInt(match[1], 10), + month: parseInt(match[2], 10), + year: parseInt(match[3], 10) + } + checkDate(res) + return res +} + +export { parseDate } diff --git a/Timing-Functions/test/ParseDate.test.js b/Timing-Functions/test/ParseDate.test.js new file mode 100644 index 0000000000..3b807bb8a5 --- /dev/null +++ b/Timing-Functions/test/ParseDate.test.js @@ -0,0 +1,40 @@ +import { parseDate } from '../ParseDate' + +describe('parseDate', () => { + it.each([ + ['18/03/2024', { year: 2024, month: 3, day: 18 }], + ['29/02/2024', { year: 2024, month: 2, day: 29 }], + ['28/02/2023', { year: 2023, month: 2, day: 28 }], + ['01/12/2024', { year: 2024, month: 12, day: 1 }], + ['1/12/2024', { year: 2024, month: 12, day: 1 }], + ['10/1/2024', { year: 2024, month: 1, day: 10 }] + ])('Returns correct output for %s', (dateString, expected) => { + expect(parseDate(dateString)).toStrictEqual(expected) + }) + + it.each([ + '18-03-2024', + '18.03.2024', + '03/2024', + '01/02/03/2024', + '123/03/2024' + ])('Throws for %s', (wrongDateString) => { + expect(() => { + parseDate(wrongDateString) + }).toThrow() + }) + + it.each([ + '40/03/2024', + '30/02/2024', + '29/02/2023', + '31/04/2023', + '00/01/2024', + '01/00/2024', + '01/13/2024' + ])('Throws for %s', (wrongDateString) => { + expect(() => { + parseDate(wrongDateString) + }).toThrow() + }) +}) From 8c7780b49ca4fba83040d4fdf389e41e5d4fbefe Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 26 Mar 2024 22:29:46 +0000 Subject: [PATCH 2/2] style: use proper spelling Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com> --- Timing-Functions/ParseDate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Timing-Functions/ParseDate.js b/Timing-Functions/ParseDate.js index 7821264de5..67f4e4cd0e 100644 --- a/Timing-Functions/ParseDate.js +++ b/Timing-Functions/ParseDate.js @@ -2,7 +2,7 @@ import { getMonthDays } from './GetMonthDays' function checkDate(date) { if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) { - throw new Error('Indalid day value.') + throw new Error('Invalid day value.') } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy