@ultirequiem/leap-year 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{types/mod.d.ts → lib/index.d.ts} +7 -7
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/parse-date-like.d.ts +11 -0
- package/lib/parse-date-like.d.ts.map +1 -0
- package/lib/parse-date-like.js +26 -0
- package/package.json +39 -26
- package/readme.md +54 -0
- package/README.md +0 -70
- package/esm/mod.js +0 -40
- package/esm/package.json +0 -3
- package/script/mod.js +0 -45
- package/script/package.json +0 -3
- /package/{LICENSE → license} +0 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```javascript
|
|
8
|
-
* import { isLeapYear } from "
|
|
8
|
+
* import { isLeapYear } from "@ultirequiem/leap-year";
|
|
9
9
|
*
|
|
10
10
|
* isLeapYear(2014); //=> false
|
|
11
11
|
* isLeapYear(2016); //=> true
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
* isLeapYear(new Date(2016, 1, 1)); //=> true
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export declare function isLeapYear(year?:
|
|
18
|
-
export declare function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
17
|
+
export declare function isLeapYear(year?: Date | string | number): boolean;
|
|
18
|
+
export declare function isLeapYear(year?: Date | string | number, returnValue?: boolean): {
|
|
19
|
+
year: number;
|
|
20
|
+
result: boolean;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AACnE,wBAAgB,UAAU,CACxB,IAAI,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC7B,WAAW,CAAC,EAAE,OAAO,GACpB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { parseDateLike } from "./parse-date-like.js";
|
|
2
|
+
export function isLeapYear(year = new Date(), returnValue = false) {
|
|
3
|
+
year = parseDateLike(year);
|
|
4
|
+
const result = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
5
|
+
return returnValue ? { result, year } : result;
|
|
6
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the (UTC) year out of anything date-like.
|
|
3
|
+
*
|
|
4
|
+
* @param value - A `Date`, a year as number or string, or a date string.
|
|
5
|
+
*
|
|
6
|
+
* @throws {@link TypeError}
|
|
7
|
+
*
|
|
8
|
+
* Thrown if the value cannot be turned into a year.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseDateLike(value: Date | string | number): number;
|
|
11
|
+
//# sourceMappingURL=parse-date-like.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-date-like.d.ts","sourceRoot":"","sources":["../src/parse-date-like.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAsBnE"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the (UTC) year out of anything date-like.
|
|
3
|
+
*
|
|
4
|
+
* @param value - A `Date`, a year as number or string, or a date string.
|
|
5
|
+
*
|
|
6
|
+
* @throws {@link TypeError}
|
|
7
|
+
*
|
|
8
|
+
* Thrown if the value cannot be turned into a year.
|
|
9
|
+
*/
|
|
10
|
+
export function parseDateLike(value) {
|
|
11
|
+
if (value instanceof Date) {
|
|
12
|
+
return value.getUTCFullYear();
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === "number") {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
const year = Number(value);
|
|
18
|
+
if (value.trim() !== "" && Number.isInteger(year)) {
|
|
19
|
+
return year;
|
|
20
|
+
}
|
|
21
|
+
const date = new Date(value);
|
|
22
|
+
if (Number.isNaN(date.getTime())) {
|
|
23
|
+
throw new TypeError(`Could not get a year out of "${value}".`);
|
|
24
|
+
}
|
|
25
|
+
return date.getUTCFullYear();
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,40 +1,53 @@
|
|
|
1
1
|
{
|
|
2
|
-
"module": "./esm/mod.js",
|
|
3
|
-
"main": "./script/mod.js",
|
|
4
|
-
"types": "./types/mod.d.ts",
|
|
5
2
|
"name": "@ultirequiem/leap-year",
|
|
3
|
+
"version": "3.0.0",
|
|
6
4
|
"description": "Check if a year is leap.",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
12
13
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
14
|
+
"files": [
|
|
15
|
+
"lib/**/*"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
18
19
|
},
|
|
19
20
|
"keywords": [
|
|
20
21
|
"leap",
|
|
21
22
|
"year",
|
|
22
23
|
"date"
|
|
23
24
|
],
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"homepage": "https://leap-year.js.org",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/UltiRequiem/leap-year.git"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"lint": "eslint 'src/**/*.ts' && prettier --check 'src/**/*.ts'",
|
|
34
|
+
"fmt": "prettier --write 'src/**/*.ts'",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"test:coverage": "vitest run --coverage"
|
|
33
37
|
},
|
|
34
|
-
"dependencies": {},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
"@types/node": "
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
"@types/node": "^26.6.5",
|
|
40
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
41
|
+
"eslint": "^10.13.0",
|
|
42
|
+
"eslint-plugin-tsdoc": "^0.5.4",
|
|
43
|
+
"eslint-plugin-unicorn": "^78.0.0",
|
|
44
|
+
"prettier": "^3.9.10",
|
|
45
|
+
"typescript": "^6.0.3",
|
|
46
|
+
"typescript-eslint": "^8.72.0",
|
|
47
|
+
"vitest": "^5.0.5"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@ultirequiem/leap-year": "^2.1.0"
|
|
51
|
+
},
|
|
52
|
+
"packageManager": "pnpm@12.6.0"
|
|
53
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# leap-year
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ultirequiem/leap-year)
|
|
4
|
+
[](https://www.npmjs.com/package/@ultirequiem/leap-year)
|
|
5
|
+
[](https://github.com/UltiRequiem/leap-year/actions/workflows/ci.yaml)
|
|
6
|
+
[](https://codecov.io/gh/UltiRequiem/leap-year)
|
|
7
|
+
|
|
8
|
+
Check if a year is a [leap year](https://en.wikipedia.org/wiki/Leap_year),
|
|
9
|
+
that's a calendar year that contains an additional day added to keep the
|
|
10
|
+
calendar year synchronized with the astronomical year or seasonal year.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
ESM only, types included: the same import works in Node.js, Bun and browsers.
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
import { isLeapYear } from "@ultirequiem/leap-year";
|
|
18
|
+
|
|
19
|
+
isLeapYear(2014); //=> false
|
|
20
|
+
isLeapYear(2016); //=> true
|
|
21
|
+
isLeapYear("6 October 2006 00:07:00 GMT"); //=> false
|
|
22
|
+
isLeapYear("2000"); //=> false
|
|
23
|
+
isLeapYear(new Date(2016, 1, 1)); //=> true
|
|
24
|
+
isLeapYear("13 December 400 00:07:00 GMT", true); //=> { year: 400, result: true }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Related
|
|
28
|
+
|
|
29
|
+
- [leap_year_utils](https://github.com/UltiRequiem/leap_year_utils): Utilities
|
|
30
|
+
around this package.
|
|
31
|
+
|
|
32
|
+
- [parse_datelike](https://github.com/UltiRequiem/parse_datelike): Parse a
|
|
33
|
+
number/string/date to a number/string/date.
|
|
34
|
+
|
|
35
|
+
- [leap-year-api](https://github.com/UltiRequiem/leap-year-api): A REST API to
|
|
36
|
+
know if a year is leap or not.
|
|
37
|
+
|
|
38
|
+
## Support
|
|
39
|
+
|
|
40
|
+
Open an Issue, I will check it as soon as possible 👀
|
|
41
|
+
|
|
42
|
+
If you want to hurry me up a bit
|
|
43
|
+
[send me a tweet](https://twitter.com/UltiRequiem) 😆
|
|
44
|
+
|
|
45
|
+
Don't forget to star the repo ⭐
|
|
46
|
+
|
|
47
|
+
## Versioning
|
|
48
|
+
|
|
49
|
+
We use [Semantic Versioning](http://semver.org). For the versions available, see
|
|
50
|
+
the [tags](https://github.com/UltiRequiem/leap-year/tags) 🏷️
|
|
51
|
+
|
|
52
|
+
## Licence
|
|
53
|
+
|
|
54
|
+
Licensed under the MIT License 📄
|
package/README.md
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
# leap-year
|
|
2
|
-
|
|
3
|
-
[](https://github.com/UltiRequiem/leap-year/actions/workflows/ci.yaml)
|
|
4
|
-
[](https://codecov.io/gh/ultirequiem/leap-year)
|
|
5
|
-
[](https://doc.deno.land/https/deno.land/x/leap_year/mod.ts)
|
|
6
|
-
|
|
7
|
-

|
|
8
|
-

|
|
9
|
-
[](https://deno.land/x/leap_year)
|
|
10
|
-
|
|
11
|
-
Check if a year is a [leap year](https://en.wikipedia.org/wiki/Leap_year).
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
### [Deno 🚀](https://deno.land/x/leap_year)
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
import { isLeapYear } from "https://deno.land/x/leap_year/mod.ts";
|
|
19
|
-
|
|
20
|
-
isLeapYear(2014); //=> false
|
|
21
|
-
isLeapYear(2016); //=> true
|
|
22
|
-
isLeapYear("6 October 2006 00:07:00 GMT"); //=> false
|
|
23
|
-
isLeapYear("2000"); //=> false
|
|
24
|
-
isLeapYear(new Date(2016, 1, 1)); //=> true
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### [Node.js 🐢](https://www.npmjs.com/package/@ultirequiem/leap-year)
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
import { isLeapYear } from "@ultirequiem/leap-year";
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### [Browser 🕸](https://developer.mozilla.org/en-US/docs/Glossary/Browser)
|
|
34
|
-
|
|
35
|
-
You can use any [CDN](https://en.wikipedia.org/wiki/Content_delivery_network)
|
|
36
|
-
that you like.
|
|
37
|
-
|
|
38
|
-
As an example, for
|
|
39
|
-
[type module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
|
|
40
|
-
👇
|
|
41
|
-
|
|
42
|
-
- [SkyPack](https://cdn.skypack.dev/@ultirequiem/leap-year)
|
|
43
|
-
|
|
44
|
-
Or for a plain
|
|
45
|
-
[script tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script) 🦔
|
|
46
|
-
|
|
47
|
-
- [jsdelivr](https://cdn.jsdelivr.net/npm/@ultirequiem/leap-year)
|
|
48
|
-
|
|
49
|
-
The API is the same on all this platforms.
|
|
50
|
-
|
|
51
|
-
## API
|
|
52
|
-
|
|
53
|
-
[Autogenerated Documentation](https://doc.deno.land/https://deno.land/x/leap_year/mod.ts)
|
|
54
|
-
🚀
|
|
55
|
-
|
|
56
|
-
## Support
|
|
57
|
-
|
|
58
|
-
Open an Issue, I will check it a soon as possible 👀
|
|
59
|
-
|
|
60
|
-
If you want to hurry me up a bit
|
|
61
|
-
[send me a tweet](https://twitter.com/intent/tweet?text=%40UltiRequiem%20) 😆
|
|
62
|
-
|
|
63
|
-
Consider [supporting me on Patreon](https://patreon.com/UltiRequiem) if you like
|
|
64
|
-
my work 🚀
|
|
65
|
-
|
|
66
|
-
Don't forget to start the repo ⭐
|
|
67
|
-
|
|
68
|
-
## Licence
|
|
69
|
-
|
|
70
|
-
Licensed under the MIT License.
|
package/esm/mod.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Check if a year is a [leap year](https://en.wikipedia.org/wiki/Leap_year).
|
|
3
|
-
*
|
|
4
|
-
* @param year - Default: `new Date()`.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```javascript
|
|
8
|
-
* import { isLeapYear } from "https://deno.land/x/leap_year/mod.ts";
|
|
9
|
-
*
|
|
10
|
-
* isLeapYear(2014); //=> false
|
|
11
|
-
* isLeapYear(2016); //=> true
|
|
12
|
-
* isLeapYear("6 October 2006 00:07:00 GMT") //=> false
|
|
13
|
-
* isLeapYear("200"); //=> false
|
|
14
|
-
* isLeapYear(new Date(2016, 1, 1)); //=> true
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export function isLeapYear(year = new Date()) {
|
|
18
|
-
year = internalParse(year);
|
|
19
|
-
return (year % 4 === 0 && year % 100 !== 0) ?? year % 400 === 0;
|
|
20
|
-
}
|
|
21
|
-
export function generateLeapYear(config) {
|
|
22
|
-
const { min = 0, max = new Date() } = config;
|
|
23
|
-
const maxYear = internalParse(max);
|
|
24
|
-
const leapYears = [];
|
|
25
|
-
for (let year = min; year <= maxYear; year++) {
|
|
26
|
-
if (isLeapYear(year)) {
|
|
27
|
-
leapYears.push(year);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return leapYears;
|
|
31
|
-
}
|
|
32
|
-
function internalParse(date) {
|
|
33
|
-
if (typeof date === "string") {
|
|
34
|
-
date = new Date(date);
|
|
35
|
-
}
|
|
36
|
-
if (!(date instanceof Date) && typeof date !== "number") {
|
|
37
|
-
throw new TypeError(`Expected "year" type to be "Date" | "number" | "string" and got ${typeof date}.`);
|
|
38
|
-
}
|
|
39
|
-
return date instanceof Date ? date.getUTCFullYear() : date;
|
|
40
|
-
}
|
package/esm/package.json
DELETED
package/script/mod.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateLeapYear = exports.isLeapYear = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Check if a year is a [leap year](https://en.wikipedia.org/wiki/Leap_year).
|
|
6
|
-
*
|
|
7
|
-
* @param year - Default: `new Date()`.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```javascript
|
|
11
|
-
* import { isLeapYear } from "https://deno.land/x/leap_year/mod.ts";
|
|
12
|
-
*
|
|
13
|
-
* isLeapYear(2014); //=> false
|
|
14
|
-
* isLeapYear(2016); //=> true
|
|
15
|
-
* isLeapYear("6 October 2006 00:07:00 GMT") //=> false
|
|
16
|
-
* isLeapYear("200"); //=> false
|
|
17
|
-
* isLeapYear(new Date(2016, 1, 1)); //=> true
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
function isLeapYear(year = new Date()) {
|
|
21
|
-
year = internalParse(year);
|
|
22
|
-
return (year % 4 === 0 && year % 100 !== 0) ?? year % 400 === 0;
|
|
23
|
-
}
|
|
24
|
-
exports.isLeapYear = isLeapYear;
|
|
25
|
-
function generateLeapYear(config) {
|
|
26
|
-
const { min = 0, max = new Date() } = config;
|
|
27
|
-
const maxYear = internalParse(max);
|
|
28
|
-
const leapYears = [];
|
|
29
|
-
for (let year = min; year <= maxYear; year++) {
|
|
30
|
-
if (isLeapYear(year)) {
|
|
31
|
-
leapYears.push(year);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return leapYears;
|
|
35
|
-
}
|
|
36
|
-
exports.generateLeapYear = generateLeapYear;
|
|
37
|
-
function internalParse(date) {
|
|
38
|
-
if (typeof date === "string") {
|
|
39
|
-
date = new Date(date);
|
|
40
|
-
}
|
|
41
|
-
if (!(date instanceof Date) && typeof date !== "number") {
|
|
42
|
-
throw new TypeError(`Expected "year" type to be "Date" | "number" | "string" and got ${typeof date}.`);
|
|
43
|
-
}
|
|
44
|
-
return date instanceof Date ? date.getUTCFullYear() : date;
|
|
45
|
-
}
|
package/script/package.json
DELETED
/package/{LICENSE → license}
RENAMED
|
File without changes
|