From 00aca32ab5cf5f7a080f544207fcf897341afe76 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Tue, 5 Mar 2024 18:13:58 +0000 Subject: [PATCH] refactor: use `isLeapYear` --- Conversions/DateDayDifference.js | 11 ++----- Timing-Functions/GetMonthDays.js | 9 +++--- Timing-Functions/test/GetMonthDays.test.js | 37 ++++++++++++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Conversions/DateDayDifference.js b/Conversions/DateDayDifference.js index e03a5aa24b..fef242568d 100644 --- a/Conversions/DateDayDifference.js +++ b/Conversions/DateDayDifference.js @@ -6,13 +6,8 @@ Algorithm & Explanation : https://ncalculators.com/time-date/date-difference-calculator.htm */ -// Internal method for make calculations easier -const isLeap = (year) => { - if (year % 400 === 0) return true - else if (year % 100 === 0) return false - else if (year % 4 === 0) return true - else return false -} +import { isLeapYear } from '../Maths/LeapYear' + const DateToDay = (dd, mm, yyyy) => { return ( 365 * (yyyy - 1) + @@ -21,7 +16,7 @@ const DateToDay = (dd, mm, yyyy) => { Math.floor((yyyy - 1) / 400) + dd + Math.floor((367 * mm - 362) / 12) + - (mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2) + (mm <= 2 ? 0 : isLeapYear(yyyy) ? -1 : -2) ) } diff --git a/Timing-Functions/GetMonthDays.js b/Timing-Functions/GetMonthDays.js index ce188c9603..43c2e7e6e6 100644 --- a/Timing-Functions/GetMonthDays.js +++ b/Timing-Functions/GetMonthDays.js @@ -6,6 +6,8 @@ e.g.: mahfoudh.arous.com ->false */ +import { isLeapYear } from '../Maths/LeapYear' + const getMonthDays = (monthNumber, year) => { const the31DaysMonths = [1, 3, 5, 7, 8, 10, 12] const the30DaysMonths = [4, 6, 9, 11] @@ -26,11 +28,8 @@ const getMonthDays = (monthNumber, year) => { return 30 } - // Check for Leap year - if (year % 4 === 0) { - if (year % 100 !== 0 || (year % 100 === 0 && year % 400 === 0)) { - return 29 - } + if (isLeapYear(year)) { + return 29 } return 28 diff --git a/Timing-Functions/test/GetMonthDays.test.js b/Timing-Functions/test/GetMonthDays.test.js index b7527c0ac6..1222cc4ae0 100644 --- a/Timing-Functions/test/GetMonthDays.test.js +++ b/Timing-Functions/test/GetMonthDays.test.js @@ -1,16 +1,33 @@ import { getMonthDays } from '../GetMonthDays' describe('Get the Days of a Month', () => { - it('expects to return 28', () => { - expect(getMonthDays(2, 2018)).toEqual(28) - }) - - it('expects to return 30', () => { - expect(getMonthDays(6, 254)).toEqual(30) - }) - - it('expects to return 29', () => { - expect(getMonthDays(2, 2024)).toEqual(29) + it.each([ + [1, 2024, 31], + [2, 2024, 29], + [3, 2024, 31], + [4, 2024, 30], + [5, 2024, 31], + [6, 2024, 30], + [7, 2024, 31], + [8, 2024, 31], + [9, 2024, 30], + [10, 2024, 31], + [11, 2024, 30], + [12, 2024, 31], + [1, 2023, 31], + [2, 2023, 28], + [3, 2023, 31], + [4, 2023, 30], + [5, 2023, 31], + [6, 2023, 30], + [7, 2023, 31], + [8, 2023, 31], + [9, 2023, 30], + [10, 2023, 31], + [11, 2023, 30], + [12, 2023, 31] + ])('Month %i in year %i has %i days', (month, year, expected) => { + expect(getMonthDays(month, year)).toBe(expected) }) it('expects to throw a type error', () => { 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