UNPKG

@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.

39 lines (37 loc) • 2.1 kB
import { isValidDate } from "../../plain/validate/isValidDate.js"; import { isObject } from "../../internal/isObject.js"; import { isValidFiscalExtraWeek } from "./isValidFiscalExtraWeek.js"; import { isValidFiscalPattern } from "./isValidFiscalPattern.js"; import { isValidFiscalYearLabel } from "./isValidFiscalYearLabel.js"; // src/calendar/validate/isValidFiscalCalendar.ts /** * Return true when `calendar` is a `FiscalCalendar` that `getFiscalPeriod` accepts. * * - `pattern` must be `"4-5-4"`, `"4-4-5"` or `"5-4-4"`. * - `yearEndsOn` must be a plain ISO date. A datetime or a date that does not exist is not. * - `extraWeek` must be `"firstPeriod"` or `"lastPeriod"`, and `yearLabel` must be * `"startYear"`, `"endYear"` or `"majorityYear"`. Both are required: there is no default. * - Extra properties are ignored. * - Returns `false` for non-objects, `null` and arrays. * - `getFiscalPeriod` returns null for a calendar this rejects, the same sentinel it returns * for an invalid date. Check the calendar here to tell the two apart. * * @param calendar candidate value of any type * @returns boolean indicating whether `getFiscalPeriod` accepts it * * @example isValidFiscalCalendar({ pattern: "4-5-4", yearEndsOn: "2026-01-31", extraWeek: "lastPeriod", yearLabel: "startYear" }) // true * @example isValidFiscalCalendar({ pattern: "4-5-4", yearEndsOn: "2026-01-31" }) // false (the 1.19 shape: no extraWeek or yearLabel) * @example isValidFiscalCalendar({ pattern: "4-5-4", yearEndsOn: "2026-02-30", extraWeek: "lastPeriod", yearLabel: "startYear" }) // false * @example isValidFiscalCalendar("4-5-4") // false * @example isValidFiscalCalendar([]) // false */ function isValidFiscalCalendar(calendar) { try { if (!isObject(calendar) || Array.isArray(calendar)) return false; const { pattern, yearEndsOn, extraWeek, yearLabel } = calendar; return isValidFiscalPattern(pattern) && typeof yearEndsOn === "string" && isValidDate(yearEndsOn) && isValidFiscalExtraWeek(extraWeek) && isValidFiscalYearLabel(yearLabel); } catch { return false; } } export { isValidFiscalCalendar };