@saugatedith/isleap
v1.0.0
Published
Zero-dependency utility to check leap years, with full edge-case handling, TypeScript types, and calendar-system awareness.
Maintainers
Readme
@saugatedith/isleap
Zero-dependency utility to check leap years — Gregorian, Julian, and proleptic.
Includes TypeScript types, edge-case handling, and a rich set of helper functions.
Installation
npm install @saugatedith/isleapQuick start
const { isLeap } = require("@saugatedith/isleap");
isLeap(2024); // true
isLeap(1900); // false ← century, not divisible by 400
isLeap(2000); // true ← divisible by 400
isLeap(2023); // falseOr use isLeapYear — both do the same thing:
const { isLeapYear } = require("@saugatedith/isleap");
isLeapYear(2024); // true
isLeapYear(1900); // false ← century, not divisible by 400
isLeapYear(2000); // true ← divisible by 400
isLeapYear(2023); // falseCalendar rules
Gregorian (default, ISO 8601)
| Condition | Leap? | |------------------------------------|-------| | Divisible by 400 | ✅ Yes | | Divisible by 100 (but not by 400) | ❌ No | | Divisible by 4 (but not by 100) | ✅ Yes | | Not divisible by 4 | ❌ No |
Julian
Simply: divisible by 4 → leap. No century exception.
Used before the Gregorian reform of 1582.
API
All functions that accept a year throw a TypeError if the value is not a finite integer.
isLeap(year) → boolean
Alias for isLeapYear(). Simple leap year check matching the package name.
isLeap(2024) // true
isLeap(1900) // false
isLeap(2000) // true
isLeap(0) // true (year 0 = 1 BC, proleptic Gregorian)
isLeap(-4) // true (5 BC)isLeapYear(year) → boolean
Gregorian leap year check (same as isLeap).
isLeapYear(2024) // true
isLeapYear(1900) // false
isLeapYear(2000) // true
isLeapYear(0) // true (year 0 = 1 BC, proleptic Gregorian)
isLeapYear(-4) // true (5 BC)isLeapYearJulian(year) → boolean
Julian calendar leap year check.
isLeapYearJulian(1900) // true ← unlike Gregorian
isLeapYearJulian(2023) // falsedaysInFebruary(year) → 28 | 29
daysInFebruary(2024) // 29
daysInFebruary(2023) // 28daysInYear(year) → 365 | 366
daysInYear(2024) // 366
daysInYear(2023) // 365nextLeapYear(year) → number
Returns the next leap year ≥ year.
nextLeapYear(2023) // 2024
nextLeapYear(2024) // 2024 (same year counts)
nextLeapYear(2025) // 2028
nextLeapYear(1900) // 1904 (century skip)prevLeapYear(year) → number
Returns the previous leap year ≤ year.
prevLeapYear(2023) // 2020
prevLeapYear(2024) // 2024
prevLeapYear(1900) // 1896leapYearsInRange(start, end) → number[]
Returns every leap year in [start, end], inclusive.
leapYearsInRange(2000, 2020)
// [2000, 2004, 2008, 2012, 2016, 2020]
leapYearsInRange(1897, 1905)
// [1904] ← 1900 excluded (century non-leap)Throws RangeError if start > end.
countLeapYears(start, end) → number
Counts leap years in [start, end] using an O(1) formula (no loop).
countLeapYears(1900, 2000) // 25
countLeapYears(2000, 2024) // 7Throws RangeError if start > end.
yearInfo(year) → YearInfo
Returns a structured info object.
yearInfo(2024)
// {
// year: 2024,
// isLeap: true,
// daysInYear: 366,
// daysInFebruary: 29,
// nextLeap: 2024,
// prevLeap: 2024,
// calendar: "Gregorian"
// }TypeScript
Full types are included out of the box — no @types/ package needed.
import { isLeap, isLeapYear, yearInfo, YearInfo } from "@saugatedith/isleap";
const leap: boolean = isLeapYear(2024);
const leap: boolean = isLeap(2024); // or use isLeapYear
const info: YearInfo = yearInfo(2024);Common gotchas
| Year | Gregorian | Julian | Why | |------|-----------|--------|-----| | 1900 | ❌ No | ✅ Yes | century non-leap in Gregorian | | 2000 | ✅ Yes | ✅ Yes | divisible by 400 in Gregorian | | 0 | ✅ Yes | ✅ Yes | year 0 = 1 BC | | -100 | ❌ No | ✅ Yes | century rule applies negatively |
Running tests
npm testLicense
MIT © Your Name
