@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
26 lines (25 loc) • 863 B
JavaScript
/**
* 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
*/
export function isValidDateUnit(unit) {
if (typeof unit !== "string") {
return false;
}
return (unit === "year" || unit === "month" || unit === "week" || unit === "day");
}