Skip to content

Commit

Permalink
Optimise the transform object method
Browse files Browse the repository at this point in the history
It now works with arrays
The type is even better
  • Loading branch information
Dlurak committed Apr 10, 2024
1 parent 1c73415 commit b51fbef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/routes/calendar/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,8 @@ export const listCalendar = new Elysia().use(HttpStatusCode()).get(
return DATABASE_READ_FAILED;
}

// the type is incorrect ending is `CustomDate | null` not `Date | null`
// Not in the mood to fix it rn
const formatted = result.data.calendar.map((c) => ({
...replaceDateDeep(c, normalDateToCustom),
tags: c.tags,
updates: c.updates.map((u) => replaceDateDeep(u, (d) => d.getTime())),
}));

Expand Down
2 changes: 1 addition & 1 deletion src/routes/tags/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import e from "@edgedb";
import { DATABASE_DELETE_FAILED, UNAUTHORIZED } from "constants/responses";
import Elysia, { t } from "elysia";
import Elysia from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
import { auth } from "plugins/auth";
Expand Down
12 changes: 8 additions & 4 deletions src/utils/objects/transform.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
type Object = Record<string | number | symbol, unknown>;

type FinalReturn<T, R> = T extends Object ? ConverDatesDeep<T, R> : T;

type ConverDatesDeep<T extends Object, R> = {
[K in keyof T]: T[K] extends Date
[K in keyof T]: Date extends T[K]
? R
: T[K] extends Object
? ConverDatesDeep<T[K], R>
: T[K];
: T[K] extends unknown[]
? FinalReturn<T[K][number], R>
: FinalReturn<T[K], R>;
};

type DateCallback<T> = (val: Date) => T;
Expand All @@ -18,6 +20,8 @@ export const replaceDateDeep = <T extends Object, R>(
const newPairs: [string, unknown][] = [];
for (const [key, val] of pairs) {
if (val instanceof Date) newPairs.push([key, callback(val)]);
else if (Array.isArray(val))
newPairs.push([key, val.map((i) => replaceDateDeep(i, callback))]);
else if (typeof val === "object" && val)
newPairs.push([key, replaceDateDeep(val as Object, callback)]);
else newPairs.push([key, val]);
Expand Down
28 changes: 28 additions & 0 deletions tests/objects/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe("transform", () => {
it("works recursively", () => {
const obj = {
date: new Date("2021-01-01"),
arrayStuff: [
{
date: new Date("2021-01-01"),
number: 1,
},
{
date: new Date("2021-01-01"),
number: 1,
},
{
date: new Date("2021-01-01"),
number: 1,
},
],
inner: {
date: new Date("2021-01-01"),
number: 1,
Expand All @@ -29,6 +43,20 @@ describe("transform", () => {

expect(replaceDateWithTimestamp(obj)).toEqual({
date: 1609459200000,
arrayStuff: [
{
date: 1609459200000,
number: 1,
},
{
date: 1609459200000,
number: 1,
},
{
date: 1609459200000,
number: 1,
},
],
inner: {
date: 1609459200000,
number: 1,
Expand Down

0 comments on commit b51fbef

Please sign in to comment.