Skip to content

Commit 4a4ed57

Browse files
authored
refactor: use isLeapYear (#1638)
1 parent d8cfdcd commit 4a4ed57

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

Diff for: Conversions/DateDayDifference.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
Algorithm & Explanation : https://ncalculators.com/time-date/date-difference-calculator.htm
77
*/
88

9-
// Internal method for make calculations easier
10-
const isLeap = (year) => {
11-
if (year % 400 === 0) return true
12-
else if (year % 100 === 0) return false
13-
else if (year % 4 === 0) return true
14-
else return false
15-
}
9+
import { isLeapYear } from '../Maths/LeapYear'
10+
1611
const DateToDay = (dd, mm, yyyy) => {
1712
return (
1813
365 * (yyyy - 1) +
@@ -21,7 +16,7 @@ const DateToDay = (dd, mm, yyyy) => {
2116
Math.floor((yyyy - 1) / 400) +
2217
dd +
2318
Math.floor((367 * mm - 362) / 12) +
24-
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
19+
(mm <= 2 ? 0 : isLeapYear(yyyy) ? -1 : -2)
2520
)
2621
}
2722

Diff for: Timing-Functions/GetMonthDays.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
e.g.: mahfoudh.arous.com ->false
77
*/
88

9+
import { isLeapYear } from '../Maths/LeapYear'
10+
911
const getMonthDays = (monthNumber, year) => {
1012
const the31DaysMonths = [1, 3, 5, 7, 8, 10, 12]
1113
const the30DaysMonths = [4, 6, 9, 11]
@@ -26,11 +28,8 @@ const getMonthDays = (monthNumber, year) => {
2628
return 30
2729
}
2830

29-
// Check for Leap year
30-
if (year % 4 === 0) {
31-
if (year % 100 !== 0 || (year % 100 === 0 && year % 400 === 0)) {
32-
return 29
33-
}
31+
if (isLeapYear(year)) {
32+
return 29
3433
}
3534

3635
return 28

Diff for: Timing-Functions/test/GetMonthDays.test.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import { getMonthDays } from '../GetMonthDays'
22

33
describe('Get the Days of a Month', () => {
4-
it('expects to return 28', () => {
5-
expect(getMonthDays(2, 2018)).toEqual(28)
6-
})
7-
8-
it('expects to return 30', () => {
9-
expect(getMonthDays(6, 254)).toEqual(30)
10-
})
11-
12-
it('expects to return 29', () => {
13-
expect(getMonthDays(2, 2024)).toEqual(29)
4+
it.each([
5+
[1, 2024, 31],
6+
[2, 2024, 29],
7+
[3, 2024, 31],
8+
[4, 2024, 30],
9+
[5, 2024, 31],
10+
[6, 2024, 30],
11+
[7, 2024, 31],
12+
[8, 2024, 31],
13+
[9, 2024, 30],
14+
[10, 2024, 31],
15+
[11, 2024, 30],
16+
[12, 2024, 31],
17+
[1, 2023, 31],
18+
[2, 2023, 28],
19+
[3, 2023, 31],
20+
[4, 2023, 30],
21+
[5, 2023, 31],
22+
[6, 2023, 30],
23+
[7, 2023, 31],
24+
[8, 2023, 31],
25+
[9, 2023, 30],
26+
[10, 2023, 31],
27+
[11, 2023, 30],
28+
[12, 2023, 31]
29+
])('Month %i in year %i has %i days', (month, year, expected) => {
30+
expect(getMonthDays(month, year)).toBe(expected)
1431
})
1532

1633
it('expects to throw a type error', () => {

0 commit comments

Comments
 (0)