Skip to content

Commit

Permalink
fix parseDate
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Dec 5, 2024
1 parent e0eb3bc commit 020c591
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 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.11.1

### Patch Changes

- fix parseDate to be always local

## 1.11.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.11.0",
"version": "1.11.1",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/getUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { describe, expect, test } from "@jest/globals";
import { getUrlSearchParams } from "./getUrlSearchParams";

describe("getUrlSearchParams", () => {
test("no domain", async () => {
expect(getUrlSearchParams("/")).toStrictEqual({});
expect(getUrlSearchParams("/?param=")).toStrictEqual({
param: "",
});
expect(getUrlSearchParams("/?param1=123&param2=asd")).toStrictEqual({
param1: "123",
param2: "asd",
});
});

test("found", async () => {
expect(getUrlSearchParams("https")).toStrictEqual({});
expect(getUrlSearchParams("https://www.ciao.com")).toStrictEqual({});
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/getUrlSearchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export const getUrlSearchParams = (
): Record<string, string> => {
if (!urlString) return {};
try {
const url = new URL(urlString);
const isRelativeUrl = urlString.startsWith("/");
const url = new URL(
urlString,
isRelativeUrl ? "https://deverything.dev/" : undefined // add base to make relative urls work
);

return Object.fromEntries(url.searchParams);
} catch (_e) {
return {};
Expand Down
79 changes: 72 additions & 7 deletions src/helpers/parseDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from "@jest/globals";
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
import { MAX_DATE_MILLISECONDS } from "../constants/time";
import { parseDate } from "./parseDate";
import timezoneMock from "timezone-mock";

// TODO: add string with millis 1678792327650170902
describe("parseDate", () => {
Expand All @@ -17,12 +18,76 @@ describe("parseDate", () => {
expect(parseDate(MAX_DATE_MILLISECONDS + 1)).toBeUndefined();
});

test("date", async () => {
expect(parseDate(new Date("2000-01-01"))).toStrictEqual(
new Date("2000-01-01")
);
describe("ISO date with non UTC timezone", () => {
beforeAll(() => {
// If this test is run in UTC, it will always pass!
timezoneMock.register("Australia/Adelaide");
});
afterAll(() => {
timezoneMock.unregister();
});
test("year", async () => {
expect(parseDate("2000")!.toISOString()).toStrictEqual(
new Date("2000-01-01T00:00:00.000+10:30").toISOString()
);
});

expect(parseDate(new Date("2000-00-00"))).toBeUndefined();
expect(parseDate(new Date("2000-02-32"))).toBeUndefined();
test("month", async () => {
expect(parseDate("2000-14")).toBeUndefined();

expect(parseDate("2000-02")).toStrictEqual(
new Date("2000-02-01T00:00:00.000+10:30")
);
});

test("day", async () => {
expect(parseDate("2000-00-00")).toBeUndefined();
expect(parseDate("2000-02-32")).toBeUndefined();

expect(parseDate("2000-02-21")).toStrictEqual(
new Date("2000-02-21T00:00:00.000+10:30")
);
});

test("time", async () => {
expect(parseDate("2000-02-02T40:00:00")).toBeUndefined();

expect(parseDate("2000-02-21T00:00:00")).toStrictEqual(
new Date("2000-02-21T00:00:00.000+10:30")
);
});
});

describe("ISO date UTC (servers default)", () => {
test("year", async () => {
expect(parseDate("2000")).toStrictEqual(
new Date("2000-01-01T00:00:00.000Z")
);
});

test("month", async () => {
expect(parseDate("2000-14")).toBeUndefined();

expect(parseDate("2000-02")).toStrictEqual(
new Date("2000-02-01T00:00:00.000Z")
);
});

test("day", async () => {
expect(parseDate("2000-00-00")).toBeUndefined();
expect(parseDate("2000-02-32")).toBeUndefined();

expect(parseDate("2000-02-21")).toStrictEqual(
new Date("2000-02-21T00:00:00.000Z")
);
});

test("time", async () => {
expect(parseDate("2000-02-02T40:00:00")).toBeUndefined();

expect(parseDate("2000-02-21T00:00:00")).toStrictEqual(
new Date("2000-02-21T00:00:00.000Z")
);
});
});
});
14 changes: 13 additions & 1 deletion src/helpers/parseDate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { DateLike, Maybe } from "../types";
import { isEmpty } from "../validators/isEmpty";
import { isJsDate } from "../validators/isJsDate";
import { isString } from "../validators/isString";

const partialDateRegex = /^\d{4}(-\d{2})?(-\d{2})?$/;

export const parseDate = (arg?: Maybe<DateLike>) => {
if (isEmpty(arg)) return;

const date = new Date(arg!);
if (isString(arg) && partialDateRegex.test(arg)) {
// Add time to date string because it will be interpreted
// as UTC date instead of local time, and there is no
// circumstance where UTC as default is desired.
arg = `${
arg + "-01-01".slice(arg.length - 4) // fill missing month and day if needed
}T00:00:00`;
}

const date = new Date(arg!); // ! => isEmpty cannot narrow type

if (!isJsDate(date)) return;

Expand Down

0 comments on commit 020c591

Please sign in to comment.