-
-
Notifications
You must be signed in to change notification settings - Fork 416
feat(maths): add IsLeapYear
#40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @function IsLeapYear | ||
* @description Checks if a year is a leap year (Gregorian calendar). | ||
* A year is a leap year if it is divisible by 4 but not by 400 or if it is divisible by 400. | ||
* @param {number} year - A year, natural number > 0. | ||
* @return {boolean} - True if given year is a leap year. | ||
* @see https://en.wikipedia.org/wiki/Leap_year#Gregorian_calendar | ||
* @example IsLeapYear(2000) = true | ||
* @example IsLeapYear(2001) = false | ||
*/ | ||
|
||
export const IsLeapYear = (year: number): boolean => { | ||
if (year <= 0 || !Number.isInteger(year)) { | ||
throw new Error("year must be a natural number > 0"); | ||
} | ||
|
||
if (year % 4 > 0) { | ||
return false; | ||
} | ||
|
||
if (year % 100 > 0) { | ||
return true; | ||
} | ||
|
||
if (year % 400 > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { IsLeapYear } from "../IsLeapYear"; | ||
|
||
describe("IsLeapYear", () => { | ||
test.each([4, 8, 12, 2004])( | ||
"a year is a leap year it is divisible by 4 but not by 400 like %i", | ||
(year) => { | ||
expect(year % 4 === 0).toBe(true); | ||
expect(year % 400 === 0).toBe(false); | ||
expect(IsLeapYear(year)).toBe(true); | ||
}, | ||
); | ||
|
||
test.each([400, 800, 1200, 1600, 2000, 2400, 40000])( | ||
"a year is a leap year it is divisible by 400 like %i", | ||
(year) => { | ||
expect(year % 400 === 0).toBe(true); | ||
expect(IsLeapYear(year)).toBe(true); | ||
}, | ||
); | ||
|
||
test.each([1, 313, 1997, 2001, 2021, 13337])( | ||
"a year is not a leap year if it is not divisible by 4 like %i", | ||
(year) => { | ||
expect(year % 4 === 0).toBe(false); | ||
expect(IsLeapYear(year)).toBe(false); | ||
}, | ||
); | ||
|
||
test.each([100, 200, 300, 700, 2100])( | ||
"a year is not a leap year if it is divisible by 100 but not by 400 like %i", | ||
(year) => { | ||
expect(year % 100 === 0).toBe(true); | ||
expect(year % 400 === 0).toBe(false); | ||
expect(IsLeapYear(year)).toBe(false); | ||
}, | ||
); | ||
|
||
test.each([1, 2022, 3000000])( | ||
"a year is supported if it is a natural number > 0 like %i", | ||
(year) => { | ||
expect(year > 0).toBe(true); | ||
expect(Number.isInteger(year)).toBe(true); | ||
expect(() => IsLeapYear(year)).not.toThrow(); | ||
}, | ||
); | ||
|
||
test.each([-1, -10, -Infinity])( | ||
"a year is not supported if it is negative like %i", | ||
(year) => { | ||
expect(year < 0).toBe(true); | ||
expect(() => IsLeapYear(year)).toThrow("year must be a natural number > 0"); | ||
}, | ||
); | ||
|
||
test.each([0.1, 1.2, 4.2])( | ||
"a year is not supported if it is not an integer %d", | ||
(year) => { | ||
expect(Number.isInteger(year)).toBe(false); | ||
expect(() => IsLeapYear(year)).toThrow("year must be a natural number > 0"); | ||
}, | ||
); | ||
|
||
test("a year is not supported if it is 0", () => { | ||
expect(() => IsLeapYear(0)).toThrow("year must be a natural number > 0"); | ||
}) | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.