| 1 | // typeddate.mjs — typed dates, a line-for-line port of |
| 2 | // internal/mapa/typeddate.go's typedDate (level-up §4.6), for the date |
| 3 | // picker (build note 83): "today", "yesterday", "tomorrow", a weekday |
| 4 | // ("friday" is the most recent Friday, today included; "next friday" |
| 5 | // the coming one), "12 nov" or "nov 12" (this year), and ISO dates. |
| 6 | // The words are English; a date in numbers is read in the reader's |
| 7 | // locale's order (build note 85): 12/11 is 12 November in en-IE and |
| 8 | // December 11 in en-US. internal/mapa/testdata/typed_dates.json holds the cases |
| 9 | // both must answer alike (Go's TestTypedDatesMatchTheSharedCases and |
| 10 | // typeddate.test.mjs). |
| 11 | |
| 12 | import { isDate } from './markup.mjs' |
| 13 | |
| 14 | const weekdays = { |
| 15 | sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, |
| 16 | sun: 0, mon: 1, tue: 2, tues: 2, wed: 3, thu: 4, thur: 4, thurs: 4, fri: 5, sat: 6 |
| 17 | } |
| 18 | |
| 19 | const months = { |
| 20 | jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, sept: 9, |
| 21 | oct: 10, nov: 11, dec: 12, january: 1, february: 2, march: 3, april: 4, june: 6, |
| 22 | july: 7, august: 8, september: 9, october: 10, november: 11, december: 12 |
| 23 | } |
| 24 | |
| 25 | // Go's strings.Fields splits on unicode.IsSpace, which is not quite |
| 26 | // JavaScript's \s: U+0085 is a space to Go, and U+FEFF is not. |
| 27 | const space = /[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/u |
| 28 | |
| 29 | // lower is Go's strings.ToLower where it matters here: İ lowers to a |
| 30 | // plain i in Go, and to i with a dot above in JavaScript. |
| 31 | const lower = text => text.replace(/\u0130/gu, 'i').toLowerCase() |
| 32 | |
| 33 | const hasOwn = (table, key) => Object.hasOwn(table, key) |
| 34 | |
| 35 | // utc is a day as Go's time.Date makes it: out-of-range days roll over. |
| 36 | function utc (year, month, day) { |
| 37 | const date = new Date(0) |
| 38 | date.setUTCFullYear(year, month - 1, day) |
| 39 | return date |
| 40 | } |
| 41 | |
| 42 | export function iso (date) { |
| 43 | const year = date.getUTCFullYear() |
| 44 | if (year < 0 || year > 9999) return '' |
| 45 | const pad = (value, width = 2) => String(value).padStart(width, '0') |
| 46 | return `${pad(year, 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}` |
| 47 | } |
| 48 | |
| 49 | function parseISO (text) { |
| 50 | if (!isDate(text)) return null |
| 51 | const [year, month, day] = text.split('-').map(Number) |
| 52 | return utc(year, month, day) |
| 53 | } |
| 54 | |
| 55 | function addDays (base, days) { |
| 56 | return utc(base.getUTCFullYear(), base.getUTCMonth() + 1, base.getUTCDate() + days) |
| 57 | } |
| 58 | |
| 59 | // typedDate is the day query names, as an ISO date, counted from today |
| 60 | // (an ISO date in the viewer's zone), reading numbers in order ("dmy", |
| 61 | // "mdy" or "ymd", dates.mjs's order()); '' when it names none. With a |
| 62 | // separator, a date in numbers must use it unless its year comes first |
| 63 | // (typeddate.go); the picker passes none. |
| 64 | export function typedDate (query, today, order = 'dmy', separator = '') { |
| 65 | const base = parseISO(today) |
| 66 | if (!base) return '' |
| 67 | const words = lower(query).split(space).filter(word => word !== '') |
| 68 | if (words.length === 1) return oneWordDate(words[0], base, order, separator) |
| 69 | if (words.length === 2) return twoWordDate(words[0], words[1], base) |
| 70 | return '' |
| 71 | } |
| 72 | |
| 73 | function oneWordDate (text, base, order, separator) { |
| 74 | switch (text) { |
| 75 | case 'today': return iso(base) |
| 76 | case 'yesterday': return iso(addDays(base, -1)) |
| 77 | case 'tomorrow': return iso(addDays(base, 1)) |
| 78 | } |
| 79 | if (hasOwn(weekdays, text)) { |
| 80 | const back = (base.getUTCDay() - weekdays[text] + 7) % 7 |
| 81 | return iso(addDays(base, -back)) |
| 82 | } |
| 83 | return isDate(text) ? text : numericDate(text, base, order, separator) |
| 84 | } |
| 85 | |
| 86 | // numericDate is a day in numbers, split by / or by . as the locale |
| 87 | // writes it: day and month in order's order, this year unless a |
| 88 | // four-digit year follows; 12.11. as German ends it. A four-digit year |
| 89 | // first is year, month, day anywhere. Anything else is none. |
| 90 | function numericDate (text, base, order, only) { |
| 91 | const separator = text.includes('/') ? '/' : '.' |
| 92 | let parts = text.split(separator) |
| 93 | if (separator === '.' && parts.length === 3 && parts[2] === '') parts = parts.slice(0, 2) |
| 94 | if (parts.length < 2 || parts.length > 3 || !parts.every(part => /^[0-9]+$/u.test(part))) return '' |
| 95 | const yearFirst = parts.length === 3 && parts[0].length === 4 |
| 96 | if (only !== '' && separator !== only && !yearFirst) return '' |
| 97 | let [year, month, day] = [String(base.getUTCFullYear()), parts[0], parts[1]] |
| 98 | if (yearFirst) [year, month, day] = parts |
| 99 | else if (parts.length === 3 && order === 'dmy') [day, month, year] = parts |
| 100 | else if (parts.length === 3 && order === 'mdy') year = parts[2] |
| 101 | else if (parts.length === 3) return '' |
| 102 | else if (order === 'dmy') [day, month] = parts |
| 103 | return calendarDate(year, month, day) |
| 104 | } |
| 105 | |
| 106 | // calendarDate is the ISO date for a four-digit year and a month and a |
| 107 | // day of one or two digits, if that day exists. |
| 108 | function calendarDate (year, month, day) { |
| 109 | if (year.length !== 4 || month.length > 2 || day.length > 2) return '' |
| 110 | const [whole, number, date] = [year, month, day].map(Number) |
| 111 | const at = utc(whole, number, date) |
| 112 | if (number < 1 || number > 12 || at.getUTCDate() !== date || at.getUTCMonth() + 1 !== number) return '' |
| 113 | return iso(at) |
| 114 | } |
| 115 | |
| 116 | // twoWordDate is "next friday", "12 nov" or "nov 12". |
| 117 | function twoWordDate (first, second, base) { |
| 118 | if (hasOwn(weekdays, second) && first === 'next') { |
| 119 | const ahead = (weekdays[second] - base.getUTCDay() + 7) % 7 || 7 |
| 120 | return iso(addDays(base, ahead)) |
| 121 | } |
| 122 | if (hasOwn(months, first)) [first, second] = [second, first] |
| 123 | // Go's strconv.Atoi: ASCII digits after an optional sign. |
| 124 | if (!hasOwn(months, second) || !/^[+-]?[0-9]+$/u.test(first)) return '' |
| 125 | const month = months[second] |
| 126 | const number = Number(first) |
| 127 | if (number < 1 || number > 31) return '' |
| 128 | const day = utc(base.getUTCFullYear(), month, number) |
| 129 | return day.getUTCMonth() + 1 === month ? iso(day) : '' |
| 130 | } |
| 131 | |