@eristack/business-calendar
Version:
Business days and holidays on YYYY-MM-DD wall dates — no timestamp import in core
54 lines (52 loc) • 1.77 kB
JavaScript
// src/core/errors.ts
var BusinessCalendarParseError = class extends Error {
code = "BUSINESS_CALENDAR_PARSE_ERROR";
constructor(message) {
super(message);
this.name = "BusinessCalendarParseError";
}
};
// src/core/wall-date.ts
var WALL_DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
function normalizeWallDate(value) {
const trimmed = value.trim();
const match = WALL_DATE_RE.exec(trimmed);
if (!match) {
throw new BusinessCalendarParseError(
`Invalid wall date "${value}" \u2014 expected YYYY-MM-DD`
);
}
const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
const utc = Date.UTC(year, month - 1, day);
const check = new Date(utc);
if (check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day) {
throw new BusinessCalendarParseError(`Invalid calendar date "${value}"`);
}
const mm = String(month).padStart(2, "0");
const dd = String(day).padStart(2, "0");
return `${year}-${mm}-${dd}`;
}
function wallDateDayOfWeek(date) {
const normalized = normalizeWallDate(date);
const [y, m, d] = normalized.split("-").map(Number);
return new Date(Date.UTC(y, m - 1, d)).getUTCDay();
}
function addWallDays(date, days) {
const normalized = normalizeWallDate(date);
const [y, m, d] = normalized.split("-").map(Number);
const next = new Date(Date.UTC(y, m - 1, d));
next.setUTCDate(next.getUTCDate() + days);
const ny = next.getUTCFullYear();
const nm = String(next.getUTCMonth() + 1).padStart(2, "0");
const nd = String(next.getUTCDate()).padStart(2, "0");
return `${ny}-${nm}-${nd}`;
}
export {
BusinessCalendarParseError,
normalizeWallDate,
wallDateDayOfWeek,
addWallDays
};
//# sourceMappingURL=chunk-YHRRW3E4.js.map