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.

26 lines (25 loc) • 883 B
// src/plain/validate/isValidDateUnit.ts /** * Return true when `unit` is a valid DateUnit. * * - Valid units are: "year", "month", "week", "day". * - Accepts any input type and returns false for non-string values. * - Uses type assertion to narrow the type. * * @param unit candidate value of any type * @returns boolean indicating validity * * @example isValidDateUnit("year") // true * @example isValidDateUnit("month") // true * @example isValidDateUnit("week") // true * @example isValidDateUnit("day") // true * @example isValidDateUnit("hour") // false * @example isValidDateUnit("invalid") // false * @example isValidDateUnit(123) // false * @example isValidDateUnit(null) // false */ function isValidDateUnit(unit) { if (typeof unit !== "string") return false; return unit === "year" || unit === "month" || unit === "week" || unit === "day"; } export { isValidDateUnit };