@kokr/date
Version:
Provides utilities for Korean dates. 날짜 관련 유틸리티를 제공합니다. 공휴일, 절기, 그리고 잡절 정보를 확인하고, 영업일 기준 날짜 계산을 지원합니다.
32 lines (31 loc) • 949 B
JavaScript
import * as dntShim from "./_dnt.shims.js";
export function cache(fn, options = {}) {
const ttl = Math.max(options.ttl ?? 86400, 0);
const cacheKeyName = options.cacheKeyName ??
"@kokr/date/holidays";
return Object.assign(async (year) => {
const now = Date.now();
let cacheData = {};
try {
cacheData = JSON.parse(dntShim.localStorage.getItem(cacheKeyName) ?? "");
}
catch {
// ignore
}
const cache = cacheData[year];
if (cache && cache.cached + ttl > now) {
return cache.holidays;
}
const holidays = await fn(year);
cacheData[year] = {
cached: now,
holidays,
};
dntShim.localStorage.setItem(cacheKeyName, JSON.stringify(cacheData));
return holidays;
}, {
clear() {
dntShim.localStorage.removeItem(cacheKeyName);
},
});
}