Skip to content

Commit

Permalink
Add optimized year, week key, quarter. Add cached version wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
velocityzen committed Mar 8, 2024
1 parent ebcc7bf commit 3117946
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
28 changes: 28 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DateTime } from "luxon";
import { IfValid, isValid } from "./helpers";

export function getCachedFor(
fn: <IsValid extends boolean>(
date: DateTime<IsValid>,
) => IfValid<IsValid, number>,
) {
const cache = new Map<string, number>();

return <IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> => {
if (!isValid(date)) {
return null as IfValid<IsValid, number>;
}

const key = date.toISODate() as string;
const cachedValue = cache.get(key);
if (cachedValue) {
return cachedValue as IfValid<IsValid, number>;
}

const value = fn(date);
cache.set(key, value);
return value as IfValid<IsValid, number>;
};
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { DateTime, Interval } from "luxon";
export * from "./cache";
export * from "./calendar";
export * from "./format";
export * from "./interval";
Expand Down
11 changes: 4 additions & 7 deletions src/quarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export function getBroadcastQuarter<IsValid extends boolean>(
return null as IfValid<IsValid, number>;
}

const quarterLength = 13;
return Math.min(Math.ceil(week / quarterLength), 4) as IfValid<
IsValid,
number
>;
return Math.ceil(date.endOf("week").month / 3) as IfValid<IsValid, number>;
}

/**
Expand All @@ -27,11 +23,12 @@ export function getBroadcastQuarter<IsValid extends boolean>(
export function getBroadcastQuarterWeek<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, [number, number]> {
const week = getBroadcastWeek(date);
if (!isValid(date) || week === null) {
if (!isValid(date)) {
return null as IfValid<IsValid, [number, number]>;
}

const week = getBroadcastWeek(date);

const quarterLength = 13;
const quarter = Math.min(Math.ceil(week / quarterLength), 4);
return [quarter, week] as IfValid<IsValid, [number, number]>;
Expand Down
10 changes: 5 additions & 5 deletions src/weekKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DateTime } from "luxon";
import { getBroadcastWeek } from "./week";
import { IfValid } from "./helpers";
import { IfValid, isValid } from "./helpers";
import { getBroadcastYear } from "./year";

/**
Expand All @@ -9,12 +9,12 @@ import { getBroadcastYear } from "./year";
export function getBroadcastWeekKey<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const broadcastYear = getBroadcastYear(date);
const broadcastWeek = getBroadcastWeek(date);

if (broadcastYear === null || broadcastWeek === null) {
if (!isValid(date)) {
return null as IfValid<IsValid, number>;
}

const broadcastYear = getBroadcastYear(date);
const broadcastWeek = getBroadcastWeek(date);

return (broadcastYear * 100 + broadcastWeek) as IfValid<IsValid, number>;
}
7 changes: 2 additions & 5 deletions src/year.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DateTime } from "luxon";

import { getBroadcastYearInterval } from "./interval";
import { IfValid, isValid } from "./helpers";

/**
Expand All @@ -9,11 +8,9 @@ import { IfValid, isValid } from "./helpers";
export function getBroadcastYear<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const yearInterval = getBroadcastYearInterval(date);

if (!(yearInterval && isValid(yearInterval.end))) {
if (!isValid(date)) {
return null as IfValid<IsValid, number>;
}

return yearInterval.end.get("year") as IfValid<IsValid, number>;
return date.endOf("week").year as unknown as IfValid<IsValid, number>;
}

0 comments on commit 3117946

Please sign in to comment.