Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = {
"FunctionDeclaration",
"FunctionExpression",
"MethodDefinition",
"Property",
"PropertyDefinition",
"TSDeclareFunction",
"TSEnumDeclaration",
"TSInterfaceDeclaration",
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- added `dateIsValid`, `dateIsLastDayOfMonth` and `dateDifferenceInDays` utility functions

## [0.1.1] - 2023-08-04

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"start-yalc": "yarn nodemon --watch dist -x \"yarn yalc push\"",
"storybook": "start-storybook -p 6006"
},
"dependencies": {},
"dependencies": {
"date-fns": "^2.30.0"
},
"devDependencies": {
"@babel/core": "^7.21.8",
"@popperjs/core": "^2.11.7",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./lib/date";
export * from "./lib/enum";
44 changes: 44 additions & 0 deletions src/lib/date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { dateIsValid, dateIsLastDayOfMonth, dateDifferenceInDays } from "./date";

describe("date tests", () => {
test.each([
// Valid dates
[new Date(), true],
[new Date(new Date()), true],
[new Date("2014-03-15"), true],
[new Date(2014, 3, 15), true],
[new Date(42), true],

// Invalid dates
[new Date(Number.MAX_VALUE), false],
[new Date(Number.NaN), false],
[new Date("2014-03-36"), false],
[null as unknown as Date, false],
[undefined as unknown as Date, false],
[42 as unknown as Date, false],
["2014-03-15" as unknown as Date, false],
[{ year: 2014, month: 2, day: 15 } as unknown as Date, false],
])("dateIsValid", (date, expected) => {
expect(dateIsValid(date)).toBe(expected);
});

test.each([
[new Date(2014, 12, 31), true],
[new Date(2014, 12, 30), false],
[new Date(2015, 1, 1), false],
])("dateIsLastDayOfMonth", (date, expected) => {
expect(dateIsLastDayOfMonth(date)).toBe(expected);
});

test.each([
[new Date(2014, 3, 15), new Date(2014, 3, 15), 0],
[new Date(2014, 3, 15), new Date(2014, 3, 16), 1],
[new Date(2014, 3, 16), new Date(2014, 3, 15), 1],
[new Date(2014, 3, 15, 0, 0), new Date(2014, 3, 15, 23, 59), 0],
[new Date(2014, 3, 15, 23, 59), new Date(2014, 3, 16, 0, 0), 1],
[null as unknown as Date, new Date(), Number.NaN],
[new Date(), null as unknown as Date, Number.NaN],
])("dateDifferenceInDays", (dateFrom, dateTo, expected) => {
expect(dateDifferenceInDays(dateFrom, dateTo)).toBe(expected);
});
});
30 changes: 30 additions & 0 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isDate, isValid, isLastDayOfMonth, differenceInCalendarDays } from "date-fns";

/**
* Check whether the date is valid
* @param date The date
* @returns A boolean indicating whether the date is valid
*/
export function dateIsValid(date: Date): boolean {
return isDate(date) && isValid(date);
}

/**
* Check whether the date is the last day of the month
* @param date The date
* @returns A boolean indicating whether the date is the last day of the month
*/
export function dateIsLastDayOfMonth(date: Date): boolean {
return isDate(date) && isLastDayOfMonth(date);
}

/**
* Calculate the difference in days between two dates
* The order is not relevant hence the result is always positive
* @param from The start date
* @param to The end date
* @returns The difference in days between two dates
*/
export function dateDifferenceInDays(from: Date, to: Date): number {
return Math.abs(differenceInCalendarDays(from, to));
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,7 @@ data-uri-to-buffer@^5.0.1:
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-5.0.1.tgz#db89a9e279c2ffe74f50637a59a32fb23b3e4d7c"
integrity sha512-a9l6T1qqDogvvnw0nKlfZzqsyikEBZBClF39V3TFoKhDtGBqHu2HkuomJc02j5zft8zrUaXEuoicLeW54RkzPg==

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