UNPKG

@date-vir/duration

Version:

Durations units an utils for date-vir.

92 lines (91 loc) 2.29 kB
import { userLocale } from '../locale.js'; /** * Names of all months in English. Don't use this for strings shown to the user, instead use * {@link getMonthNames}. * * @category Unit */ export var MonthName; (function (MonthName) { MonthName["January"] = "january"; MonthName["February"] = "february"; MonthName["March"] = "march"; MonthName["April"] = "april"; MonthName["May"] = "may"; MonthName["June"] = "june"; MonthName["July"] = "july"; MonthName["August"] = "august"; MonthName["September"] = "september"; MonthName["October"] = "october"; MonthName["November"] = "november"; MonthName["December"] = "december"; })(MonthName || (MonthName = {})); /** * All English month names in order. * * @category Unit */ export const orderedMonthNames = [ MonthName.January, MonthName.February, MonthName.March, MonthName.April, MonthName.May, MonthName.June, MonthName.July, MonthName.August, MonthName.September, MonthName.October, MonthName.November, MonthName.December, ]; /** * Bounds for valid month numbers. * * @category Internal */ export const monthNumberBounds = { min: 1, max: 12, }; /** * Bounds for valid day of the month numbers. * * @category Internal */ export const dayOfMonthBounds = { min: 1, max: 31, }; /** * Gets all month names in various formats with the given locale. Defaults to the current user's * locale. * * @category Language */ export function getMonthNames(options = {}) { return { long: getAbbreviatedMonthNames('long', options), short: getAbbreviatedMonthNames('short', options), narrow: getAbbreviatedMonthNames('narrow', options), }; } function getAbbreviatedMonthNames(abbreviation, options) { const formatter = new Intl.DateTimeFormat(options.locale || userLocale, { month: abbreviation, }); const numbered = {}; const keyed = {}; const indexed = {}; for (const [i, orderedMonthName,] of orderedMonthNames.entries()) { const day = formatter.format(new Date(2020, i, 1)); numbered[(i + 1)] = day; keyed[orderedMonthName] = day; indexed[i] = day; } return { numbered, keyed, indexed, }; }