@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.
28 lines (26 loc) • 1.25 kB
JavaScript
import { resolveRequiredLocale } from "./resolveLocale.js";
// src/internal/localeWeekInfo.ts
/**
* A locale's CLDR week data, from whichever form of the Intl Locale Info proposal the runtime
* implements.
*
* - Prefers `Intl.Locale.prototype.getWeekInfo()`, the proposal's current method, and falls back
* to the earlier `weekInfo` accessor on engines that only have that (Node 22). Node 26 removed
* the accessor, so reading it alone silently returned `undefined` there and every locale lost
* its week data.
* - Returns `undefined` when neither form is available. Fields may be missing: Node 24/26 omit
* `minimalDays`. Callers apply their own ISO defaults.
* - Throws `RangeError` (from `Intl.Locale`) if `locale` is not a well-formed BCP 47 tag; callers turn
* that into their sentinel.
*
* @example localeWeekInfo("pt-BR")?.firstDay // 7
* @example localeWeekInfo("ar-SA")?.weekend // [5, 6]
*/
function localeWeekInfo(locale) {
const resolved = resolveRequiredLocale(locale);
if (resolved === null) throw new RangeError("Invalid locale");
const intlLocale = new Intl.Locale(resolved);
if (typeof intlLocale.getWeekInfo === "function") return intlLocale.getWeekInfo();
return intlLocale.weekInfo;
}
export { localeWeekInfo };