From a748d5bbac2eff8791d73c78e9a07daba4ac69d5 Mon Sep 17 00:00:00 2001 From: WilcoSp <17604138+WilcoSp@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:26:12 +0200 Subject: [PATCH] feat: optional date arguments for functions (#58) * isAfter, isBefore and diffMilliseconds now accept null/undefined to allow comparison with `now` * isBefore & isAfter doc change from now -> the current time changed isEqual to accept 1 optional date * all diff* functions now accept optional date arguments * all add* functions now support optional date argument. tests for them are combined with the ones for diff * same* now also support optional date arguments * nearestDay now accepts optional date input * *end, *start, yearDays, monthDays & dayOfYear now support optional date input * offset, removeOffset, tzDate, fill & createPartMap now support optional date input added 'the' before current * were missing * added isFuture & isPast * fix sameSecond inputDateA to Maybe * yearStart inputDate to maybe type * export isFuture & isPast * jsdoc change for MaybeDateInput to use `current time` instead of `now` --- .prettierrc | 4 ++- src/__tests__/addDay.spec.ts | 2 ++ src/__tests__/addHour.spec.ts | 6 ++-- src/__tests__/addMinute.spec.ts | 2 ++ src/__tests__/addMonth.spec.ts | 26 +++++--------- src/__tests__/addSecond.spec.ts | 2 ++ src/__tests__/addYear.spec.ts | 2 ++ src/__tests__/dayEnd.spec.ts | 9 +++-- src/__tests__/dayOfYear.spec.ts | 7 ++++ src/__tests__/dayStart.spec.ts | 6 ++++ src/__tests__/diffDays.spec.ts | 10 ++++-- src/__tests__/diffHours.spec.ts | 10 ++++-- src/__tests__/diffMilliseconds.spec.ts | 20 ++++++++--- src/__tests__/diffMinutes.spec.ts | 16 ++++----- src/__tests__/diffMonths.spec.ts | 6 ++++ src/__tests__/diffSeconds.spec.ts | 10 ++++-- src/__tests__/diffYears.spec.ts | 6 ++++ src/__tests__/hourEnd.spec.ts | 9 +++-- src/__tests__/hourStart.spec.ts | 6 ++++ src/__tests__/isAfter.spec.ts | 10 +++++- src/__tests__/isBefore.spec.ts | 10 +++++- src/__tests__/isEqual.spec.ts | 7 +++- src/__tests__/isFuture.spec.ts | 11 ++++++ src/__tests__/isPast.spec.ts | 11 ++++++ src/__tests__/minuteEnd.spec.ts | 6 ++++ src/__tests__/minuteStart.spec.ts | 6 ++++ src/__tests__/monthDays.spec.ts | 8 ++++- src/__tests__/monthEnd.spec.ts | 15 ++++---- src/__tests__/monthStart.spec.ts | 6 ++++ src/__tests__/nearestDay.spec.ts | 25 +++++++------- src/__tests__/sameDay.spec.ts | 5 +++ src/__tests__/sameHour.spec.ts | 6 ++++ src/__tests__/sameMinute.spec.ts | 6 ++++ src/__tests__/sameMonth.spec.ts | 6 ++++ src/__tests__/sameSecond.spec.ts | 6 ++++ src/__tests__/sameYear.spec.ts | 6 ++++ src/__tests__/weekEnd.spec.ts | 19 +++++----- src/__tests__/weekStart.spec.ts | 23 ++++++------ src/__tests__/yearDays.spec.ts | 9 +++++ src/__tests__/yearEnd.spec.ts | 11 ++++-- src/__tests__/yearStart.spec.ts | 7 ++++ src/addDay.ts | 7 ++-- src/addHour.ts | 7 ++-- src/addMinute.ts | 9 ++--- src/addMonth.ts | 14 +++----- src/addSecond.ts | 7 ++-- src/addYear.ts | 10 +++--- src/applyOffset.ts | 8 ++--- src/common.ts | 48 +++++++------------------- src/date.ts | 4 +-- src/dayEnd.ts | 6 ++-- src/dayOfYear.ts | 6 ++-- src/dayStart.ts | 6 ++-- src/diffDays.ts | 28 ++++++++++++--- src/diffHours.ts | 29 +++++++++++++--- src/diffMilliseconds.ts | 17 ++++++--- src/diffMinutes.ts | 33 +++++++++++++++--- src/diffMonths.ts | 19 ++++++---- src/diffSeconds.ts | 34 ++++++++++++++---- src/diffWeeks.ts | 28 ++++++++++++--- src/diffYears.ts | 21 ++++++++--- src/hourEnd.ts | 6 ++-- src/hourStart.ts | 6 ++-- src/index.ts | 2 ++ src/isAfter.ts | 12 +++---- src/isBefore.ts | 10 +++--- src/isEqual.ts | 19 +++++++--- src/isFuture.ts | 11 ++++++ src/isPast.ts | 11 ++++++ src/minuteEnd.ts | 6 ++-- src/minuteStart.ts | 6 ++-- src/monthDays.ts | 6 ++-- src/monthEnd.ts | 6 ++-- src/monthStart.ts | 6 ++-- src/nearestDay.ts | 6 ++-- src/offset.ts | 14 ++++---- src/removeOffset.ts | 8 ++--- src/sameDay.ts | 14 ++++++-- src/sameHour.ts | 14 ++++++-- src/sameMinute.ts | 14 ++++++-- src/sameMonth.ts | 14 ++++++-- src/sameSecond.ts | 14 ++++++-- src/sameYear.ts | 14 ++++++-- src/types.ts | 5 +++ src/tzDate.ts | 8 +++-- src/weekEnd.ts | 8 ++--- src/weekStart.ts | 8 ++--- src/yearDays.ts | 6 ++-- src/yearEnd.ts | 15 ++++---- src/yearStart.ts | 9 +++-- 90 files changed, 694 insertions(+), 303 deletions(-) create mode 100644 src/__tests__/isFuture.spec.ts create mode 100644 src/__tests__/isPast.spec.ts create mode 100644 src/isFuture.ts create mode 100644 src/isPast.ts diff --git a/.prettierrc b/.prettierrc index 7c28b83..cfe4f39 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,7 @@ { "tabWidth": 2, "useTabs": false, - "semi": false + "semi": false, + "printWidth": 90, + "trailingComma": "es5" } diff --git a/src/__tests__/addDay.spec.ts b/src/__tests__/addDay.spec.ts index 92aa56e..e235803 100644 --- a/src/__tests__/addDay.spec.ts +++ b/src/__tests__/addDay.spec.ts @@ -14,4 +14,6 @@ describe("addDay", () => { it("gets the next day by providing specified negative number of days", () => { expect(addDay("2022-01-01", -5).toISOString()).toBe("2021-12-27T05:00:00.000Z") }) + + // test with the current time is at diffDays }) diff --git a/src/__tests__/addHour.spec.ts b/src/__tests__/addHour.spec.ts index 211f1e0..830ebd4 100644 --- a/src/__tests__/addHour.spec.ts +++ b/src/__tests__/addHour.spec.ts @@ -4,9 +4,7 @@ process.env.TZ = "America/New_York" describe("addHour", () => { it("can increment a normal hour", () => { - expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe( - "2022-01-01T01:00:00.000Z" - ) + expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe("2022-01-01T01:00:00.000Z") }) it("can increment the last hours of the day into a new day", () => { expect(addHour("2022-01-01T23:11:00Z", 3).toISOString()).toBe( @@ -18,4 +16,6 @@ describe("addHour", () => { "2021-12-31T19:11:00.000Z" ) }) + + // test with the current time is at diffHours. }) diff --git a/src/__tests__/addMinute.spec.ts b/src/__tests__/addMinute.spec.ts index e73c34a..54c3d07 100644 --- a/src/__tests__/addMinute.spec.ts +++ b/src/__tests__/addMinute.spec.ts @@ -18,4 +18,6 @@ describe("addMinute", () => { "2021-12-31T23:10:00.000Z" ) }) + + // test with the current time is at diffMinutes. }) diff --git a/src/__tests__/addMonth.spec.ts b/src/__tests__/addMonth.spec.ts index 829ca6c..c15e158 100644 --- a/src/__tests__/addMonth.spec.ts +++ b/src/__tests__/addMonth.spec.ts @@ -4,31 +4,21 @@ process.env.TZ = "America/New_York" describe("addMonth", () => { it("gets the next month on the first", () => { - expect(addMonth("2022-01-01").toISOString()).toBe( - "2022-02-01T05:00:00.000Z" - ) + expect(addMonth("2022-01-01").toISOString()).toBe("2022-02-01T05:00:00.000Z") }) it("can overflow a month month when the next month has fewer days", () => { - expect(addMonth("2000-01-31", 1, true).toISOString()).toBe( - "2000-03-02T05:00:00.000Z" - ) + expect(addMonth("2000-01-31", 1, true).toISOString()).toBe("2000-03-02T05:00:00.000Z") }) it("goe to the same day of the month on the next month", () => { - expect(addMonth("2000-06-04").toISOString()).toBe( - "2000-07-04T04:00:00.000Z" - ) + expect(addMonth("2000-06-04").toISOString()).toBe("2000-07-04T04:00:00.000Z") }) it("can add multiple months by passing a second argument", () => { - expect(addMonth("2000-01-01", 2).toISOString()).toBe( - "2000-03-01T05:00:00.000Z" - ) + expect(addMonth("2000-01-01", 2).toISOString()).toBe("2000-03-01T05:00:00.000Z") }) it("can add years months by passing a second argument", () => { - expect(addMonth("2000-01-01", 25).toISOString()).toBe( - "2002-02-01T05:00:00.000Z" - ) + expect(addMonth("2000-01-01", 25).toISOString()).toBe("2002-02-01T05:00:00.000Z") }) it("can prevent month overflow with third argument", () => { expect(addMonth("2020-01-31", 1, false).toISOString()).toBe( @@ -36,13 +26,13 @@ describe("addMonth", () => { ) }) it("can subtract multiple months", () => { - expect(addMonth("2020-01-31", -2).toISOString()).toBe( - "2019-11-30T05:00:00.000Z" - ) + expect(addMonth("2020-01-31", -2).toISOString()).toBe("2019-11-30T05:00:00.000Z") }) it("can subtract multiple months and allow overflow", () => { expect(addMonth("2020-01-31", -2, true).toISOString()).toBe( "2019-12-01T05:00:00.000Z" ) }) + + // test with the current time is at diffMonths. }) diff --git a/src/__tests__/addSecond.spec.ts b/src/__tests__/addSecond.spec.ts index 842302e..f75a070 100644 --- a/src/__tests__/addSecond.spec.ts +++ b/src/__tests__/addSecond.spec.ts @@ -18,4 +18,6 @@ describe("addSecond", () => { "2021-12-31T23:59:30.000Z" ) }) + + // test with the current time is at diffSeconds. }) diff --git a/src/__tests__/addYear.spec.ts b/src/__tests__/addYear.spec.ts index 13053df..aa3b3a1 100644 --- a/src/__tests__/addYear.spec.ts +++ b/src/__tests__/addYear.spec.ts @@ -16,4 +16,6 @@ describe("addYear", () => { it("can overflow the day of the month on leap year", () => { expect(addYear("2000-02-29").toISOString()).toBe("2001-02-28T05:00:00.000Z") }) + + // test with the current time is at diffYears }) diff --git a/src/__tests__/dayEnd.spec.ts b/src/__tests__/dayEnd.spec.ts index fc4fe65..73550d2 100644 --- a/src/__tests__/dayEnd.spec.ts +++ b/src/__tests__/dayEnd.spec.ts @@ -4,8 +4,11 @@ process.env.TZ = "America/New_York" describe("dayEnd", () => { it("can become the end of the day", () => { - expect(dayEnd("2023-02-22T12:00:00Z").toISOString()).toBe( - "2023-02-23T04:59:59.999Z" - ) + expect(dayEnd("2023-02-22T12:00:00Z").toISOString()).toBe("2023-02-23T04:59:59.999Z") + }) + it("can become the end of the current day", () => { + const compare = new Date() + compare.setHours(23, 59, 59, 999) + expect(dayEnd()).toEqual(compare) }) }) diff --git a/src/__tests__/dayOfYear.spec.ts b/src/__tests__/dayOfYear.spec.ts index 822029d..591572c 100644 --- a/src/__tests__/dayOfYear.spec.ts +++ b/src/__tests__/dayOfYear.spec.ts @@ -1,5 +1,7 @@ import { describe, it, expect } from "vitest" import { dayOfYear } from "../dayOfYear" +import { yearStart } from "../yearStart" +import { diffDays } from "../diffDays" process.env.TZ = "America/New_York" describe("dayOfYear", () => { @@ -9,4 +11,9 @@ describe("dayOfYear", () => { it("can find the number of days in a year", () => { expect(dayOfYear("2020-08-01")).toBe(214) }) + + it("can find the number of days of the current day", () => { + const start = yearStart() + expect(dayOfYear()).toBe(diffDays(null, start) + 1) + }) }) diff --git a/src/__tests__/dayStart.spec.ts b/src/__tests__/dayStart.spec.ts index e0b35d5..1c660b7 100644 --- a/src/__tests__/dayStart.spec.ts +++ b/src/__tests__/dayStart.spec.ts @@ -8,4 +8,10 @@ describe("dayStart", () => { "2023-02-22T05:00:00.000Z" ) }) + + it("gets the start of the day", () => { + const compare = new Date() + compare.setHours(0, 0, 0, 0) + expect(dayStart()).toEqual(compare) + }) }) diff --git a/src/__tests__/diffDays.spec.ts b/src/__tests__/diffDays.spec.ts index cd4dd91..620ca1b 100644 --- a/src/__tests__/diffDays.spec.ts +++ b/src/__tests__/diffDays.spec.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest" import { diffDays } from "../diffDays" +import { addDay } from "../addDay" describe("differenceInDays", () => { it("difference is 3 days", () => { @@ -7,9 +8,7 @@ describe("differenceInDays", () => { }) it("difference is 2 days", () => { - expect( - diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z") - ).toBe(2) + expect(diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z")).toBe(2) }) it("difference is 3 days by using round", () => { @@ -17,4 +16,9 @@ describe("differenceInDays", () => { diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z", "round") ).toBe(3) }) + + it("different should be -64 hours compared to the current time", () => { + const compare = addDay(null, -28) + expect(diffDays(compare)).toBe(-28) + }) }) diff --git a/src/__tests__/diffHours.spec.ts b/src/__tests__/diffHours.spec.ts index 06788cb..822f021 100644 --- a/src/__tests__/diffHours.spec.ts +++ b/src/__tests__/diffHours.spec.ts @@ -1,10 +1,14 @@ import { describe, expect, it } from "vitest" import { diffHours } from "../diffHours" +import { addHour } from "../addHour" describe("differenceInHours", () => { it("difference is 5 hours", () => { - expect( - diffHours("2024-04-07T15:28:00.000Z", "2024-04-07T09:50:00.000Z") - ).toBe(5) + expect(diffHours("2024-04-07T15:28:00.000Z", "2024-04-07T09:50:00.000Z")).toBe(5) + }) + + it("different should be -64 hours compared to the current time", () => { + const compare = addHour(null, 64) + expect(diffHours(null, compare)).toBe(-64) }) }) diff --git a/src/__tests__/diffMilliseconds.spec.ts b/src/__tests__/diffMilliseconds.spec.ts index da8b4d5..ac06f4a 100644 --- a/src/__tests__/diffMilliseconds.spec.ts +++ b/src/__tests__/diffMilliseconds.spec.ts @@ -1,10 +1,22 @@ import { describe, it, expect } from "vitest" import { diffMilliseconds } from "../diffMilliseconds" -describe("differenceInMilliseconds", () => { +describe("diffMilliseconds", () => { it("difference is 257 milliseconds", () => { - expect( - diffMilliseconds("2024-04-07T09:10:48.257Z", "2024-04-07T09:10:48.000Z") - ).toBe(257) + expect(diffMilliseconds("2024-04-07T09:10:48.257Z", "2024-04-07T09:10:48.000Z")).toBe( + 257 + ) + }) + + it("should be 5000 milleseconds difference compared to the current time", () => { + const now = new Date() + now.setMilliseconds(5000) // because the date function sets ms to 0, the test needs to test with increments of 1000 + expect(diffMilliseconds(now)).toBe(5000) + }) + + it("should be -5000 milleseconds difference compared to the current time", () => { + const now = new Date() + now.setMilliseconds(5000) + expect(diffMilliseconds(null, now)).toBe(-5000) }) }) diff --git a/src/__tests__/diffMinutes.spec.ts b/src/__tests__/diffMinutes.spec.ts index 1d8dddf..9375afa 100644 --- a/src/__tests__/diffMinutes.spec.ts +++ b/src/__tests__/diffMinutes.spec.ts @@ -1,19 +1,19 @@ import { describe, it, expect } from "vitest" import { diffMinutes } from "../diffMinutes" +import { addMinute } from "../addMinute" describe("differenceInMinutes", () => { it("difference is 18 minutes", () => { - expect( - diffMinutes("2024-04-07T09:28:30.050Z", "2024-04-07T09:10:00.000Z") - ).toBe(18) + expect(diffMinutes("2024-04-07T09:28:30.050Z", "2024-04-07T09:10:00.000Z")).toBe(18) }) it("difference is 19 minutes by using ceil", () => { expect( - diffMinutes( - "2024-04-07T09:28:01.050Z", - "2024-04-07T09:10:00.000Z", - "ceil" - ) + diffMinutes("2024-04-07T09:28:01.050Z", "2024-04-07T09:10:00.000Z", "ceil") ).toBe(19) }) + + it("different should be 23 minutes compared to the current time", () => { + const compare = addMinute(null, 23) + expect(diffMinutes(compare)).toBe(23) + }) }) diff --git a/src/__tests__/diffMonths.spec.ts b/src/__tests__/diffMonths.spec.ts index 4372c16..2ebaa46 100644 --- a/src/__tests__/diffMonths.spec.ts +++ b/src/__tests__/diffMonths.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect } from "vitest" import { diffMonths } from "../diffMonths" +import { addMonth } from "../addMonth" describe("differenceInMonths", () => { it("should give 11 months", () => { @@ -29,4 +30,9 @@ describe("differenceInMonths", () => { it("should also be a negative full month when swapped", () => { expect(diffMonths("2024-01-31", "2024-02-29")).toBe(-1) }) + + it("different should be 3 month compared to the current time", () => { + const compare = addMonth(null, 3) + expect(diffMonths(compare)).toBe(3) + }) }) diff --git a/src/__tests__/diffSeconds.spec.ts b/src/__tests__/diffSeconds.spec.ts index ab35cfe..4ed742c 100644 --- a/src/__tests__/diffSeconds.spec.ts +++ b/src/__tests__/diffSeconds.spec.ts @@ -1,10 +1,14 @@ import { describe, it, expect } from "vitest" import { diffSeconds } from "../diffSeconds" +import { addSecond } from "../addSecond" describe("differenceInSeconds", () => { it("difference is 28 seconds", () => { - expect( - diffSeconds("2024-04-07T09:10:28.900Z", "2024-04-07T09:10:00.000Z") - ).toBe(28) + expect(diffSeconds("2024-04-07T09:10:28.900Z", "2024-04-07T09:10:00.000Z")).toBe(28) + }) + + it("different should be 50 seconds compared to the current time", () => { + const compare = addSecond(null, 50) + expect(diffSeconds(compare)).toBe(50) }) }) diff --git a/src/__tests__/diffYears.spec.ts b/src/__tests__/diffYears.spec.ts index 22404aa..dedbb01 100644 --- a/src/__tests__/diffYears.spec.ts +++ b/src/__tests__/diffYears.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect, suite } from "vitest" import { diffYears } from "../diffYears" +import { addYear } from "../addYear" describe("differenceInYears", () => { it("returns the amount of full years between dates", () => { @@ -45,4 +46,9 @@ describe("differenceInYears", () => { expect(diffYears("2024-04-27", "2025-04-26")).toBe(0) }) }) + + it("different should be 3 month compared to the current time", () => { + const compare = addYear(null, -6) + expect(diffYears(null, compare)).toBe(6) + }) }) diff --git a/src/__tests__/hourEnd.spec.ts b/src/__tests__/hourEnd.spec.ts index d1d1812..086832f 100644 --- a/src/__tests__/hourEnd.spec.ts +++ b/src/__tests__/hourEnd.spec.ts @@ -4,8 +4,11 @@ process.env.TZ = "America/New_York" describe("hourEnd", () => { it("can become the end of the hour", () => { - expect(hourEnd("2023-02-22T12:30:00Z").toISOString()).toBe( - "2023-02-22T12:59:59.999Z" - ) + expect(hourEnd("2023-02-22T12:30:00Z").toISOString()).toBe("2023-02-22T12:59:59.999Z") + }) + it("can become the end of the current hour", () => { + const compare = new Date() + compare.setMinutes(59, 59, 999) + expect(hourEnd()).toEqual(compare) }) }) diff --git a/src/__tests__/hourStart.spec.ts b/src/__tests__/hourStart.spec.ts index b297dd2..bce76d3 100644 --- a/src/__tests__/hourStart.spec.ts +++ b/src/__tests__/hourStart.spec.ts @@ -8,4 +8,10 @@ describe("hourStart", () => { "2023-02-22T12:00:00.000Z" ) }) + + it("can become the start of the current hour", () => { + const compare = new Date() + compare.setMinutes(0, 0, 0) + expect(hourStart()).toEqual(compare) + }) }) diff --git a/src/__tests__/isAfter.spec.ts b/src/__tests__/isAfter.spec.ts index c021c4d..b666451 100644 --- a/src/__tests__/isAfter.spec.ts +++ b/src/__tests__/isAfter.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect } from "vitest" import { isAfter } from "../isAfter" +import { addDay } from "../addDay" process.env.TZ = "America/New_York" describe("isAfter", () => { @@ -11,7 +12,14 @@ describe("isAfter", () => { }) it("returns error if date is not valid", () => { expect(() => isAfter("invalid", "2022-01-01")).toThrowError( - "Non ISO 8601 compliant date", + "Non ISO 8601 compliant date" ) }) + + it("returns false if date is in the past", () => { + expect(isAfter(addDay(new Date(), -1))).toBe(false) + }) + it("returns true if date is in the future", () => { + expect(isAfter(addDay(new Date(), 1))).toBe(true) + }) }) diff --git a/src/__tests__/isBefore.spec.ts b/src/__tests__/isBefore.spec.ts index 2bc5968..3b8f058 100644 --- a/src/__tests__/isBefore.spec.ts +++ b/src/__tests__/isBefore.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect } from "vitest" import { isBefore } from "../isBefore" +import { addDay } from "../addDay" process.env.TZ = "America/New_York" describe("isBefore", () => { @@ -11,7 +12,14 @@ describe("isBefore", () => { }) it("returns error if date is not valid", () => { expect(() => isBefore("invalid", "2022-01-01")).toThrowError( - "Non ISO 8601 compliant date", + "Non ISO 8601 compliant date" ) }) + + it("returns true if date is in the past", () => { + expect(isBefore(addDay(new Date(), -1))).toBe(true) + }) + it("returns false if date is in the future", () => { + expect(isBefore(addDay(new Date(), 1))).toBe(false) + }) }) diff --git a/src/__tests__/isEqual.spec.ts b/src/__tests__/isEqual.spec.ts index 3330098..fb1c765 100644 --- a/src/__tests__/isEqual.spec.ts +++ b/src/__tests__/isEqual.spec.ts @@ -11,7 +11,12 @@ describe("isEqual", () => { }) it("returns error if date is not valid", () => { expect(() => isEqual("invalid", "2022-01-01")).toThrowError( - "Non ISO 8601 compliant date", + "Non ISO 8601 compliant date" ) }) + + it("returns true because the create dated is the current time", () => { + expect(isEqual(null, new Date())).toBe(true) + expect(isEqual(new Date())).toBe(true) + }) }) diff --git a/src/__tests__/isFuture.spec.ts b/src/__tests__/isFuture.spec.ts new file mode 100644 index 0000000..05f652e --- /dev/null +++ b/src/__tests__/isFuture.spec.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from "vitest" +import { isFuture } from "../isFuture" + +describe("isFuture", () => { + it("should return false if date is in the past", () => { + expect(isFuture(new Date(0))).toBe(false) + }) + it("should give true if the date is in the future", () => { + expect(isFuture(new Date("3000-01-01"))).toBe(true) + }) +}) diff --git a/src/__tests__/isPast.spec.ts b/src/__tests__/isPast.spec.ts new file mode 100644 index 0000000..8379202 --- /dev/null +++ b/src/__tests__/isPast.spec.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from "vitest" +import { isPast } from "../isPast" + +describe("isPast", () => { + it("should return true if date is in the past", () => { + expect(isPast(new Date(0))).toBe(true) + }) + it("should give false if the date is in the future", () => { + expect(isPast(new Date("3000-01-01"))).toBe(false) + }) +}) diff --git a/src/__tests__/minuteEnd.spec.ts b/src/__tests__/minuteEnd.spec.ts index 0e74b96..9b1df58 100644 --- a/src/__tests__/minuteEnd.spec.ts +++ b/src/__tests__/minuteEnd.spec.ts @@ -8,4 +8,10 @@ describe("minuteEnd", () => { "2023-02-22T12:30:59.999Z" ) }) + + it("can become the end of the current minute", () => { + const compare = new Date() + compare.setSeconds(59, 999) + expect(minuteEnd()).toEqual(compare) + }) }) diff --git a/src/__tests__/minuteStart.spec.ts b/src/__tests__/minuteStart.spec.ts index 7856617..86b068c 100644 --- a/src/__tests__/minuteStart.spec.ts +++ b/src/__tests__/minuteStart.spec.ts @@ -8,4 +8,10 @@ describe("minuteStart", () => { "2023-02-22T12:30:00.000Z" ) }) + + it("can become the start of the current minute", () => { + const compare = new Date() + compare.setSeconds(0, 0) + expect(minuteStart()).toEqual(compare) + }) }) diff --git a/src/__tests__/monthDays.spec.ts b/src/__tests__/monthDays.spec.ts index e819f91..7bc4931 100644 --- a/src/__tests__/monthDays.spec.ts +++ b/src/__tests__/monthDays.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from "vitest" +import { describe, expect, it } from "vitest" import { monthDays } from "../monthDays" process.env.TZ = "America/New_York" @@ -15,4 +15,10 @@ describe("monthDays", () => { it("gets the correct number of Feb days on leap years", () => { expect(monthDays("2020-02-01")).toBe(29) }) + + it("gets the amount of days of the current month", () => { + const compare = new Date() + compare.setMonth(compare.getMonth() + 1, 0) // the 0 wraps to the last day of the previous month + expect(monthDays()).toBe(compare.getDate()) + }) }) diff --git a/src/__tests__/monthEnd.spec.ts b/src/__tests__/monthEnd.spec.ts index b0c94b5..8d8e0e3 100644 --- a/src/__tests__/monthEnd.spec.ts +++ b/src/__tests__/monthEnd.spec.ts @@ -1,17 +1,14 @@ import { describe, it, expect } from "vitest" import { monthEnd } from "../monthEnd" +import { date } from "../date" process.env.TZ = "America/New_York" describe("monthEnd", () => { it("gets the correct last day of Feb on leap years", () => { - expect(monthEnd("2020-02-01").toISOString()).toBe( - "2020-02-29T05:00:00.000Z" - ) + expect(monthEnd("2020-02-01").toISOString()).toBe("2020-02-29T05:00:00.000Z") }) it("gets the correct last day of August", () => { - expect(monthEnd("1999-08-01").toISOString()).toBe( - "1999-08-31T04:00:00.000Z" - ) + expect(monthEnd("1999-08-01").toISOString()).toBe("1999-08-31T04:00:00.000Z") }) it("gets the correct last day when starting from the last day", () => { expect(monthEnd("2020-01-31T05:00:00.000Z").toISOString()).toBe( @@ -23,4 +20,10 @@ describe("monthEnd", () => { "2020-01-31T05:00:00.000Z" ) }) + + it("gets the last day of the current month", () => { + const compare = date() + compare.setMonth(compare.getMonth() + 1, 0) + expect(monthEnd()).toEqual(compare) + }) }) diff --git a/src/__tests__/monthStart.spec.ts b/src/__tests__/monthStart.spec.ts index 183a310..79f5c6a 100644 --- a/src/__tests__/monthStart.spec.ts +++ b/src/__tests__/monthStart.spec.ts @@ -18,4 +18,10 @@ describe("monthStart", () => { "2000-01-01T05:00:00.000Z" ) }) + it("can become the start of the current month", () => { + const compare = new Date() + compare.setDate(1) + compare.setHours(0, 0, 0, 0) + expect(monthStart()).toEqual(compare) + }) }) diff --git a/src/__tests__/nearestDay.spec.ts b/src/__tests__/nearestDay.spec.ts index 65c431d..e3b858e 100644 --- a/src/__tests__/nearestDay.spec.ts +++ b/src/__tests__/nearestDay.spec.ts @@ -5,17 +5,11 @@ process.env.TZ = "America/New_York" describe("nearestDay", () => { it("can find the nearest day to a date", () => { expect( - nearestDay( - "2023-02-22", - (d) => d.getDay() === 6 || d.getDay() === 0 - )!.toISOString() + nearestDay("2023-02-22", (d) => d.getDay() === 6 || d.getDay() === 0)!.toISOString() ).toBe("2023-02-25T05:00:00.000Z") expect( - nearestDay( - "2023-02-21", - (d) => d.getDay() === 6 || d.getDay() === 0 - )!.toISOString() + nearestDay("2023-02-21", (d) => d.getDay() === 6 || d.getDay() === 0)!.toISOString() ).toBe("2023-02-19T05:00:00.000Z") }) it("searches following the pattern 0, 1, -1, 2, -2, 3 -3 and so on", () => { @@ -72,10 +66,8 @@ describe("nearestDay", () => { }) it("can constrain itself to a year, not moving forwards", () => { const search = (d: Date): boolean => { - if (d.getFullYear() === 2023 && d.getMonth() === 0 && d.getDate() === 1) - return true - if (d.getFullYear() === 2024 && d.getMonth() === 1 && d.getDate() === 1) - return true + if (d.getFullYear() === 2023 && d.getMonth() === 0 && d.getDate() === 1) return true + if (d.getFullYear() === 2024 && d.getMonth() === 1 && d.getDate() === 1) return true return false } expect(nearestDay("2023-11-03", search, "year")!.toISOString()).toBe( @@ -94,4 +86,13 @@ describe("nearestDay", () => { "2023-12-31T05:00:00.000Z" ) }) + + it("can find the 28th from the current month", () => { + const find = new Date() + find.setDate(28) + find.setMilliseconds(0) + const search = (d: Date) => d.getDate() == 28 + // + expect(nearestDay(null, search, "month")).toEqual(find) + }) }) diff --git a/src/__tests__/sameDay.spec.ts b/src/__tests__/sameDay.spec.ts index 90be2eb..79da347 100644 --- a/src/__tests__/sameDay.spec.ts +++ b/src/__tests__/sameDay.spec.ts @@ -22,4 +22,9 @@ describe("sameDay", () => { it("evaluates false for two adjacent days", () => { expect(sameDay("2020-11-17", new Date("2020-11-18T10:00:00Z"))).toBe(false) }) + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setHours(3) + expect(sameDay(null, compare)).toBe(true) + }) }) diff --git a/src/__tests__/sameHour.spec.ts b/src/__tests__/sameHour.spec.ts index 0a00cee..691e642 100644 --- a/src/__tests__/sameHour.spec.ts +++ b/src/__tests__/sameHour.spec.ts @@ -18,4 +18,10 @@ describe("sameHour", () => { it("can determine different dates and time with same hours", () => { expect(sameHour("2023-01-01 10:10:30", "2024-02-02 10:12:00")).toBe(true) }) + + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setDate(6) + expect(sameHour(compare)).toBe(true) + }) }) diff --git a/src/__tests__/sameMinute.spec.ts b/src/__tests__/sameMinute.spec.ts index cf25d84..4b2fdac 100644 --- a/src/__tests__/sameMinute.spec.ts +++ b/src/__tests__/sameMinute.spec.ts @@ -18,4 +18,10 @@ describe("sameMinute", () => { it("can determine different dates and time with same minutes", () => { expect(sameMinute("2024-01-01 10:10:30", "2024-02-02 20:10:00")).toBe(true) }) + + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setSeconds(45) + expect(sameMinute(compare)).toBe(true) + }) }) diff --git a/src/__tests__/sameMonth.spec.ts b/src/__tests__/sameMonth.spec.ts index 1111955..2eb9d7c 100644 --- a/src/__tests__/sameMonth.spec.ts +++ b/src/__tests__/sameMonth.spec.ts @@ -18,4 +18,10 @@ describe("sameMonth", () => { it("can determine different dates and time with same months", () => { expect(sameMonth("2023-01-01 10:10:30", "2024-01-02 11:12:00")).toBe(true) }) + + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setMilliseconds(45) + expect(sameMonth(null, compare)).toBe(true) + }) }) diff --git a/src/__tests__/sameSecond.spec.ts b/src/__tests__/sameSecond.spec.ts index 20ecdc8..d813616 100644 --- a/src/__tests__/sameSecond.spec.ts +++ b/src/__tests__/sameSecond.spec.ts @@ -18,4 +18,10 @@ describe("sameSecond", () => { it("can determine different dates and time with same seconds", () => { expect(sameSecond("2024-01-01 10:20", "2024-02-02 20:10:00")).toBe(true) }) + + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setHours(15) + expect(sameSecond(compare)).toBe(true) + }) }) diff --git a/src/__tests__/sameYear.spec.ts b/src/__tests__/sameYear.spec.ts index d9f3d55..072292a 100644 --- a/src/__tests__/sameYear.spec.ts +++ b/src/__tests__/sameYear.spec.ts @@ -18,4 +18,10 @@ describe("sameYear", () => { it("can determine different dates and time with same year", () => { expect(sameYear("2024-01-01 12:10:30", "2024-02-02 10:12:00")).toBe(true) }) + + it("evaluates true with 1 date given", () => { + const compare = new Date() + compare.setMinutes(15) + expect(sameYear(compare)).toBe(true) + }) }) diff --git a/src/__tests__/weekEnd.spec.ts b/src/__tests__/weekEnd.spec.ts index 76ea44d..790df51 100644 --- a/src/__tests__/weekEnd.spec.ts +++ b/src/__tests__/weekEnd.spec.ts @@ -4,9 +4,7 @@ process.env.TZ = "America/New_York" describe("weekEnd", () => { it("gets the last day of the week", () => { - expect(weekEnd("2022-11-28T00:00:00").toISOString()).toBe( - "2022-12-04T04:59:59.000Z" - ) + expect(weekEnd("2022-11-28T00:00:00").toISOString()).toBe("2022-12-04T04:59:59.000Z") }) it("gets the last day of the week when offset to tuesday", () => { expect(weekEnd("2022-07-08T00:00:00", 2).toISOString()).toBe( @@ -14,13 +12,16 @@ describe("weekEnd", () => { ) }) it("gets the last day of the week when offset to wednesday and the day is thursday", () => { - expect(weekEnd("2022-10-13", 3).toISOString()).toBe( - "2022-10-19T03:59:59.000Z" - ) + expect(weekEnd("2022-10-13", 3).toISOString()).toBe("2022-10-19T03:59:59.000Z") }) it("gets the last day of the week when offset to wednesday and the day is monday", () => { - expect(weekEnd("2022-10-10", 3).toISOString()).toBe( - "2022-10-12T03:59:59.000Z" - ) + expect(weekEnd("2022-10-10", 3).toISOString()).toBe("2022-10-12T03:59:59.000Z") + }) + + it("gets the first day of the current week", () => { + const compare = new Date() + compare.setDate(compare.getDate() - compare.getDay() + 6) + compare.setHours(23, 59, 59, 0) + expect(weekEnd()).toEqual(compare) }) }) diff --git a/src/__tests__/weekStart.spec.ts b/src/__tests__/weekStart.spec.ts index 27be5fd..6162bb7 100644 --- a/src/__tests__/weekStart.spec.ts +++ b/src/__tests__/weekStart.spec.ts @@ -4,12 +4,8 @@ process.env.TZ = "America/New_York" describe("weekStart", () => { it("gets the correct first day of the week when the first day is in the middle of the week", () => { - expect(weekStart("2022-12-01").toISOString()).toBe( - "2022-11-27T05:00:00.000Z" - ) - expect(weekStart("2022-03-16").toISOString()).toBe( - "2022-03-13T05:00:00.000Z" - ) + expect(weekStart("2022-12-01").toISOString()).toBe("2022-11-27T05:00:00.000Z") + expect(weekStart("2022-03-16").toISOString()).toBe("2022-03-13T05:00:00.000Z") }) it("gets the first day of the week, when it is the first day of the week", () => { expect(weekStart("2022-05-01 00:00:00").toISOString()).toBe( @@ -24,13 +20,16 @@ describe("weekStart", () => { }) it("gets the first day of the week, when the day is shifted to wednesday and it is tuesday", () => { - expect(weekStart("2022-11-01", 3).toISOString()).toBe( - "2022-10-26T04:00:00.000Z" - ) + expect(weekStart("2022-11-01", 3).toISOString()).toBe("2022-10-26T04:00:00.000Z") }) it("gets the first day of the week, when the day is shifted to wednesday and it is thursday", () => { - expect(weekStart("2022-11-03", 3).toISOString()).toBe( - "2022-11-02T04:00:00.000Z" - ) + expect(weekStart("2022-11-03", 3).toISOString()).toBe("2022-11-02T04:00:00.000Z") + }) + + it("gets the first day of the current week", () => { + const compare = new Date() + compare.setDate(compare.getDate() - compare.getDay()) + compare.setHours(0, 0, 0, 0) + expect(weekStart()).toEqual(compare) }) }) diff --git a/src/__tests__/yearDays.spec.ts b/src/__tests__/yearDays.spec.ts index 7a92a2a..d6cd688 100644 --- a/src/__tests__/yearDays.spec.ts +++ b/src/__tests__/yearDays.spec.ts @@ -1,5 +1,8 @@ import { describe, it, expect } from "vitest" import { yearDays } from "../yearDays" +import { yearEnd } from "../yearEnd" +import { diffDays } from "../diffDays" +import { yearStart } from "../yearStart" process.env.TZ = "America/New_York" describe("yearDays", () => { @@ -9,4 +12,10 @@ describe("yearDays", () => { it("can find the number of days in a year", () => { expect(yearDays("2020-01-01")).toBe(366) }) + + it("can find the number of days of the current day", () => { + const start = yearStart() + const end = yearEnd() + expect(yearDays()).toBe(diffDays(end, start) + 1) + }) }) diff --git a/src/__tests__/yearEnd.spec.ts b/src/__tests__/yearEnd.spec.ts index 5be0c50..33588a2 100644 --- a/src/__tests__/yearEnd.spec.ts +++ b/src/__tests__/yearEnd.spec.ts @@ -3,8 +3,13 @@ import { yearEnd } from "../yearEnd" describe("yearEnd", () => { it("can become the end of the year", () => { - expect(yearEnd("2023-02-22T12:00:00Z").toISOString()).toBe( - "2024-01-01T04:59:59.999Z" - ) + expect(yearEnd("2023-02-22T12:00:00Z").toISOString()).toBe("2024-01-01T04:59:59.999Z") + }) + + it("can give the end of the current year", () => { + const compare = new Date() + compare.setMonth(11, 31) + compare.setHours(23, 59, 59, 999) + expect(yearEnd()).toEqual(compare) }) }) diff --git a/src/__tests__/yearStart.spec.ts b/src/__tests__/yearStart.spec.ts index 3991ab5..981fcb2 100644 --- a/src/__tests__/yearStart.spec.ts +++ b/src/__tests__/yearStart.spec.ts @@ -7,4 +7,11 @@ describe("yearStart", () => { "2023-01-01T05:00:00.000Z" ) }) + + it("can give the end of the current year", () => { + const compare = new Date() + compare.setMonth(0, 1) + compare.setHours(0, 0, 0, 0) + expect(yearStart()).toEqual(compare) + }) }) diff --git a/src/addDay.ts b/src/addDay.ts index 4cd8fc3..fb9dfd6 100644 --- a/src/addDay.ts +++ b/src/addDay.ts @@ -1,11 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a new date object 1/n days after the original one. - * @param inputDate - A date to increment by 1 day. + * @param [inputDate] - A date to increment or null to increment from the current time. + * @param [count] - The quantity to add. */ -export function addDay(inputDate: DateInput, count = 1) { +export function addDay(inputDate?: MaybeDateInput, count = 1) { const d = date(inputDate) d.setDate(d.getDate() + count) return d diff --git a/src/addHour.ts b/src/addHour.ts index 0d2177c..9b72c99 100644 --- a/src/addHour.ts +++ b/src/addHour.ts @@ -1,11 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a new date object 1/n hours after the original one. - * @param inputDate - A date to increment by 1 day. + * @param [inputDate] - A date to increment or null to increment from the current time. + * @param [count] - The quantity to add. */ -export function addHour(inputDate: DateInput, count = 1) { +export function addHour(inputDate?: MaybeDateInput, count = 1) { const d = date(inputDate) d.setHours(d.getHours() + count) return d diff --git a/src/addMinute.ts b/src/addMinute.ts index c5f2d3d..7fc0f49 100644 --- a/src/addMinute.ts +++ b/src/addMinute.ts @@ -1,11 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** - * Returns a new date object 1/n seconds after the original one. - * @param inputDate - A date to increment by 1 day. + * Returns a new date object 1/n minutes after the original one. + * @param [inputDate] - A date to increment or null to increment from the current time. + * @param [count] - The quantity to add. */ -export function addMinute(inputDate: DateInput, count = 1) { +export function addMinute(inputDate?: MaybeDateInput, count = 1) { const d = date(inputDate) d.setMinutes(d.getMinutes() + count) return d diff --git a/src/addMonth.ts b/src/addMonth.ts index 155ed5b..1d6d389 100644 --- a/src/addMonth.ts +++ b/src/addMonth.ts @@ -1,20 +1,16 @@ import { date } from "./date" import { monthDays } from "./monthDays" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Returns a new date object 1/n months after the original one. Keep in mind if you * start with a date late in a given month you could get a date after the next * month. - * @param inputDate - A date to increment by 1 or more months. - * @param count - The quantity to add. - * @param dateOverflow - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. + * @param [inputDate] - A date to increment or null to increment from the current time + * @param [count] - The quantity to add. + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ -export function addMonth( - inputDate: DateInput, - count = 1, - dateOverflow = false -) { +export function addMonth(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { const d = date(inputDate) const dayOfMonth = d.getDate() // If overflowing is disallowed, set the date back to the first of the month diff --git a/src/addSecond.ts b/src/addSecond.ts index 809e4fb..8fd89dc 100644 --- a/src/addSecond.ts +++ b/src/addSecond.ts @@ -1,11 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a new date object 1/n seconds after the original one. - * @param inputDate - A date to increment by 1 day. + * @param [inputDate] - A date to increment or null to increment from the current time. + * @param [count] - The quantity to add. */ -export function addSecond(inputDate: DateInput, count = 1) { +export function addSecond(inputDate?: MaybeDateInput, count = 1) { const d = date(inputDate) d.setSeconds(d.getSeconds() + count) return d diff --git a/src/addYear.ts b/src/addYear.ts index c9c8799..93a04c4 100644 --- a/src/addYear.ts +++ b/src/addYear.ts @@ -1,16 +1,16 @@ import { date } from "./date" import { monthDays } from "./monthDays" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a new date object 1/n years after the original one. Keep in mind if * you start with a date late in a given month you could get a date after the * next month. - * @param inputDate - A date to increment by 1 day. - * @param count - The quantity of years add. - * @param dateOverflow - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. + * @param [inputDate] - A date to increment or null to increment from the current time. + * @param [count] - The quantity of years add. + * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month. */ -export function addYear(inputDate: DateInput, count = 1, dateOverflow = false) { +export function addYear(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) { const d = date(inputDate) const dayOfMonth = d.getDate() // If overflowing is disallowed, set the date back to the first of the month diff --git a/src/applyOffset.ts b/src/applyOffset.ts index 6c5377d..57d8d92 100644 --- a/src/applyOffset.ts +++ b/src/applyOffset.ts @@ -1,14 +1,14 @@ import { date } from "./date" import { TimezoneToken, fixedLengthByOffset, offsetToMins } from "./common" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Apply a given offset to a date, returning a new date with the offset * applied by adding or subtracting the given number of minutes. - * @param dateInput - The date to apply the offset to. - * @param offset - The offset to apply in the +-HHmm or +-HH:mm format. + * @param [dateInput] - The date to apply the offset to. (default: current time) + * @param [offset] - The offset to apply in the +-HHmm or +-HH:mm format. */ -export function applyOffset(dateInput: DateInput, offset = "+00:00"): Date { +export function applyOffset(dateInput?: MaybeDateInput, offset = "+00:00"): Date { const d = date(dateInput) const token = ((): TimezoneToken => { switch (fixedLengthByOffset(offset)) { diff --git a/src/common.ts b/src/common.ts index 27117f9..58ff594 100644 --- a/src/common.ts +++ b/src/common.ts @@ -8,6 +8,7 @@ import type { Part, FilledPart, Format, + MaybeDateInput, } from "./types" /** @@ -128,12 +129,7 @@ export const dayPeriodMap: Map = new Map() /** * An array of all available date styles. */ -export const styles: ReadonlyArray = [ - "full", - "long", - "medium", - "short", -] +export const styles: ReadonlyArray = ["full", "long", "medium", "short"] /** * Creates a leading zero string of 2 digits. @@ -150,9 +146,7 @@ export const four = (n: number) => String(n).padStart(2, "0") * Normalizes a given part to NFKC. * @param part - The part to normalize. */ -export function normStr( - part: Intl.DateTimeFormatPart -): Intl.DateTimeFormatPart { +export function normStr(part: Intl.DateTimeFormatPart): Intl.DateTimeFormatPart { if (part.type === "literal") { part.value = part.value.normalize("NFKC") } @@ -161,14 +155,14 @@ export function normStr( /** * Returns the parts filled with pertinent values. - * @param inputDate - The date to fill parts for + * @param [inputDate] - The date to fill parts for * @param parts - An array of parts to fill * @param locale - The locale to fill with. * @param genitive - Whether to use genitive tokens values or not. * @param offset - The explicit offset to fill with (ignores the date’s true offset). */ export function fill( - inputDate: DateInput, + inputDate: MaybeDateInput, parts: Part[], locale: string, genitive = false, @@ -214,13 +208,13 @@ export function fill( /** * Creates a map of part names to their respective values. - * @param inputDate - The date to format + * @param [inputDate] - The date to format * @param parts - The individual parts the need to be formatted. * @param locale - The locale to format the parts with. * @param genitive - Whether to use genitive tokens values or not. */ function createPartMap( - inputDate: DateInput, + inputDate: MaybeDateInput, parts: Part[], locale: string, genitive = false @@ -272,9 +266,7 @@ function createPartMap( .map(normStr) break } - const genitiveFormattedPart = formattedParts.find( - (p) => p.type === part.partName - ) + const genitiveFormattedPart = formattedParts.find((p) => p.type === part.partName) const index = valueParts.findIndex((p) => p.type === part.partName) if (genitiveFormattedPart && index > -1) { valueParts[index] = genitiveFormattedPart @@ -297,14 +289,8 @@ function createPartMap( * @param timeDiffInMins - The difference in minutes between two timezones. * @returns */ -export function minsToOffset( - timeDiffInMins: number, - token: string = "Z" -): string { - const hours = String(Math.floor(Math.abs(timeDiffInMins / 60))).padStart( - 2, - "0" - ) +export function minsToOffset(timeDiffInMins: number, token: string = "Z"): string { + const hours = String(Math.floor(Math.abs(timeDiffInMins / 60))).padStart(2, "0") const mins = String(Math.abs(timeDiffInMins % 60)).padStart(2, "0") const sign = timeDiffInMins < 0 ? "-" : "+" @@ -322,9 +308,7 @@ export function minsToOffset( */ export function offsetToMins(offset: string, token: TimezoneToken): number { validOffset(offset, token) - const [_, sign, hours, mins] = offset.match( - /([+-])([0-3][0-9]):?([0-6][0-9])/ - )! + const [_, sign, hours, mins] = offset.match(/([+-])([0-3][0-9]):?([0-6][0-9])/)! const offsetInMins = Number(hours) * 60 + Number(mins) return sign === "+" ? offsetInMins : -offsetInMins } @@ -382,19 +366,13 @@ export function validate(parts: Part[]): Part[] | never { if (part.partName === "literal" && !isNaN(parseFloat(part.partValue))) { throw new Error(`Numbers in format (${part.partValue}).`) } - if ( - lastPart && - lastPart.partName !== "literal" && - part.partName !== "literal" - ) { + if (lastPart && lastPart.partName !== "literal" && part.partName !== "literal") { if ( !(lastPart.token in fixedLength) && !(part.token in fixedLength) && !(isNumeric(lastPart) && part.token.toLowerCase() === "a") ) { - throw new Error( - `Illegal adjacent tokens (${lastPart.token}, ${part.token})` - ) + throw new Error(`Illegal adjacent tokens (${lastPart.token}, ${part.token})`) } } lastPart = part diff --git a/src/date.ts b/src/date.ts index a22f8e0..5e93826 100644 --- a/src/date.ts +++ b/src/date.ts @@ -1,5 +1,5 @@ import { iso8601, iso8601Match } from "./iso8601" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Normalizes a "short" date like 2012-01-01 to 2012-01-01T00:00:00 to prevent @@ -18,7 +18,7 @@ function normalize(date: string) { * A date to parse. * @param date - A Date object or an ISO 8601 date. */ -export function date(date?: DateInput): Date { +export function date(date?: MaybeDateInput): Date { if (!date) { date = new Date() } diff --git a/src/dayEnd.ts b/src/dayEnd.ts index a6659d6..1cb9024 100644 --- a/src/dayEnd.ts +++ b/src/dayEnd.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for end of the given day. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current day */ -export function dayEnd(inputDate: DateInput): Date { +export function dayEnd(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setHours(23, 59, 59, 999) return d diff --git a/src/dayOfYear.ts b/src/dayOfYear.ts index 9037ce1..28a6e63 100644 --- a/src/dayOfYear.ts +++ b/src/dayOfYear.ts @@ -1,12 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Gets the what day of the year a given date is. For example, August 1st is * the 213th day of the year on non- years and 214th on leap years. - * @param inputDate - The input date. + * @param [inputDate] - The input date or nothing for the current day. */ -export function dayOfYear(inputDate: DateInput): number { +export function dayOfYear(inputDate?: MaybeDateInput): number { const d = date(inputDate) return Math.round( (new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0).getTime() - diff --git a/src/dayStart.ts b/src/dayStart.ts index 0d8a7cf..da98be8 100644 --- a/src/dayStart.ts +++ b/src/dayStart.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for start of the given day. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current day */ -export function dayStart(inputDate: DateInput): Date { +export function dayStart(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setHours(0, 0, 0) return d diff --git a/src/diffDays.ts b/src/diffDays.ts index deb11de..73669e5 100644 --- a/src/diffDays.ts +++ b/src/diffDays.ts @@ -1,19 +1,37 @@ import { diffMilliseconds } from "./diffMilliseconds" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" import { diffRound, type DiffRoundingMethod } from "./diffRound" /** * Returns the difference between 2 dates in days. - * @param dateA A date to compare with the right date - * @param dateB A date to compare with the left date - * @param roundingMethod the rounding method to use, default: trunc + * @param dateA - A date to compare with the right date + * @param [dateB] - A date to compare with the left date or nothing to compare with the current time + * @param [roundingMethod] - the rounding method to use, default: trunc */ export function diffDays( dateA: DateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number +/** + * Returns the difference between 2 dates in days. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A date to compare with the left date + * @param [roundingMethod] - the rounding method to use, default: trunc + */ +export function diffDays( + dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod -) { +): number + +export function diffDays( + dateA: MaybeDateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number { return diffRound( + // @ts-ignore diffMilliseconds(dateA, dateB) / 86_400_000, // hour * 24 roundingMethod ) diff --git a/src/diffHours.ts b/src/diffHours.ts index d935224..5f9bca3 100644 --- a/src/diffHours.ts +++ b/src/diffHours.ts @@ -1,19 +1,38 @@ import { diffMilliseconds } from "./diffMilliseconds" import { diffRound, type DiffRoundingMethod } from "./diffRound" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Returns the difference between 2 dates in hours. - * @param dateA A date to compare with the right date - * @param dateB A date to compare with the left date - * @param roundingMethod the rounding method to use, default: trunc + * @param dateA - A date to compare with the right date + * @param [dateB] - A date to compare with the left date or nothing to compare with the current time + * @param [roundingMethod] - the rounding method to use, default: trunc */ export function diffHours( dateA: DateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number + +/** + * Returns the difference between 2 dates in hours. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A date to compare with the left date + * @param [roundingMethod] - the rounding method to use, default: trunc + */ +export function diffHours( + dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod -) { +): number + +export function diffHours( + dateA: MaybeDateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number { return diffRound( + //@ts-ignore diffMilliseconds(dateA, dateB) / 3_600_000, // 1000 * 60 * 60 roundingMethod ) diff --git a/src/diffMilliseconds.ts b/src/diffMilliseconds.ts index 2e1e780..faa4c88 100644 --- a/src/diffMilliseconds.ts +++ b/src/diffMilliseconds.ts @@ -1,12 +1,21 @@ import { date } from "./date" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Returns the difference between 2 dates in milliseconds. - * @param dateA A date to compare with the right date - * @param dateB A date to compare with the left date + * @param dateA - A date to compare with the right date + * @param [dateB] - A date to compare with the left date or nothing to compare with the current time */ -export function diffMilliseconds(dateA: DateInput, dateB: DateInput) { +export function diffMilliseconds(dateA: DateInput, dateB?: MaybeDateInput): number + +/** + * Returns the difference between 2 dates in milliseconds. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A date to compare with the left date + */ +export function diffMilliseconds(dateA: MaybeDateInput, dateB: DateInput): number + +export function diffMilliseconds(dateA: MaybeDateInput, dateB?: MaybeDateInput): number { const left = date(dateA) const right = date(dateB) return +left - +right diff --git a/src/diffMinutes.ts b/src/diffMinutes.ts index da0de7d..1fbabf4 100644 --- a/src/diffMinutes.ts +++ b/src/diffMinutes.ts @@ -1,16 +1,39 @@ -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" import { diffMilliseconds } from "./diffMilliseconds" import { diffRound, type DiffRoundingMethod } from "./diffRound" /** * Returns the difference between 2 dates in minutes. - * @param dateA A date to compare with the right date - * @param roundingMethod the rounding method to use, default: trunc + * @param dateA - A date to compare with the right date + * @param [dateB] - A Date to compare with the left date or nothing to compare with the current time + * @param [roundingMethod] the rounding method to use, default: trunc + */ +export function diffMinutes( + dateA: DateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number + +/** + * Returns the difference between 2 dates in minutes. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A Date to compare with the left date + * @param [roundingMethod] the rounding method to use, default: trunc */ export function diffMinutes( dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod -) { - return diffRound(diffMilliseconds(dateA, dateB) / 60_000, roundingMethod) +): number + +export function diffMinutes( + dateA: MaybeDateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number { + return diffRound( + //@ts-ignore + diffMilliseconds(dateA, dateB) / 60_000, + roundingMethod + ) } diff --git a/src/diffMonths.ts b/src/diffMonths.ts index 960752e..fdb54e1 100644 --- a/src/diffMonths.ts +++ b/src/diffMonths.ts @@ -1,13 +1,21 @@ import { date } from "./date" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" import { monthDays } from "./monthDays" /** * Returns the difference between 2 dates in months. - * @param dateA A date to compare with the dateB date - * @param dateB A date to compare with the dateA date + * @param dateA - A date to compare with the dateB date + * @param [dateB] - A date to compare with the dateA date or nothing to compare with the current time */ -export function diffMonths(dateA: DateInput, dateB: DateInput): number { +export function diffMonths(dateA: DateInput, dateB?: MaybeDateInput): number +/** + * Returns the difference between 2 dates in months. + * @param [dateA] - A date to compare with the dateB date or null to compare with the current time + * @param dateB - A date to compare with the dateA date + */ +export function diffMonths(dateA: MaybeDateInput, dateB: DateInput): number + +export function diffMonths(dateA: MaybeDateInput, dateB?: MaybeDateInput): number { const l = date(dateA) const r = date(dateB) // if the dateB one is bigger, we switch them around as it's easier to do @@ -17,8 +25,7 @@ export function diffMonths(dateA: DateInput, dateB: DateInput): number { } // we first get the amount of calendar months - let months = - (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth()) + let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth()) const ld = l.getDate() const rd = r.getDate() diff --git a/src/diffSeconds.ts b/src/diffSeconds.ts index 8ad6e56..0be157d 100644 --- a/src/diffSeconds.ts +++ b/src/diffSeconds.ts @@ -1,17 +1,39 @@ import { diffMilliseconds } from "./diffMilliseconds" import { DiffRoundingMethod, diffRound } from "./diffRound" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Returns the difference between 2 dates in seconds. - * @param dateA A date to compare with the right date - * @param dateB A date to compare with the left date - * @param roundingMethod the rounding method to use, default: trunc + * @param dateA - A date to compare with the right date + * @param [dateB] - A date to compare with the left date or nothing to compare with the current time. + * @param [roundingMethod] - the rounding method to use, default: trunc */ export function diffSeconds( dateA: DateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number + +/** + * Returns the difference between 2 dates in seconds. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A date to compare with the left date + * @param [roundingMethod] - the rounding method to use, default: trunc + */ +export function diffSeconds( + dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod -) { - return diffRound(diffMilliseconds(dateA, dateB) / 1000, roundingMethod) +): number + +export function diffSeconds( + dateA: MaybeDateInput, + dateB?: MaybeDateInput, + roundingMethod?: DiffRoundingMethod +): number { + return diffRound( + // @ts-ignore + diffMilliseconds(dateA, dateB) / 1000, + roundingMethod + ) } diff --git a/src/diffWeeks.ts b/src/diffWeeks.ts index 0eb9ea7..4e1b90b 100644 --- a/src/diffWeeks.ts +++ b/src/diffWeeks.ts @@ -1,18 +1,36 @@ import { diffMilliseconds } from "./diffMilliseconds" -import { DateInput } from "./types" +import { DateInput, MaybeDateInput } from "./types" import { diffRound, type DiffRoundingMethod } from "./diffRound" /** * Returns the difference between 2 dates in days. - * @param dateA A date to compare with the right date - * @param dateB A date to compare with the left date - * @param roundingMethod the rounding method to use, default: trunc + * @param dateA - A date to compare with the right date + * @param [dateB] - A date to compare with the left date or nothing to compare with the current time + * @param [roundingMethod] - the rounding method to use, default: trunc */ export function diffWeeks( dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod -) { +): number + +/** + * Returns the difference between 2 dates in days. + * @param [dateA] - A date to compare with the right date or null to compare with the current time + * @param dateB - A date to compare with the left date + * @param [roundingMethod] - the rounding method to use, default: trunc + */ +export function diffWeeks( + dateA: DateInput, + dateB: DateInput, + roundingMethod?: DiffRoundingMethod +): number + +export function diffWeeks( + dateA: MaybeDateInput, + dateB: DateInput, + roundingMethod?: DiffRoundingMethod +): number { return diffRound( diffMilliseconds(dateA, dateB) / 604800000, // day * 7 roundingMethod diff --git a/src/diffYears.ts b/src/diffYears.ts index 836b830..cf85af0 100644 --- a/src/diffYears.ts +++ b/src/diffYears.ts @@ -1,13 +1,24 @@ import { diffMonths } from "./diffMonths" -import { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Returns the difference between 2 dates in years. - * @param dateA A date to compare with the dateB date - * @param dateB A date to compare with the dateA date + * @param dateA - A date to compare with the dateB date + * @param [dateB] - A date to compare with the dateA date or nothing to compare with the current time */ -export function diffYears(dateA: DateInput, dateB: DateInput): number { - const r = Math.trunc(diffMonths(dateA, dateB) / 12) +export function diffYears(dateA: DateInput, dateB?: MaybeDateInput): number + +/** + * Returns the difference between 2 dates in years. + * @param [dateA] - A date to compare with the dateB date or null to compare with the current time + * @param dateB - A date to compare with the dateA date + */ +export function diffYears(dateA: MaybeDateInput, dateB: DateInput): number +export function diffYears(dateA: MaybeDateInput, dateB?: MaybeDateInput): number { + const r = Math.trunc( + //@ts-ignore + diffMonths(dateA, dateB) / 12 + ) //ensures we don't give back -0 return r == 0 ? 0 : r } diff --git a/src/hourEnd.ts b/src/hourEnd.ts index 6086560..5659667 100644 --- a/src/hourEnd.ts +++ b/src/hourEnd.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for end of the given hour. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current time */ -export function hourEnd(inputDate: DateInput): Date { +export function hourEnd(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setMinutes(59, 59, 999) return d diff --git a/src/hourStart.ts b/src/hourStart.ts index 0f81b14..9d46a19 100644 --- a/src/hourStart.ts +++ b/src/hourStart.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for start of the given hour. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current time */ -export function hourStart(inputDate: DateInput): Date { +export function hourStart(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setMinutes(0, 0) return d diff --git a/src/index.ts b/src/index.ts index eaa33e8..3854023 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,6 +42,8 @@ export { yearEnd } from "./yearEnd" export { isBefore } from "./isBefore" export { isAfter } from "./isAfter" export { isEqual } from "./isEqual" +export { isPast } from "./isPast" +export { isFuture } from "./isFuture" export * from "./types" export { diffMilliseconds } from "./diffMilliseconds" export { diffSeconds } from "./diffSeconds" diff --git a/src/isAfter.ts b/src/isAfter.ts index 214399f..910e346 100644 --- a/src/isAfter.ts +++ b/src/isAfter.ts @@ -1,20 +1,20 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * @name isAfter * @category Common Helpers - * @summary Is the first date after the second one? + * @summary Is the first date after the second one or the current time? * * @description - * Is the first date after the second one? + * Is the first date after the second one or the current time? * * @param inputDate - The date that should be after the other one to return true - * @param dateToCompare - The date to compare with + * @param [dateToCompare] - The date to compare with or the current time if nothing given * - * @returns The first date is after the second date. + * @returns The first date is after the second date or the current time. */ -export function isAfter(inputDate: DateInput, dateToCompare: DateInput) { +export function isAfter(inputDate: DateInput, dateToCompare?: MaybeDateInput) { const _date = date(inputDate) const _dateToCompare = date(dateToCompare) diff --git a/src/isBefore.ts b/src/isBefore.ts index e2f4763..98d422b 100644 --- a/src/isBefore.ts +++ b/src/isBefore.ts @@ -1,15 +1,15 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** - * Is the first date before the second one? + * Is the first date before the second one or the current time? * * @param inputDate - The date that should be before the other one to return true - * @param dateToCompare - The date to compare with + * @param [dateToCompare] - The date to compare with or the current time if nothing given * - * @returns The first date is before the second date. + * @returns The first date is before the second date or the current time. */ -export function isBefore(inputDate: DateInput, dateToCompare: DateInput) { +export function isBefore(inputDate: DateInput, dateToCompare?: MaybeDateInput): boolean { const _date = date(inputDate) const _dateToCompare = date(dateToCompare) diff --git a/src/isEqual.ts b/src/isEqual.ts index db01b4f..1998ff3 100644 --- a/src/isEqual.ts +++ b/src/isEqual.ts @@ -1,15 +1,26 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** - * Are the given dates equal? + * Are the given dates equal or given date to the current time? * * @param dateLeft - The first date to compare + * @param [dateRight] - The second date to compare or the current time of nothing given + * + * @returns The dates are equal or date to the current time + */ +export function isEqual(dateLeft: DateInput, dateRight?: MaybeDateInput): boolean +/** + * Are the given dates equal or given date to the current time? + * + * @param [dateLeft] - The first date to compare or the current time if null given * @param dateRight - The second date to compare * - * @returns The dates are equal. + * @returns The dates are equal or date to the current time */ -export function isEqual(dateLeft: DateInput, dateRight: DateInput) { + +export function isEqual(dateLeft: MaybeDateInput, dateRight: DateInput): boolean +export function isEqual(dateLeft: MaybeDateInput, dateRight?: MaybeDateInput): boolean { const _dateLeft = date(dateLeft) const _dateRight = date(dateRight) diff --git a/src/isFuture.ts b/src/isFuture.ts new file mode 100644 index 0000000..19705f7 --- /dev/null +++ b/src/isFuture.ts @@ -0,0 +1,11 @@ +import { isAfter } from "./isAfter" +import { DateInput } from "./types" + +/** + * is the date in the future compared to the current time + * @param inputDate - The date that should be in the future + * @returns the given date is in the future compared to the current time + */ +export function isFuture(inputDate: DateInput): boolean { + return isAfter(inputDate) +} diff --git a/src/isPast.ts b/src/isPast.ts new file mode 100644 index 0000000..42e0073 --- /dev/null +++ b/src/isPast.ts @@ -0,0 +1,11 @@ +import { isBefore } from "./isBefore" +import { DateInput } from "./types" + +/** + * is the date in the past compared to the current time + * @param inputDate - The date that should be in the past + * @returns the given date is in the past compared to the current time + */ +export function isPast(inputDate: DateInput): boolean { + return isBefore(inputDate) +} diff --git a/src/minuteEnd.ts b/src/minuteEnd.ts index f8668db..3cd5db0 100644 --- a/src/minuteEnd.ts +++ b/src/minuteEnd.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for end of the given minute. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current time */ -export function minuteEnd(inputDate: DateInput): Date { +export function minuteEnd(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setSeconds(59, 999) return d diff --git a/src/minuteStart.ts b/src/minuteStart.ts index eeaf241..5da972c 100644 --- a/src/minuteStart.ts +++ b/src/minuteStart.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for start of the given minute. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current time */ -export function minuteStart(inputDate: DateInput): Date { +export function minuteStart(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setSeconds(0) return d diff --git a/src/monthDays.ts b/src/monthDays.ts index 1fd612f..3809ec9 100644 --- a/src/monthDays.ts +++ b/src/monthDays.ts @@ -1,11 +1,11 @@ import { monthEnd } from "./monthEnd" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns the total number of days from a given month. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current month */ -export function monthDays(inputDate: DateInput): number { +export function monthDays(inputDate?: MaybeDateInput): number { const d = monthEnd(inputDate) return d.getDate() } diff --git a/src/monthEnd.ts b/src/monthEnd.ts index f801c12..1a86f9e 100644 --- a/src/monthEnd.ts +++ b/src/monthEnd.ts @@ -1,12 +1,12 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for the with the input date set to the last day of * the current month. Does not change the time. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current month */ -export function monthEnd(inputDate: DateInput): Date { +export function monthEnd(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setDate(1) d.setMonth(d.getMonth() + 1) diff --git a/src/monthStart.ts b/src/monthStart.ts index 3cbace2..46d9fd7 100644 --- a/src/monthStart.ts +++ b/src/monthStart.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for the first day of a month. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current time */ -export function monthStart(inputDate: DateInput): Date { +export function monthStart(inputDate?: MaybeDateInput): Date { const d = date(inputDate) d.setDate(1) d.setHours(0, 0, 0) diff --git a/src/nearestDay.ts b/src/nearestDay.ts index e0c9e4a..e7db1d4 100644 --- a/src/nearestDay.ts +++ b/src/nearestDay.ts @@ -3,17 +3,17 @@ import { monthDays } from "./monthDays" import { yearDays } from "./yearDays" import { dayOfYear } from "./dayOfYear" import { addDay } from "./addDay" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Performs a bidirectional search for the nearest date that passes a function. - * @param target - Performs a search for the nearest passing date. + * @param [inputDate] - Performs a search for the nearest passing date. * @param search - The search function to use, given a date returns a boolean. * @param constraint - The number of iterations to perform before giving up, or logical constraint like "month", or "week". * */ export function nearestDay( - inputDate: DateInput, + inputDate: MaybeDateInput, search: (date: Date) => boolean, constraint: number | "month" | "week" | "year" = 7 ): Date | null { diff --git a/src/offset.ts b/src/offset.ts index 1f2f484..6dfbed2 100644 --- a/src/offset.ts +++ b/src/offset.ts @@ -1,7 +1,7 @@ import { date } from "./date" import { normStr, minsToOffset, TimezoneToken } from "./common" import { deviceTZ } from "./deviceTZ" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Converts a date object from one timezone to that same time in UTC. This is @@ -42,12 +42,12 @@ function relativeTime(d: Date, timeZone: string): Date { * Returns the offset between two timezones on a given date. The results are * ISO8601 compatible offsets like -0800 or +0530. * - * @param dateInput - The date on which to determine the offset. - * @param tzA - (default: UTC) The second timezone to compare determine the offset between. - * @param tzB - (default: device) The first timezone to compare determine the offset between. + * @param [dateInput] - (default: current time) The date on which to determine the offset + * @param [tzA] - (default: UTC) The second timezone to compare determine the offset between. + * @param [tzB] - (default: device) The first timezone to compare determine the offset between. */ export function offset( - utcTime: DateInput, + utcTime?: MaybeDateInput, tzA = "UTC", tzB = "device", timeZoneToken: TimezoneToken = "Z" @@ -56,8 +56,6 @@ export function offset( const d = date(utcTime) const timeA = relativeTime(d, tzA) const timeB = relativeTime(d, tzB) - const timeDiffInMins = Math.round( - (timeB.getTime() - timeA.getTime()) / 1000 / 60 - ) + const timeDiffInMins = Math.round((timeB.getTime() - timeA.getTime()) / 1000 / 60) return minsToOffset(timeDiffInMins, timeZoneToken) } diff --git a/src/removeOffset.ts b/src/removeOffset.ts index bf221ba..e2c6dd6 100644 --- a/src/removeOffset.ts +++ b/src/removeOffset.ts @@ -1,12 +1,12 @@ import { applyOffset } from "./applyOffset" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Inverts the offset and applies it to the given date, returning a new date. - * @param dateInput - The date to remove the offset from. - * @param offset - The offset to remove in the +-HHmm or +-HH:mm format. + * @param [dateInput] - The date to remove the offset from. (default: current time) + * @param [offset] - The offset to remove in the +-HHmm or +-HH:mm format. */ -export function removeOffset(dateInput: DateInput, offset = "+00:00"): Date { +export function removeOffset(dateInput?: MaybeDateInput, offset = "+00:00"): Date { const positive = offset.slice(0, 1) === "+" return applyOffset( dateInput, diff --git a/src/sameDay.ts b/src/sameDay.ts index 446417d..a26a0ad 100644 --- a/src/sameDay.ts +++ b/src/sameDay.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same date. Ignores time. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if nothing given + */ +export function sameDay(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same date. Ignores time. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameDay(inputDateA: DateInput, inputDateB: DateInput) { +export function sameDay(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameDay( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return ( diff --git a/src/sameHour.ts b/src/sameHour.ts index f57ab3f..530ccf6 100644 --- a/src/sameHour.ts +++ b/src/sameHour.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same time hour. Ignores date. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if nothing given + */ +export function sameHour(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same time hour. Ignores date. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameHour(inputDateA: DateInput, inputDateB: DateInput) { +export function sameHour(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameHour( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return a.getHours() === b.getHours() diff --git a/src/sameMinute.ts b/src/sameMinute.ts index 556f33e..65f4cee 100644 --- a/src/sameMinute.ts +++ b/src/sameMinute.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same time minutes. Ignores date. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if nothing given + */ +export function sameMinute(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same time minutes. Ignores date. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameMinute(inputDateA: DateInput, inputDateB: DateInput) { +export function sameMinute(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameMinute( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return a.getMinutes() === b.getMinutes() diff --git a/src/sameMonth.ts b/src/sameMonth.ts index 89ae7c0..9b9cc0f 100644 --- a/src/sameMonth.ts +++ b/src/sameMonth.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same time minutes. Ignores date. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if nothing given + */ +export function sameMonth(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same time minutes. Ignores date. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameMonth(inputDateA: DateInput, inputDateB: DateInput) { +export function sameMonth(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameMonth( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return a.getMonth() === b.getMonth() diff --git a/src/sameSecond.ts b/src/sameSecond.ts index 9238dc9..16c4e5a 100644 --- a/src/sameSecond.ts +++ b/src/sameSecond.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same time seconds. Ignores date. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if nothing given + */ +export function sameSecond(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same time seconds. Ignores date. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameSecond(inputDateA: DateInput, inputDateB: DateInput) { +export function sameSecond(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameSecond( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return a.getSeconds() === b.getSeconds() diff --git a/src/sameYear.ts b/src/sameYear.ts index 3a26aef..6cddd91 100644 --- a/src/sameYear.ts +++ b/src/sameYear.ts @@ -1,12 +1,22 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { DateInput, MaybeDateInput } from "./types" /** * Checks if two date objects refer to the same year. * @param inputDateA - First date to compare + * @param [inputDateB] - Second date to compare or the current time if null given + */ +export function sameYear(inputDateA: DateInput, inputDateB?: MaybeDateInput): boolean +/** + * Checks if two date objects refer to the same year. + * @param [inputDateA] - First date to compare or the current time if null given * @param inputDateB - Second date to compare */ -export function sameYear(inputDateA: DateInput, inputDateB: DateInput) { +export function sameYear(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean +export function sameYear( + inputDateA: MaybeDateInput, + inputDateB?: MaybeDateInput +): boolean { const a = date(inputDateA) const b = date(inputDateB) return a.getFullYear() === b.getFullYear() diff --git a/src/types.ts b/src/types.ts index b15f390..293b674 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,11 @@ */ export type DateInput = Date | string +/** + * The date format used as a maybe input value. Either a date, ISO8601 string or null for current time + */ +export type MaybeDateInput = DateInput | null + /** * Format parts with text names use these descriptors: */ diff --git a/src/tzDate.ts b/src/tzDate.ts index 02bb490..af70d4f 100644 --- a/src/tzDate.ts +++ b/src/tzDate.ts @@ -1,7 +1,7 @@ import { offset } from "./offset" import { applyOffset } from "./applyOffset" import { date } from "./date" -import { DateInput } from "./types" +import { DateInput, MaybeDateInput } from "./types" /** * Creates a date object for the input date at the given timezone. For example @@ -10,10 +10,12 @@ import { DateInput } from "./types" * * If given a Date object it will use local time and convert it to the given * timezone, thus "changing" the date. - * @param inputDate - An iso8601 date string with no timezone + * + * if given no string or date object, it'll use the current locale time and convert to the given timezone + * @param [inputDate] - An iso8601 date string with no timezone * @param tz - A timezone string */ -export function tzDate(inputDate: DateInput, tz: string) { +export function tzDate(inputDate: MaybeDateInput, tz: string) { const d = date(inputDate) return applyOffset(d, offset(d, tz)) } diff --git a/src/weekEnd.ts b/src/weekEnd.ts index df5bdd1..02b81f4 100644 --- a/src/weekEnd.ts +++ b/src/weekEnd.ts @@ -1,14 +1,14 @@ import { weekStart } from "./weekStart" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for the last day at the last second of the given week. * Defaults to Sunday as the first day of the week: * 0 = Sunday ... 6 = Saturday - * @param inputDate - Gets the last day of the week - * @param startOfWeekDay - The first day of the week + * @param [inputDate] - Gets the last day of the week + * @param [startOfWeekDay] - The first day of the week */ -export function weekEnd(inputDate: DateInput, startOfWeekDay = 0): Date { +export function weekEnd(inputDate?: MaybeDateInput, startOfWeekDay = 0): Date { const d = weekStart(inputDate, startOfWeekDay) d.setDate(d.getDate() + 6) d.setHours(23, 59, 59) diff --git a/src/weekStart.ts b/src/weekStart.ts index 8692c49..2b47d36 100644 --- a/src/weekStart.ts +++ b/src/weekStart.ts @@ -1,14 +1,14 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for start of the given week. Defaults to Sunday as the * first day of the week: * 0 = Sunday ... 6 = Saturday - * @param inputDate - A string or Date object - * @param startOfWeekDay - Determines which day of the week is the first + * @param [inputDate] - A string, Date object or nothing for current week + * @param [startOfWeekDay] - Determines which day of the week is the first */ -export function weekStart(inputDate: DateInput, startOfWeekDay = 0): Date { +export function weekStart(inputDate?: MaybeDateInput, startOfWeekDay = 0): Date { const d = date(inputDate) let diff = startOfWeekDay - d.getDay() if (diff > 0) diff = diff - 7 diff --git a/src/yearDays.ts b/src/yearDays.ts index 0e13365..45fb34d 100644 --- a/src/yearDays.ts +++ b/src/yearDays.ts @@ -1,11 +1,11 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Get the number of days in the given date’s year. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current year */ -export function yearDays(inputDate: DateInput): number { +export function yearDays(inputDate?: MaybeDateInput): number { const d = date(inputDate) return ( (new Date(d.getFullYear() + 1, 0, 0).getTime() - diff --git a/src/yearEnd.ts b/src/yearEnd.ts index 987abeb..37ace9c 100644 --- a/src/yearEnd.ts +++ b/src/yearEnd.ts @@ -1,16 +1,15 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for the with the input date set to the end of the current year. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current year */ -export function yearEnd(inputDate: DateInput): Date { - const d = date(inputDate); +export function yearEnd(inputDate?: MaybeDateInput): Date { + const d = date(inputDate) - d.setMonth(11); - d.setDate(31); - d.setHours(23, 59, 59, 999); + d.setMonth(11, 31) + d.setHours(23, 59, 59, 999) - return d; + return d } diff --git a/src/yearStart.ts b/src/yearStart.ts index c74b98c..0cdd42a 100644 --- a/src/yearStart.ts +++ b/src/yearStart.ts @@ -1,15 +1,14 @@ import { date } from "./date" -import type { DateInput } from "./types" +import type { MaybeDateInput } from "./types" /** * Returns a Date object for the with the input date set to the start of the current year. - * @param inputDate - A string or Date object + * @param [inputDate] - A string, Date object or nothing for the current year */ -export function yearStart(inputDate: DateInput): Date { +export function yearStart(inputDate?: MaybeDateInput): Date { const d = date(inputDate) - d.setMonth(0) - d.setDate(1) + d.setMonth(0, 1) d.setHours(0, 0, 0) return d