@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.
47 lines (46 loc) • 1.67 kB
JavaScript
// src/internal/printsDateField.ts
/** The resolved options that make a formatter print a date field. */
const DATE_FIELDS = [
"dateStyle",
"era",
"year",
"month",
"day"
];
/**
* The part types of a date field. A chinese or dangi `year` prints `relatedYear` and `yearName`,
* and V8 leaves `year` out of `resolvedOptions` for it, so the parts are read too.
*/
const DATE_PART_TYPES = /* @__PURE__ */ new Set([
"era",
"year",
"relatedYear",
"yearName",
"month",
"day"
]);
/** Whether the formatter's parts for one instant hold a year, month, day or era. */
function printsDatePart(formatter) {
try {
return formatter.formatToParts(0).some(({ type }) => DATE_PART_TYPES.has(type));
} catch {
return false;
}
}
/**
* Whether a formatter prints a year, month, day or era: its resolved options carry `dateStyle`,
* `era`, `year`, `month` or `day`, or its parts for one instant hold one of them (`relatedYear`
* and `yearName` count as a year). A time-only or weekday-only formatter prints none: the time of
* day and the seven-day week do not depend on the calendar (ECMA-402 ToLocalTime).
*
* @param formatter the formatter
* @param resolved its `resolvedOptions()`
* @returns `true` when it prints a date field
*
* @example printsDateField(new Intl.DateTimeFormat("en"), new Intl.DateTimeFormat("en").resolvedOptions()) // true
* @example printsDateField(new Intl.DateTimeFormat("en", { weekday: "long" }), new Intl.DateTimeFormat("en", { weekday: "long" }).resolvedOptions()) // false
*/
function printsDateField(formatter, resolved) {
return DATE_FIELDS.some((name) => resolved[name] !== void 0) || printsDatePart(formatter);
}
export { printsDateField };