@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.
227 lines (225 loc) • 6.9 kB
JavaScript
import { plainDateOf } from "./inputChecks.js";
import { isObject } from "./isObject.js";
// src/internal/unitNames.ts
/**
* The membership checks behind the public unit, cycle-field and fiscal validators, so a public
* function never calls another. Each takes any value and is `false` for a value that is not one
* of the listed names.
*/
const DATE_UNITS = [
"year",
"month",
"week",
"day"
];
const TIME_UNITS = [
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
];
/** The plural date duration unit names, coarsest first. */
const DATE_DURATION_UNITS = [
"years",
"months",
"weeks",
"days"
];
/** The plural time duration unit names, coarsest first. */
const TIME_DURATION_UNITS = [
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds"
];
const DATE_CYCLE_FIELDS = [
"year",
"month",
"day"
];
/**
* The check behind `isValidDateTimeDurationUnit`.
*
* @param unit the candidate
* @returns whether it is a plural date or time duration unit name
*
* @example isDateTimeDurationUnitName("days") // true
* @example isDateTimeDurationUnitName("day") // false
*/
function isDateTimeDurationUnitName(unit) {
return DATE_DURATION_UNITS.includes(unit) || TIME_DURATION_UNITS.includes(unit);
}
/**
* The check behind `isValidDateDurationUnit`.
*
* @param unit the candidate
* @returns whether it is `"years"`, `"months"`, `"weeks"` or `"days"`
*
* @example isDateDurationUnitName("weeks") // true
* @example isDateDurationUnitName("hours") // false
*/
function isDateDurationUnitName(unit) {
return DATE_DURATION_UNITS.includes(unit);
}
/**
* The check behind `isValidTimeDurationUnit`.
*
* @param unit the candidate
* @returns whether it is a plural time duration unit name, `"hours"` to `"nanoseconds"`
*
* @example isTimeDurationUnitName("hours") // true
* @example isTimeDurationUnitName("days") // false
*/
function isTimeDurationUnitName(unit) {
return TIME_DURATION_UNITS.includes(unit);
}
/**
* The check behind `isValidDateTimeUnit`.
*
* @param unit the candidate
* @returns whether it is a singular date or time unit name
*
* @example isDateTimeUnitName("day") // true
* @example isDateTimeUnitName("days") // false
*/
function isDateTimeUnitName(unit) {
return DATE_UNITS.includes(unit) || TIME_UNITS.includes(unit);
}
/**
* The check behind `isValidDateUnit`.
*
* @param unit the candidate
* @returns whether it is `"year"`, `"month"`, `"week"` or `"day"`
*
* @example isDateUnitName("week") // true
* @example isDateUnitName("hour") // false
*/
function isDateUnitName(unit) {
return DATE_UNITS.includes(unit);
}
/**
* The check behind `isValidTimeUnit`.
*
* @param unit the candidate
* @returns whether it is a singular time unit name, `"hour"` to `"nanosecond"`
*
* @example isTimeUnitName("hour") // true
* @example isTimeUnitName("day") // false
*/
function isTimeUnitName(unit) {
return TIME_UNITS.includes(unit);
}
/**
* The check behind `isValidDateCycleField`.
*
* @param field the candidate
* @returns whether it is `"year"`, `"month"` or `"day"`
*
* @example isDateCycleFieldName("month") // true
* @example isDateCycleFieldName("week") // false
*/
function isDateCycleFieldName(field) {
return DATE_CYCLE_FIELDS.includes(field);
}
/**
* The check behind `isValidTimeCycleField`.
*
* @param field the candidate
* @returns whether it is a singular time unit name, `"hour"` to `"nanosecond"`
*
* @example isTimeCycleFieldName("minute") // true
* @example isTimeCycleFieldName("day") // false
*/
function isTimeCycleFieldName(field) {
return TIME_UNITS.includes(field);
}
/**
* The check behind `isValidDateTimeCycleField`.
*
* @param field the candidate
* @returns whether it is a date or a time cycle field name
*
* @example isDateTimeCycleFieldName("hour") // true
* @example isDateTimeCycleFieldName("week") // false
*/
function isDateTimeCycleFieldName(field) {
return isDateCycleFieldName(field) || isTimeCycleFieldName(field);
}
/** The singular names a zone bucket takes. */
function isZoneBucketBase(unit) {
return unit === "hour" || unit === "day" || unit === "week" || unit === "month";
}
/**
* The check behind `isValidZoneBucketUnit`: `"hour"`, `"day"`, `"week"` or `"month"`, or the
* plural of one (a trailing `s`, as `resolveDateTimeUnit` reads it).
*
* @param unit the candidate
* @returns whether it names a zone bucket
*
* @example isZoneBucketUnitName("weeks") // true
* @example isZoneBucketUnitName("year") // false
*/
function isZoneBucketUnitName(unit) {
if (typeof unit !== "string") return false;
return isZoneBucketBase(unit) || unit.endsWith("s") && isZoneBucketBase(unit.slice(0, -1));
}
/**
* The check behind `isValidFiscalPattern`.
*
* @param pattern the candidate
* @returns whether it is `"4-5-4"`, `"4-4-5"` or `"5-4-4"`
*
* @example isFiscalPatternName("4-5-4") // true
* @example isFiscalPatternName("4-5-5") // false
*/
function isFiscalPatternName(pattern) {
return pattern === "4-5-4" || pattern === "4-4-5" || pattern === "5-4-4";
}
/**
* The check behind `isValidFiscalExtraWeek`.
*
* @param value the candidate
* @returns whether it is `"firstPeriod"` or `"lastPeriod"`
*
* @example isFiscalExtraWeekName("lastPeriod") // true
* @example isFiscalExtraWeekName("none") // false
*/
function isFiscalExtraWeekName(value) {
return value === "firstPeriod" || value === "lastPeriod";
}
/**
* The check behind `isValidFiscalYearLabel`.
*
* @param value the candidate
* @returns whether it is `"startYear"`, `"endYear"` or `"majorityYear"`
*
* @example isFiscalYearLabelName("endYear") // true
* @example isFiscalYearLabelName("year") // false
*/
function isFiscalYearLabelName(value) {
return value === "startYear" || value === "endYear" || value === "majorityYear";
}
/**
* The check behind `isValidFiscalCalendar`: a plain object with a fiscal pattern, an ISO
* `yearEndsOn` date, an extra-week rule and a year label. Never throws.
*
* @param calendar the candidate
* @returns whether it is a `FiscalCalendar`
*
* @example isFiscalCalendarRecord({ pattern: "4-5-4", yearEndsOn: "2024-02-03", extraWeek: "lastPeriod", yearLabel: "endYear" }) // true
* @example isFiscalCalendarRecord(null) // false
*/
function isFiscalCalendarRecord(calendar) {
try {
if (!isObject(calendar) || Array.isArray(calendar)) return false;
const { pattern, yearEndsOn, extraWeek, yearLabel } = calendar;
return isFiscalPatternName(pattern) && typeof yearEndsOn === "string" && plainDateOf(yearEndsOn) !== null && isFiscalExtraWeekName(extraWeek) && isFiscalYearLabelName(yearLabel);
} catch {
return false;
}
}
export { DATE_DURATION_UNITS, TIME_DURATION_UNITS, isDateCycleFieldName, isDateDurationUnitName, isDateTimeCycleFieldName, isDateTimeDurationUnitName, isDateTimeUnitName, isDateUnitName, isFiscalCalendarRecord, isFiscalExtraWeekName, isFiscalPatternName, isFiscalYearLabelName, isTimeCycleFieldName, isTimeDurationUnitName, isTimeUnitName, isZoneBucketUnitName };