Skip to content

Commit 0862433

Browse files
authored
getDateRangeSeries (#28)
1 parent 173ac4c commit 0862433

File tree

6 files changed

+137
-1
lines changed

6 files changed

+137
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# deverything
22

3+
## 3.1.0
4+
5+
### Minor Changes
6+
7+
- getDateRangeSeries
8+
39
## 3.0.0
410

511
### Major Changes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deverything",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "Everything you need for Dev",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, it, expect } from "@jest/globals";
2+
import { getDateRangeSeries } from "./getDateRangeSeries";
3+
4+
describe("getDateRangeSeries", () => {
5+
it("should return a series of dates as ISO strings for days", () => {
6+
const startDate = new Date("2024-01-01");
7+
const endDate = new Date("2024-01-05");
8+
const unit = "days";
9+
10+
const result = getDateRangeSeries({ startDate, endDate }, unit);
11+
12+
expect(result).toEqual([
13+
"2024-01-01T00:00:00.000Z",
14+
"2024-01-02T00:00:00.000Z",
15+
"2024-01-03T00:00:00.000Z",
16+
"2024-01-04T00:00:00.000Z",
17+
]);
18+
});
19+
20+
it("should handle a series for hours", () => {
21+
const startDate = new Date("2024-01-01T00:00:00.000Z");
22+
const endDate = new Date("2024-01-01T03:00:00.000Z");
23+
const unit = "hours";
24+
25+
const result = getDateRangeSeries({ startDate, endDate }, unit);
26+
27+
expect(result).toEqual([
28+
"2024-01-01T00:00:00.000Z",
29+
"2024-01-01T01:00:00.000Z",
30+
"2024-01-01T02:00:00.000Z",
31+
]);
32+
});
33+
34+
it("should handle a series for minutes", () => {
35+
const startDate = new Date("2024-01-01T00:00:00.000Z");
36+
const endDate = new Date("2024-01-01T00:05:00.000Z");
37+
const unit = "minutes";
38+
39+
const result = getDateRangeSeries({ startDate, endDate }, unit);
40+
41+
expect(result).toEqual([
42+
"2024-01-01T00:00:00.000Z",
43+
"2024-01-01T00:01:00.000Z",
44+
"2024-01-01T00:02:00.000Z",
45+
"2024-01-01T00:03:00.000Z",
46+
"2024-01-01T00:04:00.000Z",
47+
]);
48+
});
49+
50+
it("should handle a series for seconds", () => {
51+
const startDate = new Date("2024-01-01T00:00:00.000Z");
52+
const endDate = new Date("2024-01-01T00:00:05.000Z");
53+
const unit = "seconds";
54+
55+
const result = getDateRangeSeries({ startDate, endDate }, unit);
56+
57+
expect(result).toEqual([
58+
"2024-01-01T00:00:00.000Z",
59+
"2024-01-01T00:00:01.000Z",
60+
"2024-01-01T00:00:02.000Z",
61+
"2024-01-01T00:00:03.000Z",
62+
"2024-01-01T00:00:04.000Z",
63+
]);
64+
});
65+
66+
it("should throw an error if startDate is after endDate", () => {
67+
const startDate = new Date("2024-01-05");
68+
const endDate = new Date("2024-01-01");
69+
const unit = "days";
70+
71+
expect(() => getDateRangeSeries({ startDate, endDate }, unit)).toThrow();
72+
});
73+
74+
it("should handle a single date range", () => {
75+
const startDate = new Date("2024-01-01T00:00:00.000Z");
76+
const endDate = new Date("2024-01-01T00:00:00.000Z");
77+
const unit = "days";
78+
79+
const result = getDateRangeSeries({ startDate, endDate }, unit);
80+
81+
expect(result).toEqual([]);
82+
});
83+
});

src/dates/getDateRangeSeries.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { parseDate } from "../helpers/parseDate";
2+
import { DateRange, ISODate } from "../types";
3+
4+
/**
5+
*
6+
* @description Generate a series of dates between the start and end dates
7+
* NOTE: it does NOT include the end date
8+
*/
9+
export const getDateRangeSeries = (
10+
dateRange: DateRange,
11+
unit: "days" | "hours" | "minutes" | "seconds"
12+
): ISODate[] => {
13+
const series: ISODate[] = [];
14+
const unitMap: Record<typeof unit, number> = {
15+
days: 24 * 60 * 60 * 1000, // milliseconds in a day
16+
hours: 60 * 60 * 1000, // milliseconds in an hour
17+
minutes: 60 * 1000, // milliseconds in a minute
18+
seconds: 1000, // milliseconds in a second
19+
};
20+
21+
const increment = unitMap[unit];
22+
23+
const { startDate, endDate } = dateRange;
24+
25+
const start = parseDate(startDate);
26+
const end = parseDate(endDate);
27+
28+
if (!start || !end || start.getTime() > end.getTime()) {
29+
throw new Error("Invalid date range");
30+
}
31+
32+
// Ensure we are working with time in milliseconds
33+
let currentTime = start.getTime();
34+
const endTime = end.getTime();
35+
36+
while (currentTime < endTime) {
37+
series.push(new Date(currentTime).toISOString());
38+
currentTime += increment; // Move forward by the specified unit
39+
}
40+
41+
return series;
42+
};

src/dates/getDateSeries.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { ISODate } from "../types";
22

3+
/**
4+
*
5+
* @deprecated use getDateRangeSeries instead
6+
*/
37
export const getDateSeries = (
48
startDate: Date,
59
endDate: Date,

src/dates/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./getDateSeries";
2+
export * from "./getDateRangeSeries";
23
export * from "./isOver18";
34
export * from "./startOfDay";
45
export * from "./startOfNextMonth";

0 commit comments

Comments
 (0)