// typeddate.mjs — typed dates, a line-for-line port of // internal/mapa/typeddate.go's typedDate (level-up §4.6), for the date // picker (build note 83): "today", "yesterday", "tomorrow", a weekday // ("friday" is the most recent Friday, today included; "next friday" // the coming one), "12 nov" or "nov 12" (this year), and ISO dates. // The words are English; a date in numbers is read in the reader's // locale's order (build note 85): 12/11 is 12 November in en-IE and // December 11 in en-US. internal/mapa/testdata/typed_dates.json holds the cases // both must answer alike (Go's TestTypedDatesMatchTheSharedCases and // typeddate.test.mjs). import { isDate } from './markup.mjs' const weekdays = { sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sun: 0, mon: 1, tue: 2, tues: 2, wed: 3, thu: 4, thur: 4, thurs: 4, fri: 5, sat: 6 } const months = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, sept: 9, oct: 10, nov: 11, dec: 12, january: 1, february: 2, march: 3, april: 4, june: 6, july: 7, august: 8, september: 9, october: 10, november: 11, december: 12 } // Go's strings.Fields splits on unicode.IsSpace, which is not quite // JavaScript's \s: U+0085 is a space to Go, and U+FEFF is not. const space = /[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/u // lower is Go's strings.ToLower where it matters here: İ lowers to a // plain i in Go, and to i with a dot above in JavaScript. const lower = text => text.replace(/\u0130/gu, 'i').toLowerCase() const hasOwn = (table, key) => Object.hasOwn(table, key) // utc is a day as Go's time.Date makes it: out-of-range days roll over. function utc (year, month, day) { const date = new Date(0) date.setUTCFullYear(year, month - 1, day) return date } export function iso (date) { const year = date.getUTCFullYear() if (year < 0 || year > 9999) return '' const pad = (value, width = 2) => String(value).padStart(width, '0') return `${pad(year, 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}` } function parseISO (text) { if (!isDate(text)) return null const [year, month, day] = text.split('-').map(Number) return utc(year, month, day) } function addDays (base, days) { return utc(base.getUTCFullYear(), base.getUTCMonth() + 1, base.getUTCDate() + days) } // typedDate is the day query names, as an ISO date, counted from today // (an ISO date in the viewer's zone), reading numbers in order ("dmy", // "mdy" or "ymd", dates.mjs's order()); '' when it names none. With a // separator, a date in numbers must use it unless its year comes first // (typeddate.go); the picker passes none. export function typedDate (query, today, order = 'dmy', separator = '') { const base = parseISO(today) if (!base) return '' const words = lower(query).split(space).filter(word => word !== '') if (words.length === 1) return oneWordDate(words[0], base, order, separator) if (words.length === 2) return twoWordDate(words[0], words[1], base) return '' } function oneWordDate (text, base, order, separator) { switch (text) { case 'today': return iso(base) case 'yesterday': return iso(addDays(base, -1)) case 'tomorrow': return iso(addDays(base, 1)) } if (hasOwn(weekdays, text)) { const back = (base.getUTCDay() - weekdays[text] + 7) % 7 return iso(addDays(base, -back)) } return isDate(text) ? text : numericDate(text, base, order, separator) } // numericDate is a day in numbers, split by / or by . as the locale // writes it: day and month in order's order, this year unless a // four-digit year follows; 12.11. as German ends it. A four-digit year // first is year, month, day anywhere. Anything else is none. function numericDate (text, base, order, only) { const separator = text.includes('/') ? '/' : '.' let parts = text.split(separator) if (separator === '.' && parts.length === 3 && parts[2] === '') parts = parts.slice(0, 2) if (parts.length < 2 || parts.length > 3 || !parts.every(part => /^[0-9]+$/u.test(part))) return '' const yearFirst = parts.length === 3 && parts[0].length === 4 if (only !== '' && separator !== only && !yearFirst) return '' let [year, month, day] = [String(base.getUTCFullYear()), parts[0], parts[1]] if (yearFirst) [year, month, day] = parts else if (parts.length === 3 && order === 'dmy') [day, month, year] = parts else if (parts.length === 3 && order === 'mdy') year = parts[2] else if (parts.length === 3) return '' else if (order === 'dmy') [day, month] = parts return calendarDate(year, month, day) } // calendarDate is the ISO date for a four-digit year and a month and a // day of one or two digits, if that day exists. function calendarDate (year, month, day) { if (year.length !== 4 || month.length > 2 || day.length > 2) return '' const [whole, number, date] = [year, month, day].map(Number) const at = utc(whole, number, date) if (number < 1 || number > 12 || at.getUTCDate() !== date || at.getUTCMonth() + 1 !== number) return '' return iso(at) } // twoWordDate is "next friday", "12 nov" or "nov 12". function twoWordDate (first, second, base) { if (hasOwn(weekdays, second) && first === 'next') { const ahead = (weekdays[second] - base.getUTCDay() + 7) % 7 || 7 return iso(addDays(base, ahead)) } if (hasOwn(months, first)) [first, second] = [second, first] // Go's strconv.Atoi: ASCII digits after an optional sign. if (!hasOwn(months, second) || !/^[+-]?[0-9]+$/u.test(first)) return '' const month = months[second] const number = Number(first) if (number < 1 || number > 31) return '' const day = utc(base.getUTCFullYear(), month, number) return day.getUTCMonth() + 1 === month ? iso(day) : '' }