@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) • 902 B
JavaScript
// src/plain/validate/isValidTimeUnit.ts
/**
* Return true when `unit` is a valid TimeUnit.
*
* - Valid units are: "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 isValidTimeUnit("hour") // true
* @example isValidTimeUnit("minute") // true
* @example isValidTimeUnit("second") // true
* @example isValidTimeUnit("invalid") // false
* @example isValidTimeUnit(123) // false
* @example isValidTimeUnit(null) // false
*/
function isValidTimeUnit(unit) {
if (typeof unit !== "string") return false;
return unit === "hour" || unit === "minute" || unit === "second" || unit === "millisecond" || unit === "microsecond" || unit === "nanosecond";
}
export { isValidTimeUnit };