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.

38 lines (36 loc) • 1.91 kB
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; // src/calendar/validate/isValidZoneBucketUnit.ts /** * Return true when `unit` is a valid ZoneBucketUnit. * * - Valid units are: "hour", "day", "week", "month", singular or plural (`"days"`), as Temporal * §13.17 names a unit either way and `floorToZone`/`bucketRange` accept both. * - Accepts any input type and returns false for non-string values. * - Uses type assertion to narrow the type. * - Narrower than `isValidDateTimeUnit` on purpose: these are the boundaries a calendar in a * zone actually has. Sub-hour units need no zone, and "year" is left out because the * calendar and fiscal year questions are `getQuarter`'s and `getFiscalPeriod`'s. * - `floorToZone` returns "" and `bucketRange` returns [] for a unit they do not recognise — * the same sentinels they return for an unusable instant. Check the unit here to tell a * misconfigured rollup apart from a bad timestamp. * * @param unit candidate value of any type * @returns boolean indicating validity * * @example isValidZoneBucketUnit("hour") // true * @example isValidZoneBucketUnit("day") // true * @example isValidZoneBucketUnit("week") // true * @example isValidZoneBucketUnit("month") // true * @example isValidZoneBucketUnit("year") // false (a calendar year is getQuarter's question) * @example isValidZoneBucketUnit("minute") // false (a sub-hour boundary needs no zone) * @example isValidZoneBucketUnit("days") // true (plural name) * @example isValidZoneBucketUnit("years") // false * @example isValidZoneBucketUnit(1) // false * @example isValidZoneBucketUnit(null) // false */ function isValidZoneBucketUnit(unit) { if (typeof unit !== "string") return false; const singular = resolveDateTimeUnit(unit); return singular === "hour" || singular === "day" || singular === "week" || singular === "month"; } export { isValidZoneBucketUnit };