@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.
61 lines (59 loc) • 2.75 kB
JavaScript
import { Temporal } from "./temporal.js";
import { cachedFormatter } from "./formatterCache.js";
import { getLocaleMonthNames } from "../plain/locale/getLocaleMonthNames.js";
// src/internal/localeMonthNames.ts
const ALL_DIGITS = /^\p{Nd}+$/u;
/**
* Read a locale's month names in the format and the stand-alone context.
*
* - The stand-alone list is {@link getLocaleMonthNames}. The format list comes from
* `formatToParts` on a month-and-day pattern, so the locale picks its own form.
* - A locale that writes the month as a number beside a day (pt-PT short, zh-CN, zh-TW,
* ja-JP) has no format name there, so that month takes its stand-alone name.
* - Returns `null` when one name, compared case-insensitively, names two different months
* across both lists: UTS #35 Part 4, Parsing Dates and Times, matches stand-alone forms
* only "if they are unique".
* - The format formatter forces `numberingSystem: "latn"`: a `-u-nu-` locale such as `hanidec`
* prints a numeric month in letters (一, 二, 一〇) that `\p{Nd}` does not match, so a numeric
* month would be read as a name. With Latin digits it always reaches the stand-alone fallback.
* - Never throws. The options carry no `year`, so no pattern with a year is formatted.
*
* @param locale A resolved BCP 47 tag
* @param width `"long"` or `"short"`
* @returns Both lists, or `null` for an unresolvable locale or an ambiguous name
* @example localeMonthNames("ru-RU", "long") // { format: ["января", …, "марта", …], standAlone: ["январь", …, "март", …] }
* @example localeMonthNames("not-a-locale-!!", "long") // null
*/
function localeMonthNames(locale, width) {
try {
const standAlone = getLocaleMonthNames(locale, width);
if (standAlone.length !== 12) return null;
const formatter = cachedFormatter("DateTimeFormat", locale, {
month: width,
day: "numeric",
calendar: "gregory",
numberingSystem: "latn",
timeZone: "UTC"
});
const format = [];
for (let month = 1; month <= 12; month++) {
const epochMilliseconds = Temporal.PlainDate.from(`2024-${String(month).padStart(2, "0")}-15`).toZonedDateTime("UTC").epochMilliseconds;
const text = formatter.formatToParts(epochMilliseconds).find((candidate) => candidate.type === "month")?.value ?? "";
format.push(text === "" || ALL_DIGITS.test(text) ? standAlone[month - 1] : text);
}
const owner = /* @__PURE__ */ new Map();
for (const list of [format, standAlone]) for (const [index, name] of list.entries()) {
const key = name.toLowerCase();
const seen = owner.get(key);
if (seen !== void 0 && seen !== index) return null;
owner.set(key, index);
}
return {
format,
standAlone
};
} catch {
return null;
}
}
export { localeMonthNames };