-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename to getDateCachedFor and accept any return type
- Loading branch information
1 parent
9cbd595
commit 0895609
Showing
1 changed file
with
6 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |