Skip to content

Commit a748d5b

Browse files
authored
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`
1 parent 2afcef7 commit a748d5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+694
-303
lines changed

.prettierrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"tabWidth": 2,
33
"useTabs": false,
4-
"semi": false
4+
"semi": false,
5+
"printWidth": 90,
6+
"trailingComma": "es5"
57
}

src/__tests__/addDay.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ describe("addDay", () => {
1414
it("gets the next day by providing specified negative number of days", () => {
1515
expect(addDay("2022-01-01", -5).toISOString()).toBe("2021-12-27T05:00:00.000Z")
1616
})
17+
18+
// test with the current time is at diffDays
1719
})

src/__tests__/addHour.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ process.env.TZ = "America/New_York"
44

55
describe("addHour", () => {
66
it("can increment a normal hour", () => {
7-
expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe(
8-
"2022-01-01T01:00:00.000Z"
9-
)
7+
expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe("2022-01-01T01:00:00.000Z")
108
})
119
it("can increment the last hours of the day into a new day", () => {
1210
expect(addHour("2022-01-01T23:11:00Z", 3).toISOString()).toBe(
@@ -18,4 +16,6 @@ describe("addHour", () => {
1816
"2021-12-31T19:11:00.000Z"
1917
)
2018
})
19+
20+
// test with the current time is at diffHours.
2121
})

src/__tests__/addMinute.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ describe("addMinute", () => {
1818
"2021-12-31T23:10:00.000Z"
1919
)
2020
})
21+
22+
// test with the current time is at diffMinutes.
2123
})

src/__tests__/addMonth.spec.ts

+8-18
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,35 @@ process.env.TZ = "America/New_York"
44

55
describe("addMonth", () => {
66
it("gets the next month on the first", () => {
7-
expect(addMonth("2022-01-01").toISOString()).toBe(
8-
"2022-02-01T05:00:00.000Z"
9-
)
7+
expect(addMonth("2022-01-01").toISOString()).toBe("2022-02-01T05:00:00.000Z")
108
})
119
it("can overflow a month month when the next month has fewer days", () => {
12-
expect(addMonth("2000-01-31", 1, true).toISOString()).toBe(
13-
"2000-03-02T05:00:00.000Z"
14-
)
10+
expect(addMonth("2000-01-31", 1, true).toISOString()).toBe("2000-03-02T05:00:00.000Z")
1511
})
1612
it("goe to the same day of the month on the next month", () => {
17-
expect(addMonth("2000-06-04").toISOString()).toBe(
18-
"2000-07-04T04:00:00.000Z"
19-
)
13+
expect(addMonth("2000-06-04").toISOString()).toBe("2000-07-04T04:00:00.000Z")
2014
})
2115

2216
it("can add multiple months by passing a second argument", () => {
23-
expect(addMonth("2000-01-01", 2).toISOString()).toBe(
24-
"2000-03-01T05:00:00.000Z"
25-
)
17+
expect(addMonth("2000-01-01", 2).toISOString()).toBe("2000-03-01T05:00:00.000Z")
2618
})
2719

2820
it("can add years months by passing a second argument", () => {
29-
expect(addMonth("2000-01-01", 25).toISOString()).toBe(
30-
"2002-02-01T05:00:00.000Z"
31-
)
21+
expect(addMonth("2000-01-01", 25).toISOString()).toBe("2002-02-01T05:00:00.000Z")
3222
})
3323
it("can prevent month overflow with third argument", () => {
3424
expect(addMonth("2020-01-31", 1, false).toISOString()).toBe(
3525
"2020-02-29T05:00:00.000Z"
3626
)
3727
})
3828
it("can subtract multiple months", () => {
39-
expect(addMonth("2020-01-31", -2).toISOString()).toBe(
40-
"2019-11-30T05:00:00.000Z"
41-
)
29+
expect(addMonth("2020-01-31", -2).toISOString()).toBe("2019-11-30T05:00:00.000Z")
4230
})
4331
it("can subtract multiple months and allow overflow", () => {
4432
expect(addMonth("2020-01-31", -2, true).toISOString()).toBe(
4533
"2019-12-01T05:00:00.000Z"
4634
)
4735
})
36+
37+
// test with the current time is at diffMonths.
4838
})

src/__tests__/addSecond.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ describe("addSecond", () => {
1818
"2021-12-31T23:59:30.000Z"
1919
)
2020
})
21+
22+
// test with the current time is at diffSeconds.
2123
})

src/__tests__/addYear.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ describe("addYear", () => {
1616
it("can overflow the day of the month on leap year", () => {
1717
expect(addYear("2000-02-29").toISOString()).toBe("2001-02-28T05:00:00.000Z")
1818
})
19+
20+
// test with the current time is at diffYears
1921
})

src/__tests__/dayEnd.spec.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ process.env.TZ = "America/New_York"
44

55
describe("dayEnd", () => {
66
it("can become the end of the day", () => {
7-
expect(dayEnd("2023-02-22T12:00:00Z").toISOString()).toBe(
8-
"2023-02-23T04:59:59.999Z"
9-
)
7+
expect(dayEnd("2023-02-22T12:00:00Z").toISOString()).toBe("2023-02-23T04:59:59.999Z")
8+
})
9+
it("can become the end of the current day", () => {
10+
const compare = new Date()
11+
compare.setHours(23, 59, 59, 999)
12+
expect(dayEnd()).toEqual(compare)
1013
})
1114
})

src/__tests__/dayOfYear.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, it, expect } from "vitest"
22
import { dayOfYear } from "../dayOfYear"
3+
import { yearStart } from "../yearStart"
4+
import { diffDays } from "../diffDays"
35
process.env.TZ = "America/New_York"
46

57
describe("dayOfYear", () => {
@@ -9,4 +11,9 @@ describe("dayOfYear", () => {
911
it("can find the number of days in a year", () => {
1012
expect(dayOfYear("2020-08-01")).toBe(214)
1113
})
14+
15+
it("can find the number of days of the current day", () => {
16+
const start = yearStart()
17+
expect(dayOfYear()).toBe(diffDays(null, start) + 1)
18+
})
1219
})

src/__tests__/dayStart.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ describe("dayStart", () => {
88
"2023-02-22T05:00:00.000Z"
99
)
1010
})
11+
12+
it("gets the start of the day", () => {
13+
const compare = new Date()
14+
compare.setHours(0, 0, 0, 0)
15+
expect(dayStart()).toEqual(compare)
16+
})
1117
})

src/__tests__/diffDays.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { describe, expect, it } from "vitest"
22
import { diffDays } from "../diffDays"
3+
import { addDay } from "../addDay"
34

45
describe("differenceInDays", () => {
56
it("difference is 3 days", () => {
67
expect(diffDays("2024-04-10", "2024-04-07")).toBe(3)
78
})
89

910
it("difference is 2 days", () => {
10-
expect(
11-
diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z")
12-
).toBe(2)
11+
expect(diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z")).toBe(2)
1312
})
1413

1514
it("difference is 3 days by using round", () => {
1615
expect(
1716
diffDays("2024-04-10T09:50:00.000Z", "2024-04-07T15:28:00.000Z", "round")
1817
).toBe(3)
1918
})
19+
20+
it("different should be -64 hours compared to the current time", () => {
21+
const compare = addDay(null, -28)
22+
expect(diffDays(compare)).toBe(-28)
23+
})
2024
})

src/__tests__/diffHours.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { describe, expect, it } from "vitest"
22
import { diffHours } from "../diffHours"
3+
import { addHour } from "../addHour"
34

45
describe("differenceInHours", () => {
56
it("difference is 5 hours", () => {
6-
expect(
7-
diffHours("2024-04-07T15:28:00.000Z", "2024-04-07T09:50:00.000Z")
8-
).toBe(5)
7+
expect(diffHours("2024-04-07T15:28:00.000Z", "2024-04-07T09:50:00.000Z")).toBe(5)
8+
})
9+
10+
it("different should be -64 hours compared to the current time", () => {
11+
const compare = addHour(null, 64)
12+
expect(diffHours(null, compare)).toBe(-64)
913
})
1014
})
+16-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import { describe, it, expect } from "vitest"
22
import { diffMilliseconds } from "../diffMilliseconds"
33

4-
describe("differenceInMilliseconds", () => {
4+
describe("diffMilliseconds", () => {
55
it("difference is 257 milliseconds", () => {
6-
expect(
7-
diffMilliseconds("2024-04-07T09:10:48.257Z", "2024-04-07T09:10:48.000Z")
8-
).toBe(257)
6+
expect(diffMilliseconds("2024-04-07T09:10:48.257Z", "2024-04-07T09:10:48.000Z")).toBe(
7+
257
8+
)
9+
})
10+
11+
it("should be 5000 milleseconds difference compared to the current time", () => {
12+
const now = new Date()
13+
now.setMilliseconds(5000) // because the date function sets ms to 0, the test needs to test with increments of 1000
14+
expect(diffMilliseconds(now)).toBe(5000)
15+
})
16+
17+
it("should be -5000 milleseconds difference compared to the current time", () => {
18+
const now = new Date()
19+
now.setMilliseconds(5000)
20+
expect(diffMilliseconds(null, now)).toBe(-5000)
921
})
1022
})

src/__tests__/diffMinutes.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { describe, it, expect } from "vitest"
22
import { diffMinutes } from "../diffMinutes"
3+
import { addMinute } from "../addMinute"
34

45
describe("differenceInMinutes", () => {
56
it("difference is 18 minutes", () => {
6-
expect(
7-
diffMinutes("2024-04-07T09:28:30.050Z", "2024-04-07T09:10:00.000Z")
8-
).toBe(18)
7+
expect(diffMinutes("2024-04-07T09:28:30.050Z", "2024-04-07T09:10:00.000Z")).toBe(18)
98
})
109
it("difference is 19 minutes by using ceil", () => {
1110
expect(
12-
diffMinutes(
13-
"2024-04-07T09:28:01.050Z",
14-
"2024-04-07T09:10:00.000Z",
15-
"ceil"
16-
)
11+
diffMinutes("2024-04-07T09:28:01.050Z", "2024-04-07T09:10:00.000Z", "ceil")
1712
).toBe(19)
1813
})
14+
15+
it("different should be 23 minutes compared to the current time", () => {
16+
const compare = addMinute(null, 23)
17+
expect(diffMinutes(compare)).toBe(23)
18+
})
1919
})

src/__tests__/diffMonths.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest"
22
import { diffMonths } from "../diffMonths"
3+
import { addMonth } from "../addMonth"
34

45
describe("differenceInMonths", () => {
56
it("should give 11 months", () => {
@@ -29,4 +30,9 @@ describe("differenceInMonths", () => {
2930
it("should also be a negative full month when swapped", () => {
3031
expect(diffMonths("2024-01-31", "2024-02-29")).toBe(-1)
3132
})
33+
34+
it("different should be 3 month compared to the current time", () => {
35+
const compare = addMonth(null, 3)
36+
expect(diffMonths(compare)).toBe(3)
37+
})
3238
})

src/__tests__/diffSeconds.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { describe, it, expect } from "vitest"
22
import { diffSeconds } from "../diffSeconds"
3+
import { addSecond } from "../addSecond"
34

45
describe("differenceInSeconds", () => {
56
it("difference is 28 seconds", () => {
6-
expect(
7-
diffSeconds("2024-04-07T09:10:28.900Z", "2024-04-07T09:10:00.000Z")
8-
).toBe(28)
7+
expect(diffSeconds("2024-04-07T09:10:28.900Z", "2024-04-07T09:10:00.000Z")).toBe(28)
8+
})
9+
10+
it("different should be 50 seconds compared to the current time", () => {
11+
const compare = addSecond(null, 50)
12+
expect(diffSeconds(compare)).toBe(50)
913
})
1014
})

src/__tests__/diffYears.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect, suite } from "vitest"
22
import { diffYears } from "../diffYears"
3+
import { addYear } from "../addYear"
34

45
describe("differenceInYears", () => {
56
it("returns the amount of full years between dates", () => {
@@ -45,4 +46,9 @@ describe("differenceInYears", () => {
4546
expect(diffYears("2024-04-27", "2025-04-26")).toBe(0)
4647
})
4748
})
49+
50+
it("different should be 3 month compared to the current time", () => {
51+
const compare = addYear(null, -6)
52+
expect(diffYears(null, compare)).toBe(6)
53+
})
4854
})

src/__tests__/hourEnd.spec.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ process.env.TZ = "America/New_York"
44

55
describe("hourEnd", () => {
66
it("can become the end of the hour", () => {
7-
expect(hourEnd("2023-02-22T12:30:00Z").toISOString()).toBe(
8-
"2023-02-22T12:59:59.999Z"
9-
)
7+
expect(hourEnd("2023-02-22T12:30:00Z").toISOString()).toBe("2023-02-22T12:59:59.999Z")
8+
})
9+
it("can become the end of the current hour", () => {
10+
const compare = new Date()
11+
compare.setMinutes(59, 59, 999)
12+
expect(hourEnd()).toEqual(compare)
1013
})
1114
})

src/__tests__/hourStart.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ describe("hourStart", () => {
88
"2023-02-22T12:00:00.000Z"
99
)
1010
})
11+
12+
it("can become the start of the current hour", () => {
13+
const compare = new Date()
14+
compare.setMinutes(0, 0, 0)
15+
expect(hourStart()).toEqual(compare)
16+
})
1117
})

src/__tests__/isAfter.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest"
22
import { isAfter } from "../isAfter"
3+
import { addDay } from "../addDay"
34
process.env.TZ = "America/New_York"
45

56
describe("isAfter", () => {
@@ -11,7 +12,14 @@ describe("isAfter", () => {
1112
})
1213
it("returns error if date is not valid", () => {
1314
expect(() => isAfter("invalid", "2022-01-01")).toThrowError(
14-
"Non ISO 8601 compliant date",
15+
"Non ISO 8601 compliant date"
1516
)
1617
})
18+
19+
it("returns false if date is in the past", () => {
20+
expect(isAfter(addDay(new Date(), -1))).toBe(false)
21+
})
22+
it("returns true if date is in the future", () => {
23+
expect(isAfter(addDay(new Date(), 1))).toBe(true)
24+
})
1725
})

src/__tests__/isBefore.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest"
22
import { isBefore } from "../isBefore"
3+
import { addDay } from "../addDay"
34
process.env.TZ = "America/New_York"
45

56
describe("isBefore", () => {
@@ -11,7 +12,14 @@ describe("isBefore", () => {
1112
})
1213
it("returns error if date is not valid", () => {
1314
expect(() => isBefore("invalid", "2022-01-01")).toThrowError(
14-
"Non ISO 8601 compliant date",
15+
"Non ISO 8601 compliant date"
1516
)
1617
})
18+
19+
it("returns true if date is in the past", () => {
20+
expect(isBefore(addDay(new Date(), -1))).toBe(true)
21+
})
22+
it("returns false if date is in the future", () => {
23+
expect(isBefore(addDay(new Date(), 1))).toBe(false)
24+
})
1725
})

src/__tests__/isEqual.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ describe("isEqual", () => {
1111
})
1212
it("returns error if date is not valid", () => {
1313
expect(() => isEqual("invalid", "2022-01-01")).toThrowError(
14-
"Non ISO 8601 compliant date",
14+
"Non ISO 8601 compliant date"
1515
)
1616
})
17+
18+
it("returns true because the create dated is the current time", () => {
19+
expect(isEqual(null, new Date())).toBe(true)
20+
expect(isEqual(new Date())).toBe(true)
21+
})
1722
})

0 commit comments

Comments
 (0)