@skitterbyte/skitterspec-jira 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,156 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The window a ta-da list covers — local time, because "today" is the
5
+ * reader's day and not UTC's. `--since <when>` runs from that day to now;
6
+ * `--on <when>` is that one day. Both read the same `<when>`:
7
+ *
8
+ * named today · yesterday · friday · last friday · last week
9
+ * relative 3 days ago · 3d · 2 weeks ago · 2w
10
+ * a date 2026-11-06 · 6 Nov · Nov 6 · 6 November 2026
11
+ *
12
+ * Pure: `now` is injected, so every shape is pinned without a clock.
13
+ */
14
+
15
+ const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
16
+ const MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
17
+
18
+ const GRAMMAR = 'today, yesterday, a weekday (last friday), N days/weeks ago (3d, 2w), or a date (2026-11-06, 6 Nov)'
19
+
20
+ function daysBefore(d, n) {
21
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate() - n)
22
+ }
23
+
24
+ // A name or its first three letters; `sept` is accepted for September.
25
+ function lookup(names, word) {
26
+ const w = word.toLowerCase()
27
+ const i = names.findIndex((n) => n === w || n.slice(0, 3) === w)
28
+ if (i !== -1) return i
29
+ return w === 'sept' && names === MONTHS ? 8 : -1
30
+ }
31
+
32
+ function exactDate(year, month, day, value) {
33
+ const d = new Date(year, month, day)
34
+ // `new Date` rolls 31 Feb over into March rather than refusing it.
35
+ if (d.getMonth() !== month || d.getDate() !== day) throw new Error(`no such date: ${value}`)
36
+ return d
37
+ }
38
+
39
+ /**
40
+ * @param {string|undefined} value
41
+ * @param {Date} now
42
+ * @returns {Date} the local midnight the day begins at
43
+ * @throws on a value it cannot read — a window guessed from a typo would
44
+ * report the wrong day with total confidence
45
+ */
46
+ function parseWhen(value, now = new Date()) {
47
+ const raw = (value || 'today').trim()
48
+ const v = raw.toLowerCase().replace(/\s+/g, ' ')
49
+ const today = daysBefore(now, 0)
50
+ let d = null
51
+
52
+ if (v === 'today') d = today
53
+ else if (v === 'yesterday') d = daysBefore(now, 1)
54
+ else if (v === 'last week' || v === 'a week ago') d = daysBefore(now, 7)
55
+
56
+ // A weekday is the most recent PAST one, so it is never more than a week
57
+ // back: `friday` on a Monday is three days back, and `monday` on a Monday is
58
+ // seven — on the day itself, "since monday" meaning today would be `today`
59
+ // spelled oddly. `last friday` says the same thing in more words.
60
+ if (!d) {
61
+ const m = /^(?:last )?([a-z]+)$/.exec(v)
62
+ const day = m ? lookup(WEEKDAYS, m[1]) : -1
63
+ if (day !== -1) d = daysBefore(now, (now.getDay() - day + 7) % 7 || 7)
64
+ }
65
+
66
+ if (!d) {
67
+ const m = /^(\d+|an?) ?(d|days?|w|weeks?)(?: ago)?$/.exec(v) || /^-(\d+)(d|w)$/.exec(v)
68
+ if (m) {
69
+ const n = /^\d+$/.test(m[1]) ? Number(m[1]) : 1
70
+ d = daysBefore(now, m[2].startsWith('w') ? n * 7 : n)
71
+ }
72
+ }
73
+
74
+ if (!d) {
75
+ const iso = /^(\d{4})-(\d{2})-(\d{2})$/.exec(v)
76
+ if (iso) d = exactDate(Number(iso[1]), Number(iso[2]) - 1, Number(iso[3]), raw)
77
+ }
78
+
79
+ if (!d) {
80
+ // `6 Nov`, `Nov 6`, either with a year. Without one it is the most recent
81
+ // such date — `20 Dec` read in January means last December.
82
+ const dm = /^(\d{1,2})(?:st|nd|rd|th)? ([a-z]+),? ?(\d{4})?$/.exec(v)
83
+ const md = /^([a-z]+) (\d{1,2})(?:st|nd|rd|th)?,? ?(\d{4})?$/.exec(v)
84
+ const parts = dm ? { day: dm[1], month: dm[2], year: dm[3] } : md ? { day: md[2], month: md[1], year: md[3] } : null
85
+ const month = parts ? lookup(MONTHS, parts.month) : -1
86
+ if (month !== -1) {
87
+ if (parts.year) d = exactDate(Number(parts.year), month, Number(parts.day), raw)
88
+ else {
89
+ d = exactDate(now.getFullYear(), month, Number(parts.day), raw)
90
+ if (d > today) d = exactDate(now.getFullYear() - 1, month, Number(parts.day), raw)
91
+ }
92
+ }
93
+ }
94
+
95
+ if (!d && /^\d{1,2}[/.]\d{1,2}(?:[/.]\d{2,4})?$/.test(v)) {
96
+ // 11/12 is the 11th of December in London and the 12th of November in New
97
+ // York. Picking one is the confident wrong answer this refuses to give.
98
+ throw new Error(`${raw} reads two ways (day/month or month/day) — write 2026-11-12 or 12 Nov`)
99
+ }
100
+ if (!d) throw new Error(`expected ${GRAMMAR} — got: ${raw}`)
101
+ if (d > today) throw new Error(`${raw} is in the future`)
102
+ return d
103
+ }
104
+
105
+ /**
106
+ * @param {{ since?: string, on?: string }} when
107
+ * @returns {{ since: Date, until: Date }}
108
+ */
109
+ function windowFor({ since, on } = {}, now = new Date()) {
110
+ if (since && on) throw new Error('--since and --on together name two windows — pick one')
111
+ if (on) {
112
+ const start = parseWhen(on, now)
113
+ const end = daysBefore(start, -1)
114
+ return { since: start, until: end < now ? end : now }
115
+ }
116
+ return { since: parseWhen(since, now), until: now }
117
+ }
118
+
119
+ const localDay = (d) =>
120
+ `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
121
+
122
+ /**
123
+ * The window as the reader's own dates — one day, or `first → last`. The
124
+ * digest's timestamps are UTC instants; a label is not, or `--on 10 Nov` read
125
+ * east of Greenwich would be named the 9th.
126
+ */
127
+ function windowLabel({ since, until }) {
128
+ const last = new Date(until.getTime() - 1) // an `--on` window ends at the next midnight, exclusive
129
+ const a = localDay(since)
130
+ const b = localDay(last < since ? since : last)
131
+ return a === b ? a : `${a} → ${b}`
132
+ }
133
+
134
+ const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
135
+ const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
136
+
137
+ /**
138
+ * The window as a person writes dates: `Thursday 12 November 2026` for one
139
+ * day, `Fri 6 – Thu 12 Nov 2026` for a span — the month and year said once
140
+ * when the two ends share them, and both times when they do not.
141
+ */
142
+ function humanLabel({ since, until }) {
143
+ const last = new Date(until.getTime() - 1)
144
+ const end = last < since ? since : last
145
+ if (localDay(since) === localDay(end)) {
146
+ return `${DAY_NAMES[since.getDay()]} ${since.getDate()} ${MONTH_NAMES[since.getMonth()]} ${since.getFullYear()}`
147
+ }
148
+ const short = (d) => `${DAY_NAMES[d.getDay()].slice(0, 3)} ${d.getDate()}`
149
+ const month = (d) => MONTH_NAMES[d.getMonth()].slice(0, 3)
150
+ const sameYear = since.getFullYear() === end.getFullYear()
151
+ const sameMonth = sameYear && since.getMonth() === end.getMonth()
152
+ const from = sameMonth ? short(since) : `${short(since)} ${month(since)}${sameYear ? '' : ` ${since.getFullYear()}`}`
153
+ return `${from} – ${short(end)} ${month(end)} ${end.getFullYear()}`
154
+ }
155
+
156
+ module.exports = { parseWhen, windowFor, windowLabel, humanLabel, localDay, GRAMMAR }
File without changes