| 1 | // gen-dates.mjs — make locale-dates: writes internal/mapa/locale/ |
| 2 | // dates.json, the CLDR date data Mapa formats with (level-up build note |
| 3 | // 85). rastrillo's locale packs carry CLDR's number and money data but |
| 4 | // no dates, so Mapa carries its own, for the locales it offers. |
| 5 | // |
| 6 | // The data is read out of the ICU this Node links, which is CLDR (the |
| 7 | // file records which versions), one pattern per kind of date Mapa |
| 8 | // shows: a CLDR skeleton, given to Intl.DateTimeFormat, comes back as |
| 9 | // its pattern in formatToParts, and the names each pattern uses are |
| 10 | // read through that same pattern, so a language that declines a month |
| 11 | // after a day gets the declined form there and only there. Nothing |
| 12 | // reads Intl at run time: Go (internal/mapa/locale) and the editor |
| 13 | // (dates.mjs) both interpret these patterns, held to each other by |
| 14 | // internal/mapa/testdata/date_labels.json. |
| 15 | // |
| 16 | // Before writing, every pattern is drawn by this file's interpreter |
| 17 | // (the one dates.mjs exports) for every day of 2024 to 2026 and every |
| 18 | // minute pair that matters, and compared with Intl; any difference |
| 19 | // fails the run, so the file never holds a pattern that misreads ICU. |
| 20 | |
| 21 | import { writeFileSync } from 'node:fs' |
| 22 | import { dirname, join } from 'node:path' |
| 23 | import { fileURLToPath } from 'node:url' |
| 24 | import { formatWith } from './dates.mjs' |
| 25 | |
| 26 | const here = dirname(fileURLToPath(import.meta.url)) |
| 27 | const out = join(here, '..', 'internal', 'mapa', 'locale', 'dates.json') |
| 28 | |
| 29 | // The locales Mapa offers, in the order Accept-Language falls back |
| 30 | // through them after the team's own (locale.go). |
| 31 | const offered = ['en-IE', 'en-GB', 'en-US', 'de', 'fr', 'es'] |
| 32 | |
| 33 | // kinds are the dates Mapa shows, as Intl options (CLDR skeletons). |
| 34 | const kinds = { |
| 35 | day: { weekday: 'long', day: 'numeric', month: 'long' }, |
| 36 | dayYear: { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }, |
| 37 | short: { weekday: 'short', day: 'numeric', month: 'short' }, |
| 38 | date: { day: 'numeric', month: 'long' }, |
| 39 | dateYear: { day: 'numeric', month: 'long', year: 'numeric' }, |
| 40 | monthYear: { month: 'long', year: 'numeric' }, |
| 41 | month: { month: 'long' }, |
| 42 | year: { year: 'numeric' }, |
| 43 | // CLDR's own short time and long date: as skeletons, hour: 'numeric' |
| 44 | // would come back unpadded (9:07) where CLDR writes 09:07. |
| 45 | time: { timeStyle: 'short' }, |
| 46 | stamp: { dateStyle: 'long', timeStyle: 'short' } |
| 47 | } |
| 48 | |
| 49 | const at = (year, month, day, hour = 0, minute = 0) => |
| 50 | new Date(Date.UTC(year, month - 1, day, hour, minute)) |
| 51 | |
| 52 | function formatter (locale, options) { |
| 53 | return new Intl.DateTimeFormat(locale, { timeZone: 'UTC', ...options }) |
| 54 | } |
| 55 | |
| 56 | // fieldOf is one formatToParts part as a pattern field. A sample whose |
| 57 | // day, month and hour are single digits shows whether each is padded. |
| 58 | function fieldOf (part, hourCycle) { |
| 59 | switch (part.type) { |
| 60 | case 'literal': return { t: 'lit', v: part.value } |
| 61 | case 'weekday': return { t: 'weekday' } |
| 62 | case 'year': return { t: 'year' } |
| 63 | case 'dayPeriod': return { t: 'period' } |
| 64 | case 'minute': return { t: 'minute' } |
| 65 | case 'day': return { t: 'day', pad: part.value.length === 2 } |
| 66 | case 'month': |
| 67 | return /^[0-9]+$/u.test(part.value) |
| 68 | ? { t: 'monthNumber', pad: part.value.length === 2 } |
| 69 | : { t: 'month' } |
| 70 | case 'hour': return { t: 'hour', pad: part.value.length === 2, cycle: hourCycle } |
| 71 | } |
| 72 | throw new Error(`no field for ${part.type}`) |
| 73 | } |
| 74 | |
| 75 | function namesOf (format, type, dates) { |
| 76 | return dates.map(date => format.formatToParts(date).find(part => part.type === type).value) |
| 77 | } |
| 78 | |
| 79 | // patternOf is one kind's pattern for a locale, with the names it uses. |
| 80 | function patternOf (locale, options) { |
| 81 | const format = formatter(locale, options) |
| 82 | const cycle = format.resolvedOptions().hourCycle ?? '' |
| 83 | const parts = format.formatToParts(at(2025, 1, 5, 9, 7)).map(part => fieldOf(part, cycle)) |
| 84 | const pattern = { parts } |
| 85 | const types = new Set(parts.map(part => part.t)) |
| 86 | // 4 to 10 January 2026 run Sunday to Saturday. |
| 87 | if (types.has('weekday')) pattern.weekdays = namesOf(format, 'weekday', [4, 5, 6, 7, 8, 9, 10].map(day => at(2026, 1, day))) |
| 88 | if (types.has('month')) pattern.months = namesOf(format, 'month', [...Array(12)].map((_, month) => at(2026, month + 1, 15))) |
| 89 | if (types.has('period')) pattern.periods = namesOf(format, 'dayPeriod', [at(2026, 1, 1, 9), at(2026, 1, 1, 21)]) |
| 90 | return pattern |
| 91 | } |
| 92 | |
| 93 | // check draws every pattern for 2024 to 2026 and compares it with Intl. |
| 94 | function check (locale, kind, pattern) { |
| 95 | const format = formatter(locale, kinds[kind]) |
| 96 | const times = kind === 'time' || kind === 'stamp' |
| 97 | ? [[0, 0], [0, 5], [9, 7], [11, 59], [12, 0], [13, 30], [21, 9], [23, 59]] |
| 98 | : [[0, 0]] |
| 99 | for (let day = at(2024, 1, 1); day < at(2027, 1, 1); day = new Date(day.getTime() + 864e5)) { |
| 100 | for (const [hour, minute] of times) { |
| 101 | const moment = new Date(day.getTime() + (hour * 60 + minute) * 6e4) |
| 102 | const fields = { |
| 103 | year: moment.getUTCFullYear(), month: moment.getUTCMonth() + 1, day: moment.getUTCDate(), |
| 104 | weekday: moment.getUTCDay(), hour, minute |
| 105 | } |
| 106 | const ours = formatWith(pattern, fields) |
| 107 | // formatToParts, not format: V8's format swaps CLDR's U+202F |
| 108 | // before AM and PM for a plain space, for old pages' sake, and |
| 109 | // the patterns are CLDR's. |
| 110 | const theirs = format.formatToParts(moment).map(part => part.value).join('') |
| 111 | if (ours !== theirs) throw new Error(`${locale} ${kind}: ${JSON.stringify(ours)} where ICU writes ${JSON.stringify(theirs)}`) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | function standalone (locale, width) { |
| 117 | const format = formatter(locale, { weekday: width }) |
| 118 | return [4, 5, 6, 7, 8, 9, 10].map(day => format.format(at(2026, 1, day))) |
| 119 | } |
| 120 | |
| 121 | // orderOf is the numeric date's order, "dmy", "mdy" or "ymd", which is |
| 122 | // how a typed 12/11 is read (typeddate.go). |
| 123 | function orderOf (locale) { |
| 124 | const parts = formatter(locale, { day: 'numeric', month: 'numeric', year: 'numeric' }) |
| 125 | .formatToParts(at(2025, 1, 5)) |
| 126 | return parts.filter(part => part.type !== 'literal').map(part => part.type[0]).join('') |
| 127 | } |
| 128 | |
| 129 | // separatorOf is the mark between that date's numbers: "/", or "." in |
| 130 | // German. ⌘K reads a date in numbers only with it (typeddate.go). |
| 131 | function separatorOf (locale) { |
| 132 | const parts = formatter(locale, { day: 'numeric', month: 'numeric', year: 'numeric' }) |
| 133 | .formatToParts(at(2025, 1, 5)) |
| 134 | return parts.find(part => part.type === 'literal').value.trim() |
| 135 | } |
| 136 | |
| 137 | function localeData (locale) { |
| 138 | const info = new Intl.Locale(locale).getWeekInfo() |
| 139 | const digits = new Intl.NumberFormat(locale).resolvedOptions().numberingSystem |
| 140 | if (digits !== 'latn') throw new Error(`${locale} writes ${digits} digits; dates.mjs writes latn only`) |
| 141 | const patterns = {} |
| 142 | for (const [kind, options] of Object.entries(kinds)) { |
| 143 | patterns[kind] = patternOf(locale, options) |
| 144 | check(locale, kind, patterns[kind]) |
| 145 | } |
| 146 | return { |
| 147 | // firstDay is Go's time.Weekday: 0 is Sunday. Intl counts 1 to 7 |
| 148 | // from Monday. |
| 149 | firstDay: info.firstDay % 7, |
| 150 | order: orderOf(locale), |
| 151 | separator: separatorOf(locale), |
| 152 | weekdays: standalone(locale, 'long'), |
| 153 | narrow: standalone(locale, 'narrow'), |
| 154 | patterns |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // generate is dates.json's data, from the ICU this Node links. |
| 159 | export function generate () { |
| 160 | return { |
| 161 | about: 'CLDR date patterns for the locales Mapa offers, read from ICU by editor/gen-dates.mjs (make locale-dates). Weekday lists run Sunday first; firstDay is Go\'s time.Weekday. Do not edit by hand.', |
| 162 | source: { icu: process.versions.icu, cldr: process.versions.cldr, unicode: process.versions.unicode }, |
| 163 | offered, |
| 164 | locales: Object.fromEntries(offered.map(locale => [locale, localeData(locale)])) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // encode is the file's bytes. |
| 169 | export function encode (data) { |
| 170 | return JSON.stringify(data, null, 1) + '\n' |
| 171 | } |
| 172 | |
| 173 | if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 174 | const data = generate() |
| 175 | writeFileSync(out, encode(data)) |
| 176 | console.log(`wrote ${out}: ${offered.join(', ')} (CLDR ${data.source.cldr})`) |
| 177 | } |
| 178 | |