Skip to content

Commit

Permalink
patch date series
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Dec 15, 2024
1 parent 05eb072 commit 53760c1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.12.1

### Patch Changes

- array

## 1.12.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.12.0",
"version": "1.12.1",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
68 changes: 33 additions & 35 deletions src/dates/getDateSeries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { describe, it, expect } from "@jest/globals";
import { getDateSeries } from "./getDateSeries";

describe("getDateSeries", () => {
it("should return a series of dates as ISO strings mapped to indices for days", () => {
it("should return a series of dates as ISO strings for days", () => {
const startDate = new Date("2024-01-01");
const endDate = new Date("2024-01-05");
const unit = "days";

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({
"2024-01-01T00:00:00.000Z": 0,
"2024-01-02T00:00:00.000Z": 0,
"2024-01-03T00:00:00.000Z": 0,
"2024-01-04T00:00:00.000Z": 0,
"2024-01-05T00:00:00.000Z": 0,
});
expect(result).toEqual([
"2024-01-01T00:00:00.000Z",
"2024-01-02T00:00:00.000Z",
"2024-01-03T00:00:00.000Z",
"2024-01-04T00:00:00.000Z",
"2024-01-05T00:00:00.000Z",
]);
});

it("should handle a series for hours", () => {
Expand All @@ -25,12 +25,12 @@ describe("getDateSeries", () => {

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({
"2024-01-01T00:00:00.000Z": 0,
"2024-01-01T01:00:00.000Z": 0,
"2024-01-01T02:00:00.000Z": 0,
"2024-01-01T03:00:00.000Z": 0,
});
expect(result).toEqual([
"2024-01-01T00:00:00.000Z",
"2024-01-01T01:00:00.000Z",
"2024-01-01T02:00:00.000Z",
"2024-01-01T03:00:00.000Z",
]);
});

it("should handle a series for minutes", () => {
Expand All @@ -40,14 +40,14 @@ describe("getDateSeries", () => {

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({
"2024-01-01T00:00:00.000Z": 0,
"2024-01-01T00:01:00.000Z": 0,
"2024-01-01T00:02:00.000Z": 0,
"2024-01-01T00:03:00.000Z": 0,
"2024-01-01T00:04:00.000Z": 0,
"2024-01-01T00:05:00.000Z": 0,
});
expect(result).toEqual([
"2024-01-01T00:00:00.000Z",
"2024-01-01T00:01:00.000Z",
"2024-01-01T00:02:00.000Z",
"2024-01-01T00:03:00.000Z",
"2024-01-01T00:04:00.000Z",
"2024-01-01T00:05:00.000Z",
]);
});

it("should handle a series for seconds", () => {
Expand All @@ -57,24 +57,24 @@ describe("getDateSeries", () => {

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({
"2024-01-01T00:00:00.000Z": 0,
"2024-01-01T00:00:01.000Z": 0,
"2024-01-01T00:00:02.000Z": 0,
"2024-01-01T00:00:03.000Z": 0,
"2024-01-01T00:00:04.000Z": 0,
"2024-01-01T00:00:05.000Z": 0,
});
expect(result).toEqual([
"2024-01-01T00:00:00.000Z",
"2024-01-01T00:00:01.000Z",
"2024-01-01T00:00:02.000Z",
"2024-01-01T00:00:03.000Z",
"2024-01-01T00:00:04.000Z",
"2024-01-01T00:00:05.000Z",
]);
});

it("should return an empty object if startDate is after endDate", () => {
it("should return an empty array if startDate is after endDate", () => {
const startDate = new Date("2024-01-05");
const endDate = new Date("2024-01-01");
const unit = "days";

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({});
expect(result).toEqual([]);
});

it("should handle a single date range", () => {
Expand All @@ -84,8 +84,6 @@ describe("getDateSeries", () => {

const result = getDateSeries(startDate, endDate, unit);

expect(result).toEqual({
"2024-01-01T00:00:00.000Z": 0,
});
expect(result).toEqual(["2024-01-01T00:00:00.000Z"]);
});
});
6 changes: 3 additions & 3 deletions src/dates/getDateSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const getDateSeries = (
startDate: Date,
endDate: Date,
unit: "days" | "hours" | "minutes" | "seconds"
): Record<ISODate, number> => {
const series: Record<ISODate, number> = {};
): ISODate[] => {
const series: ISODate[] = [];
const unitMap: Record<typeof unit, number> = {
days: 24 * 60 * 60 * 1000, // milliseconds in a day
hours: 60 * 60 * 1000, // milliseconds in an hour
Expand All @@ -20,7 +20,7 @@ export const getDateSeries = (
const end = endDate.getTime();

while (current <= end) {
series[new Date(current).toISOString()] = 0;
series.push(new Date(current).toISOString());
current += increment; // Move forward by the specified unit
}

Expand Down

0 comments on commit 53760c1

Please sign in to comment.