Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat!: convert date to iso instead of number #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/Primitives.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
type Year = `${number}${number}${number}${number}`;
type Month = `${number}${number}`;
type Day = `${number}${number}`;
type Hour = `${number}${number}`;
type Minute = `${number}${number}`;
type Second = `${number}${number}`;
type Milliseconds = `${number}${number}${number}`;
type Timezone = "Z" | `${"+" | "-"}${number}${number}:${number}${number}`;

export type ISODateTime =
`${Year}-${Month}-${Day}T${Hour}:${Minute}:${Second}.${Milliseconds}${Timezone}`;

type Methods<T> = {
[P in keyof T]: T[P] extends Function ? P : never;
}[keyof T];
Expand All @@ -9,24 +21,24 @@ type Properties<T> = Omit<MethodsAndProperties<T>, Methods<T>>;

type PrimitiveTypes = string | number | boolean | undefined | null;

type DateToNumber<T> = T extends Date ? number : T;
type DateToISODateTime<T> = T extends Date ? ISODateTime : T;

type ValueObjectValue<T> = T extends PrimitiveTypes
? T
: T extends Date
? number
? ISODateTime
: T extends { value: infer U }
? DateToNumber<U>
? DateToISODateTime<U>
: T extends Array<{ value: infer U }>
? DateToNumber<U>[]
? DateToISODateTime<U>[]
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: unknown }
? {
[K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]>;
}
: T extends unknown
? DateToNumber<T>
? DateToISODateTime<T>
: never;

export type Primitives<T> = {
Expand Down
5 changes: 3 additions & 2 deletions tests/Primitives.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectTypeOf } from "expect-type";

import { Primitives } from "../src";
import { ISODateTime } from "../src/Primitives";
import { Course } from "./Course";
import { DeliveryInfo } from "./DeliveryInfo";
import { Learner } from "./Learner";
Expand Down Expand Up @@ -70,12 +71,12 @@ describe("Primitives", () => {
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should get primitive number type from Date", () => {
it("should get primitive ISO string type from Date", () => {
type actualPrimitives = Primitives<Step>;

type expectedPrimitives = {
readonly name: string;
readonly publishedAt: number;
readonly publishedAt: ISODateTime;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
Expand Down
Loading