@eristack/business-calendar
Version:
Business days and holidays on YYYY-MM-DD wall dates — no timestamp import in core
68 lines (67 loc) • 1.78 kB
JavaScript
import {
BusinessCalendarParseError,
addWallDays,
normalizeWallDate,
wallDateDayOfWeek
} from "./chunk-YHRRW3E4.js";
// src/core/calendar.ts
function normalizeWeekendDays(days) {
const set = /* @__PURE__ */ new Set();
for (const day of days) {
if (!Number.isInteger(day) || day < 0 || day > 6) {
throw new BusinessCalendarParseError(
`weekendDays entry ${day} must be an integer 0\u20136 (Sun\u2013Sat)`
);
}
set.add(day);
}
return set;
}
function createBusinessCalendar(options) {
const weekendDays = normalizeWeekendDays(options.weekendDays);
const holidays = new Set(
options.holidays.map((h) => normalizeWallDate(h))
);
function isBusinessDay(date) {
const wall = normalizeWallDate(date);
if (holidays.has(wall)) {
return false;
}
const dow = wallDateDayOfWeek(wall);
return !weekendDays.has(dow);
}
function nextBusinessDay(date) {
let cursor = normalizeWallDate(date);
do {
cursor = addWallDays(cursor, 1);
} while (!isBusinessDay(cursor));
return cursor;
}
function addBusinessDays(date, days) {
if (!Number.isInteger(days)) {
throw new BusinessCalendarParseError("days must be an integer");
}
let cursor = normalizeWallDate(date);
if (days === 0) {
return cursor;
}
const step = days > 0 ? 1 : -1;
let remaining = Math.abs(days);
while (remaining > 0) {
cursor = addWallDays(cursor, step);
if (isBusinessDay(cursor)) {
remaining -= 1;
}
}
return cursor;
}
return { isBusinessDay, nextBusinessDay, addBusinessDays };
}
export {
BusinessCalendarParseError,
addWallDays,
createBusinessCalendar,
normalizeWallDate,
wallDateDayOfWeek
};
//# sourceMappingURL=index.js.map