From 311794655a8a206bc3e90bcb50e682ebbee028af Mon Sep 17 00:00:00 2001 From: Alexey Novikov Date: Fri, 8 Mar 2024 14:44:27 -0500 Subject: [PATCH] Add optimized year, week key, quarter. Add cached version wrapper --- src/cache.ts | 28 ++++++++++++++++++++++++++++ src/index.ts | 1 + src/quarter.ts | 11 ++++------- src/weekKey.ts | 10 +++++----- src/year.ts | 7 ++----- 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/cache.ts diff --git a/src/cache.ts b/src/cache.ts new file mode 100644 index 0000000..e259454 --- /dev/null +++ b/src/cache.ts @@ -0,0 +1,28 @@ +import { DateTime } from "luxon"; +import { IfValid, isValid } from "./helpers"; + +export function getCachedFor( + fn: ( + date: DateTime, + ) => IfValid, +) { + const cache = new Map(); + + return ( + date: DateTime, + ): IfValid => { + if (!isValid(date)) { + return null as IfValid; + } + + const key = date.toISODate() as string; + const cachedValue = cache.get(key); + if (cachedValue) { + return cachedValue as IfValid; + } + + const value = fn(date); + cache.set(key, value); + return value as IfValid; + }; +} diff --git a/src/index.ts b/src/index.ts index 88a1cf8..34814b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export { DateTime, Interval } from "luxon"; +export * from "./cache"; export * from "./calendar"; export * from "./format"; export * from "./interval"; diff --git a/src/quarter.ts b/src/quarter.ts index 30e8066..9c02fec 100644 --- a/src/quarter.ts +++ b/src/quarter.ts @@ -14,11 +14,7 @@ export function getBroadcastQuarter( return null as IfValid; } - 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; } /** @@ -27,11 +23,12 @@ export function getBroadcastQuarter( export function getBroadcastQuarterWeek( date: DateTime, ): IfValid { - const week = getBroadcastWeek(date); - if (!isValid(date) || week === null) { + if (!isValid(date)) { return null as IfValid; } + const week = getBroadcastWeek(date); + const quarterLength = 13; const quarter = Math.min(Math.ceil(week / quarterLength), 4); return [quarter, week] as IfValid; diff --git a/src/weekKey.ts b/src/weekKey.ts index f8f9cce..a7ffc28 100644 --- a/src/weekKey.ts +++ b/src/weekKey.ts @@ -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"; /** @@ -9,12 +9,12 @@ import { getBroadcastYear } from "./year"; export function getBroadcastWeekKey( date: DateTime, ): IfValid { - const broadcastYear = getBroadcastYear(date); - const broadcastWeek = getBroadcastWeek(date); - - if (broadcastYear === null || broadcastWeek === null) { + if (!isValid(date)) { return null as IfValid; } + const broadcastYear = getBroadcastYear(date); + const broadcastWeek = getBroadcastWeek(date); + return (broadcastYear * 100 + broadcastWeek) as IfValid; } diff --git a/src/year.ts b/src/year.ts index 8de7ed1..c6120a5 100644 --- a/src/year.ts +++ b/src/year.ts @@ -1,6 +1,5 @@ import { DateTime } from "luxon"; -import { getBroadcastYearInterval } from "./interval"; import { IfValid, isValid } from "./helpers"; /** @@ -9,11 +8,9 @@ import { IfValid, isValid } from "./helpers"; export function getBroadcastYear( date: DateTime, ): IfValid { - const yearInterval = getBroadcastYearInterval(date); - - if (!(yearInterval && isValid(yearInterval.end))) { + if (!isValid(date)) { return null as IfValid; } - return yearInterval.end.get("year") as IfValid; + return date.endOf("week").year as unknown as IfValid; }