Skip to content

Commit 1d98588

Browse files
authored
Add date utility functions (#16)
Resolves #9
1 parent a2ebc7f commit 1d98588

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module.exports = {
7979
"FunctionDeclaration",
8080
"FunctionExpression",
8181
"MethodDefinition",
82-
"Property",
82+
"PropertyDefinition",
8383
"TSDeclareFunction",
8484
"TSEnumDeclaration",
8585
"TSInterfaceDeclaration",

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- added `dateIsValid`, `dateIsLastDayOfMonth` and `dateDifferenceInDays` utility functions
11+
1012
## [0.1.1] - 2023-08-04
1113

1214
### Fixed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
"start-yalc": "yarn nodemon --watch dist -x \"yarn yalc push\"",
4343
"storybook": "start-storybook -p 6006"
4444
},
45-
"dependencies": {},
45+
"dependencies": {
46+
"date-fns": "^2.30.0"
47+
},
4648
"devDependencies": {
4749
"@babel/core": "^7.21.8",
4850
"@popperjs/core": "^2.11.7",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./lib/date";
12
export * from "./lib/enum";

src/lib/date.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { dateIsValid, dateIsLastDayOfMonth, dateDifferenceInDays } from "./date";
2+
3+
describe("date tests", () => {
4+
test.each([
5+
// Valid dates
6+
[new Date(), true],
7+
[new Date(new Date()), true],
8+
[new Date("2014-03-15"), true],
9+
[new Date(2014, 3, 15), true],
10+
[new Date(42), true],
11+
12+
// Invalid dates
13+
[new Date(Number.MAX_VALUE), false],
14+
[new Date(Number.NaN), false],
15+
[new Date("2014-03-36"), false],
16+
[null as unknown as Date, false],
17+
[undefined as unknown as Date, false],
18+
[42 as unknown as Date, false],
19+
["2014-03-15" as unknown as Date, false],
20+
[{ year: 2014, month: 2, day: 15 } as unknown as Date, false],
21+
])("dateIsValid", (date, expected) => {
22+
expect(dateIsValid(date)).toBe(expected);
23+
});
24+
25+
test.each([
26+
[new Date(2014, 12, 31), true],
27+
[new Date(2014, 12, 30), false],
28+
[new Date(2015, 1, 1), false],
29+
])("dateIsLastDayOfMonth", (date, expected) => {
30+
expect(dateIsLastDayOfMonth(date)).toBe(expected);
31+
});
32+
33+
test.each([
34+
[new Date(2014, 3, 15), new Date(2014, 3, 15), 0],
35+
[new Date(2014, 3, 15), new Date(2014, 3, 16), 1],
36+
[new Date(2014, 3, 16), new Date(2014, 3, 15), 1],
37+
[new Date(2014, 3, 15, 0, 0), new Date(2014, 3, 15, 23, 59), 0],
38+
[new Date(2014, 3, 15, 23, 59), new Date(2014, 3, 16, 0, 0), 1],
39+
[null as unknown as Date, new Date(), Number.NaN],
40+
[new Date(), null as unknown as Date, Number.NaN],
41+
])("dateDifferenceInDays", (dateFrom, dateTo, expected) => {
42+
expect(dateDifferenceInDays(dateFrom, dateTo)).toBe(expected);
43+
});
44+
});

src/lib/date.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { isDate, isValid, isLastDayOfMonth, differenceInCalendarDays } from "date-fns";
2+
3+
/**
4+
* Check whether the date is valid
5+
* @param date The date
6+
* @returns A boolean indicating whether the date is valid
7+
*/
8+
export function dateIsValid(date: Date): boolean {
9+
return isDate(date) && isValid(date);
10+
}
11+
12+
/**
13+
* Check whether the date is the last day of the month
14+
* @param date The date
15+
* @returns A boolean indicating whether the date is the last day of the month
16+
*/
17+
export function dateIsLastDayOfMonth(date: Date): boolean {
18+
return isDate(date) && isLastDayOfMonth(date);
19+
}
20+
21+
/**
22+
* Calculate the difference in days between two dates
23+
* The order is not relevant hence the result is always positive
24+
* @param from The start date
25+
* @param to The end date
26+
* @returns The difference in days between two dates
27+
*/
28+
export function dateDifferenceInDays(from: Date, to: Date): number {
29+
return Math.abs(differenceInCalendarDays(from, to));
30+
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ data-uri-to-buffer@^5.0.1:
22812281
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-5.0.1.tgz#db89a9e279c2ffe74f50637a59a32fb23b3e4d7c"
22822282
integrity sha512-a9l6T1qqDogvvnw0nKlfZzqsyikEBZBClF39V3TFoKhDtGBqHu2HkuomJc02j5zft8zrUaXEuoicLeW54RkzPg==
22832283

2284-
date-fns@^2.29.3:
2284+
date-fns@^2.29.3, date-fns@^2.30.0:
22852285
version "2.30.0"
22862286
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"
22872287
integrity sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==

0 commit comments

Comments
 (0)