Skip to content

Commit

Permalink
Fix types for valid dates and intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
velocityzen committed Mar 5, 2024
1 parent d5dda7e commit 619ec1a
Show file tree
Hide file tree
Showing 14 changed files with 3,528 additions and 8,525 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 18.15.0
nodejs 20.10.0
11,635 changes: 3,299 additions & 8,336 deletions package-lock.json

Large diffs are not rendered by default.

42 changes: 16 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,43 @@
"scripts": {
"bin": "node ./bin/broadcast-calendar.js",
"prebuild": "rimraf lib",
"build": "rollup --bundleConfigAsCjs -c",
"build": "rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
"lint": "eslint .",
"precommit": "npm run lint",
"test": "npm run build && ava",
"test:coverage": "c8 ava",
"format": "prettier --loglevel error --write \"./src/**/*.ts\" \"./test.js\" \"./rollup.config.ts\" \"*.md\"",
"format:staged": "git-format-staged -f 'prettier --ignore-unknown --loglevel error --stdin --stdin-filepath \"{}\"' .",
"prepare": "husky install",
"release": "npm run build && release-it"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ViacomInc/broadcast-calendar.git"
},
"keywords": [
"date",
"dates",
"broadcast",
"calendar"
],
"engines": {
"node": ">=16.0.0"
},
"dependencies": {
"keywords": ["date", "dates", "broadcast", "calendar"],
"peerDependencies": {
"luxon": "^3.0.0"
},
"private": false,
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/luxon": "^3.0.0",
"@types/rollup-plugin-peer-deps-external": "^2.2.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"ansi-regex": ">=5.0.1",
"ava": "^5.0.0",
"c8": "^7.13.0",
"ava": "^6.0.0",
"c8": "^9.1.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.0",
"git-format-staged": "^3.0.0",
"husky": "^8.0.0",
"release-it": "^15.0.0",
"rimraf": "^4.0.0",
"rollup": "^3.0.0",
"rollup-plugin-node-externals": "^5.1.2",
"rollup-plugin-typescript2": "^0.34.0",
"husky": "^9.0.0",
"release-it": "^17.0.0",
"rimraf": "^5.0.0",
"rollup": "^4.0.0",
"rollup-plugin-node-externals": "^7.0.0",
"ts-node": "^10.2.0",
"typescript": "^5.0.0"
}
Expand Down
19 changes: 12 additions & 7 deletions rollup.config.js → rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import externals from "rollup-plugin-node-externals";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import externals from "rollup-plugin-node-externals";

const pkg = require("./package.json");
const plugins = [
commonjs(),
resolve(),
externals(),
typescript({ exclude: "rollup.config.ts" }),
];

export default {
input: "src/index.ts",
output: [
{
file: pkg.main,
file: "lib/index.js",
format: "cjs",
sourcemap: true,
exports: "named",
},
{
file: pkg.module,
file: "lib/index.es.js",
format: "es",
sourcemap: true,
exports: "named",
},
],
plugins: [externals(), typescript(), resolve(), commonjs()],
plugins,
};
13 changes: 10 additions & 3 deletions src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DateTime } from "luxon";
import { BroadcastCalendar } from "./types";
import { getBroadcastYear, getBroadcastQuarter } from "./yearQuarter";
import {
getBroadcastYearInterval,
Expand All @@ -10,11 +9,15 @@ import {
import { getBroadcastWeek } from "./week";
import { getBroadcastWeekKey } from "./weekKey";

export function toCalendarDateTime(broadcast: DateTime): DateTime {
export function toCalendarDateTime<IsValid extends boolean>(
broadcast: DateTime<IsValid>,
): DateTime<IsValid> {
return broadcast.hour < 6 ? broadcast.plus({ day: 1 }) : broadcast;
}

export function getBroadcastCalendar(date: DateTime): BroadcastCalendar {
export function getBroadcastCalendar<IsValid extends boolean>(
date: DateTime<IsValid>,
) {
return {
date,
year: getBroadcastYear(date),
Expand All @@ -27,3 +30,7 @@ export function getBroadcastCalendar(date: DateTime): BroadcastCalendar {
weekKey: getBroadcastWeekKey(date),
};
}

export type BroadcastCalendar<IsValid extends boolean> = ReturnType<
typeof getBroadcastCalendar<IsValid>
>;
36 changes: 20 additions & 16 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { DateTime, Interval } from "luxon";
import {
BroadcastCalendar,
BroadcastCalendarString,
BroadcastTimeZone,
StringInterval,
} from "./types";
import { BroadcastTimeZone, StringInterval } from "./types";
import { IfValid } from "./helpers";
import { BroadcastCalendar } from "./calendar";

export const makeFormatter =
(format: string) =>
Expand All @@ -13,31 +10,38 @@ export const makeFormatter =

export const toISODate = makeFormatter("yyyy-MM-dd");

export function formatToISOWithoutTZ(datetime: DateTime): string | null {
export function formatToISOWithoutTZ<IsValid extends boolean>(
datetime: DateTime<IsValid>,
) {
return datetime.toISO({
includeOffset: false,
suppressMilliseconds: true,
});
}

export function formatToSQLWithoutTZ(datetime: DateTime): string | null {
export function formatToSQLWithoutTZ<IsValid extends boolean>(
datetime: DateTime<IsValid>,
) {
return datetime.toSQL({
includeOffset: false,
});
}

export function formatBroadcastDateInterval(
interval: Interval,
format = toISODate
): StringInterval | null {
export function formatBroadcastDateInterval<IsValid extends boolean>(
interval: Interval<IsValid>,
format = toISODate,
): IfValid<IsValid, StringInterval> {
if (!interval.start || !interval.end) {
return null;
return null as IfValid<IsValid, StringInterval>;
}

return [format(interval.start), format(interval.end)];
return [format(interval.start), format(interval.end)] as IfValid<
IsValid,
StringInterval
>;
}

export function formatBroadcastCalendar({
export function formatBroadcastCalendar<IsValid extends boolean>({
date,
year,
yearInterval,
Expand All @@ -47,7 +51,7 @@ export function formatBroadcastCalendar({
week,
weekKey,
weekInterval,
}: BroadcastCalendar): BroadcastCalendarString {
}: BroadcastCalendar<IsValid>) {
return {
date: date.toISODate(),
year,
Expand Down
12 changes: 11 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { DateTime } from "luxon";

export function isValid(date: DateTime | null | undefined): date is DateTime {
export type IfValid<
ThisIsValid extends boolean,
ValidType,
InvalidType = DefaultInvalidType,
> = ThisIsValid extends true ? ValidType : InvalidType;

export type DefaultInvalidType = null;

export function isValid(
date: DateTime | null | undefined,
): date is DateTime<true> {
return Boolean(date?.isValid);
}
Loading

0 comments on commit 619ec1a

Please sign in to comment.