@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
47 lines (45 loc) • 2.05 kB
JavaScript
import { plainDateOf } from "./inputChecks.js";
import { parseBusinessCalendar, resolveBusinessCalendar, stepBusinessDates } from "./businessCalendar.js";
import { isValidAmount } from "./isValidAmount.js";
// src/internal/businessDayValues.ts
/**
* The shared body of `addBusinessDays` and `subtractBusinessDays`: validate, resolve the
* calendar, and walk `amount` working days from `value`.
*
* - `sign` is the direction a **positive** `amount` moves — `1` for adding, `-1` for
* subtracting — so a negative `amount` walks the other way in both.
* - `calendar` is optional; absent means Saturday–Sunday with no holidays.
* - Returns `""` on an invalid date, a non-integer or out-of-range `amount`, an invalid
* calendar, and when the walk runs past `MAX_BUSINESS_DAY_STEPS`.
*/
function stepBusinessDaysValue(value, amount, sign, calendar) {
const resolved = resolveBusinessCalendar(calendar);
const date = plainDateOf(value);
if (date === null || !isValidAmount(amount) || !Number.isInteger(amount) || resolved === null) return "";
try {
if (amount === 0) return date.toString();
const result = stepBusinessDates(date, amount > 0 ? sign : sign === 1 ? -1 : 1, Math.abs(amount), resolved);
return result === null ? "" : result.toString();
} catch {
return "";
}
}
/**
* The shared body of `nextBusinessDay` and `previousBusinessDay`: the first working day
* **strictly** one business day away from `value`, in `direction`.
*
* Unlike `stepBusinessDaysValue`, `calendar` is required — these two assume no weekend, so an
* absent calendar is invalid input rather than the Saturday–Sunday default.
*/
function neighbourBusinessDayValue(value, direction, calendar) {
const resolved = parseBusinessCalendar(calendar);
const date = plainDateOf(value);
if (date === null || resolved === null) return "";
try {
const result = stepBusinessDates(date, direction, 1, resolved);
return result === null ? "" : result.toString();
} catch {
return "";
}
}
export { neighbourBusinessDayValue, stepBusinessDaysValue };