@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.
24 lines (23 loc) • 1.05 kB
JavaScript
// src/plain/validate/isValidDateTimeUnit.ts
/**
* Return true when `unit` is a valid DateTimeUnit.
*
* - Valid units are: "year", "month", "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond".
* - 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 isValidDateTimeUnit("year") // true
* @example isValidDateTimeUnit("month") // true
* @example isValidDateTimeUnit("weeks") // false
* @example isValidDateTimeUnit("invalid") // false
* @example isValidDateTimeUnit(123) // false
* @example isValidDateTimeUnit(null) // false
*/
function isValidDateTimeUnit(unit) {
if (typeof unit !== "string") return false;
return unit === "year" || unit === "month" || unit === "week" || unit === "day" || unit === "hour" || unit === "minute" || unit === "second" || unit === "millisecond" || unit === "microsecond" || unit === "nanosecond";
}
export { isValidDateTimeUnit };