Skip to content

Commit

Permalink
Rename to getDateCachedFor and accept any return type
Browse files Browse the repository at this point in the history
  • Loading branch information
velocityzen committed Mar 9, 2024
1 parent 9cbd595 commit 0895609
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
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>();
export function getDateCachedFor<T>(fn: (date: DateTime<true>) => T) {
const cache = new Map<string, T>();

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;
return (date: DateTime<true>): T => {
const key = date.toISODate();
const cachedValue = cache.get(key);
if (cachedValue) {
return cachedValue as IfValid<IsValid, number>;
return cachedValue;
}

const value = fn(date);
cache.set(key, value);
return value as IfValid<IsValid, number>;
return value;
};
}

0 comments on commit 0895609

Please sign in to comment.