oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// dates.mjs — dates in the reader's locale (level-up build note 85). The
2// server picks the locale (rastrillo's middleware, Accept-Language, then
3// MAPA_LOCALE) and puts its CLDR patterns in the editor's island as
4// "dates"; page.mjs hands them to useDates before anything is drawn.
5// formatWith is the same interpreter internal/mapa/locale's format.go
6// is, held to it by internal/mapa/testdata/date_labels.json, so a chip
7// the editor labels reads exactly as the server's. Intl is never asked:
8// its patterns are the browser's ICU, which need not be the CLDR Mapa
9// ships (gen-dates.mjs reads that once, at build time).
10
11let current = null
12
13// useDates sets the locale's data: the island's "dates".
14export function useDates (data) {
15 current = data ?? null
16}
17
18export function datesInUse () {
19 return current
20}
21
22const pad = (value, wanted) => wanted ? String(value).padStart(2, '0') : String(value)
23
24function hourOf (hour, cycle) {
25 switch (cycle) {
26 case 'h12': return hour % 12 === 0 ? 12 : hour % 12
27 case 'h11': return hour % 12
28 case 'h24': return hour === 0 ? 24 : hour
29 }
30 return hour
31}
32
33// formatWith draws one pattern for fields: year, month (1 to 12), day,
34// weekday (0 is Sunday), hour and minute.
35export function formatWith (pattern, fields) {
36 let out = ''
37 for (const part of pattern.parts) {
38 switch (part.t) {
39 case 'lit': out += part.v; break
40 case 'weekday': out += pattern.weekdays[fields.weekday]; break
41 case 'month': out += pattern.months[fields.month - 1]; break
42 case 'monthNumber': out += pad(fields.month, part.pad); break
43 case 'day': out += pad(fields.day, part.pad); break
44 case 'year': out += String(fields.year); break
45 case 'hour': out += pad(hourOf(fields.hour, part.cycle), part.pad); break
46 case 'minute': out += pad(fields.minute, true); break
47 case 'period': out += pattern.periods[fields.hour < 12 ? 0 : 1]; break
48 }
49 }
50 return out
51}
52
53// fieldsOf is an ISO date's fields.
54export function fieldsOf (iso) {
55 const [year, month, day] = iso.split('-').map(Number)
56 const date = new Date(0)
57 date.setUTCFullYear(year, month - 1, day)
58 return { year, month, day, weekday: date.getUTCDay(), hour: 0, minute: 0 }
59}
60
61// format is one kind of date (dates.json's kinds) for an ISO date, in
62// data's locale; the ISO date itself when there is no data.
63export function format (kind, iso, data = current) {
64 const pattern = data?.patterns?.[kind]
65 return pattern ? formatWith(pattern, fieldsOf(iso)) : iso
66}
67
68// dayLabel is locale.Locale.Day: "Thursday 12 November" in en-GB, with
69// the year when it is not thisYear.
70export function dayLabel (iso, thisYear = new Date().getFullYear(), data = current) {
71 return format(Number(iso.slice(0, 4)) === thisYear ? 'day' : 'dayYear', iso, data)
72}
73
74// monthTitle is the month calendar's title: "November 2026".
75export function monthTitle (iso, data = current) {
76 return format('monthYear', `${iso.slice(0, 7)}-01`, data)
77}
78
79// firstDay is the locale's first day of the week, 0 for Sunday; Monday
80// when there is no data.
81export function firstDay (data = current) {
82 return data?.firstDay ?? 1
83}
84
85// weekHeads are the week's columns from its first day: the day's name
86// and its narrow letter.
87export function weekHeads (data = current) {
88 const english = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
89 const names = data?.weekdays ?? english
90 const letters = data?.narrow ?? english.map(name => name[0])
91 return Array.from({ length: 7 }, (_, n) => {
92 const day = (firstDay(data) + n) % 7
93 return { name: names[day], letter: letters[day] }
94 })
95}
96
97// order is how a numeric date is read: "dmy", "mdy" or "ymd".
98export function order (data = current) {
99 return data?.order ?? 'dmy'
100}
101