@thekestongroup/ui 0.2.0 → 0.3.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.
- package/README.md +90 -32
- package/dist/components/Age/Age.vue.d.ts +19 -0
- package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
- package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
- package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
- package/dist/components/Badge/Badge.vue.d.ts +41 -0
- package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
- package/dist/components/Button/Button.vue.d.ts +69 -0
- package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
- package/dist/components/Card/Card.vue.d.ts +56 -0
- package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
- package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
- package/dist/components/Chip/Chip.vue.d.ts +44 -0
- package/dist/components/Divider/Divider.vue.d.ts +36 -0
- package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
- package/dist/components/FormField/FormField.vue.d.ts +61 -0
- package/dist/components/Header/Header.vue.d.ts +104 -0
- package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
- package/dist/components/Input/Input.vue.d.ts +56 -0
- package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
- package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
- package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
- package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
- package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
- package/dist/components/Link/Link.vue.d.ts +39 -0
- package/dist/components/Notification/Notification.vue.d.ts +62 -0
- package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
- package/dist/components/Notification/Toaster.vue.d.ts +51 -0
- package/dist/components/Notification/notification.samples.d.ts +3 -0
- package/dist/components/Notification/notifications.d.ts +115 -0
- package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
- package/dist/components/Popover/Popover.vue.d.ts +83 -0
- package/dist/components/Popover/popover.d.ts +16 -0
- package/dist/components/Progress/Progress.vue.d.ts +26 -0
- package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
- package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
- package/dist/components/Select/Select.vue.d.ts +110 -0
- package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
- package/dist/components/Sidebar/sidebar.d.ts +10 -0
- package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
- package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
- package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
- package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
- package/dist/components/Switch/Switch.vue.d.ts +39 -0
- package/dist/components/Table/Table.vue.d.ts +82 -0
- package/dist/components/Table/table.d.ts +36 -0
- package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
- package/dist/index.d.ts +89 -25
- package/dist/keston-ui.js +4963 -14785
- package/dist/lib/cn.d.ts +4 -0
- package/dist/lib/dates.d.ts +27 -0
- package/dist/lib/initials.d.ts +2 -0
- package/dist/lib/parseDate.d.ts +44 -0
- package/dist/lib/useNow.d.ts +3 -0
- package/package.json +53 -24
- package/src/components/.gitkeep +0 -0
- package/src/components/Age/Age.stories.ts +24 -0
- package/src/components/Age/Age.vue +56 -0
- package/src/components/AppShell/AppShell.stories.ts +124 -0
- package/src/components/AppShell/AppShell.vue +212 -0
- package/src/components/Avatar/Avatar.stories.ts +56 -0
- package/src/components/Avatar/Avatar.vue +118 -0
- package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
- package/src/components/Badge/Badge.stories.ts +92 -0
- package/src/components/Badge/Badge.vue +67 -0
- package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
- package/src/components/Button/Button.mdx +79 -0
- package/src/components/Button/Button.stories.ts +207 -0
- package/src/components/Button/Button.vue +266 -0
- package/src/components/Calendar/Calendar.stories.ts +51 -0
- package/src/components/Calendar/Calendar.vue +364 -0
- package/src/components/Card/Card.stories.ts +109 -0
- package/src/components/Card/Card.vue +115 -0
- package/src/components/Checkbox/Checkbox.stories.ts +102 -0
- package/src/components/Checkbox/Checkbox.vue +102 -0
- package/src/components/Checklist/Checklist.stories.ts +67 -0
- package/src/components/Checklist/Checklist.vue +38 -0
- package/src/components/Chip/Chip.stories.ts +71 -0
- package/src/components/Chip/Chip.vue +86 -0
- package/src/components/Divider/Divider.stories.ts +57 -0
- package/src/components/Divider/Divider.vue +58 -0
- package/src/components/FormField/FieldMessage.vue +81 -0
- package/src/components/FormField/FormField.stories.ts +55 -0
- package/src/components/FormField/FormField.vue +160 -0
- package/src/components/Header/Header.stories.ts +168 -0
- package/src/components/Header/Header.vue +232 -0
- package/src/components/InfoTip/InfoTip.stories.ts +63 -0
- package/src/components/InfoTip/InfoTip.vue +50 -0
- package/src/components/Input/Input.stories.ts +206 -0
- package/src/components/Input/Input.vue +90 -0
- package/src/components/InputCode/InputCode.stories.ts +85 -0
- package/src/components/InputCode/InputCode.vue +145 -0
- package/src/components/InputDate/InputDate.stories.ts +58 -0
- package/src/components/InputDate/InputDate.vue +258 -0
- package/src/components/InputNumber/InputNumber.stories.ts +63 -0
- package/src/components/InputNumber/InputNumber.vue +109 -0
- package/src/components/InputPassword/InputPassword.stories.ts +105 -0
- package/src/components/InputPassword/InputPassword.vue +132 -0
- package/src/components/InputRichText/InputRichText.stories.ts +100 -0
- package/src/components/InputRichText/InputRichText.vue +194 -0
- package/src/components/Link/Link.stories.ts +73 -0
- package/src/components/Link/Link.vue +56 -0
- package/src/components/Notification/Notification.stories.ts +196 -0
- package/src/components/Notification/Notification.vue +136 -0
- package/src/components/Notification/NotificationPane.vue +187 -0
- package/src/components/Notification/Toaster.vue +446 -0
- package/src/components/Notification/notification.samples.ts +45 -0
- package/src/components/Notification/notifications.test.ts +99 -0
- package/src/components/Notification/notifications.ts +172 -0
- package/src/components/PageHeader/PageHeader.stories.ts +79 -0
- package/src/components/PageHeader/PageHeader.vue +66 -0
- package/src/components/Popover/Popover.stories.ts +135 -0
- package/src/components/Popover/Popover.vue +192 -0
- package/src/components/Popover/popover.ts +20 -0
- package/src/components/Progress/Progress.stories.ts +74 -0
- package/src/components/Progress/Progress.vue +75 -0
- package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
- package/src/components/RadioGroup/RadioGroup.vue +132 -0
- package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
- package/src/components/RelativeTime/RelativeTime.vue +51 -0
- package/src/components/Select/Select.mdx +202 -0
- package/src/components/Select/Select.stories.ts +223 -0
- package/src/components/Select/Select.vue +630 -0
- package/src/components/Sidebar/Sidebar.stories.ts +192 -0
- package/src/components/Sidebar/Sidebar.vue +382 -0
- package/src/components/Sidebar/sidebar.ts +20 -0
- package/src/components/Slideover/Slideover.stories.ts +225 -0
- package/src/components/Slideover/Slideover.vue +365 -0
- package/src/components/Spinner/Spinner.stories.ts +79 -0
- package/src/components/Spinner/Spinner.vue +97 -0
- package/src/components/StatTile/StatTile.stories.ts +75 -0
- package/src/components/StatTile/StatTile.vue +89 -0
- package/src/components/SummaryList/SummaryList.stories.ts +103 -0
- package/src/components/SummaryList/SummaryList.vue +127 -0
- package/src/components/Switch/Switch.stories.ts +100 -0
- package/src/components/Switch/Switch.vue +96 -0
- package/src/components/Table/Table.stories.ts +206 -0
- package/src/components/Table/Table.vue +220 -0
- package/src/components/Table/table.test.ts +65 -0
- package/src/components/Table/table.ts +70 -0
- package/src/components/Tooltip/Tooltip.stories.ts +57 -0
- package/src/components/Tooltip/Tooltip.vue +86 -0
- package/src/examples/AppPages.stories.ts +500 -0
- package/src/examples/FormLayout.stories.ts +185 -0
- package/src/examples/KestonIdPages.stories.ts +415 -0
- package/src/examples/SchoolPages.stories.ts +665 -0
- package/src/index.ts +114 -36
- package/src/lib/cn.ts +4 -0
- package/src/lib/dates.test.ts +68 -0
- package/src/lib/dates.ts +86 -0
- package/src/lib/initials.ts +8 -0
- package/src/lib/parseDate.test.ts +331 -0
- package/src/lib/parseDate.ts +561 -0
- package/src/lib/useNow.ts +20 -0
- package/src/styles/theme.css +101 -0
- package/dist/components/AppLauncher.vue.d.ts +0 -10
- package/dist/components/Avatar.vue.d.ts +0 -16
- package/dist/components/Button.vue.d.ts +0 -27
- package/dist/components/Checkbox.vue.d.ts +0 -18
- package/dist/components/CloseButton.vue.d.ts +0 -6
- package/dist/components/Dialog.vue.d.ts +0 -23
- package/dist/components/Drawer.vue.d.ts +0 -34
- package/dist/components/DropdownMenu.vue.d.ts +0 -21
- package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
- package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
- package/dist/components/Input.vue.d.ts +0 -14
- package/dist/components/Label.vue.d.ts +0 -16
- package/dist/components/NotificationsMenu.vue.d.ts +0 -3
- package/dist/components/Popover.vue.d.ts +0 -23
- package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
- package/dist/components/Select.vue.d.ts +0 -28
- package/dist/components/Separator.vue.d.ts +0 -11
- package/dist/components/ToastRenderer.vue.d.ts +0 -3
- package/dist/components/Toaster.vue.d.ts +0 -3
- package/dist/components/Tooltip.vue.d.ts +0 -23
- package/dist/components/TopBar.vue.d.ts +0 -34
- package/dist/favicon-old.svg +0 -1
- package/dist/favicon.svg +0 -1
- package/dist/keston-ui.cjs +0 -15
- package/dist/lib/toast.d.ts +0 -29
- package/dist/lib/types.d.ts +0 -5
- package/src/components/AppLauncher.vue +0 -63
- package/src/components/Avatar.vue +0 -51
- package/src/components/Button.vue +0 -54
- package/src/components/Checkbox.vue +0 -47
- package/src/components/CloseButton.vue +0 -24
- package/src/components/Dialog.vue +0 -65
- package/src/components/Drawer.vue +0 -202
- package/src/components/DropdownMenu.vue +0 -41
- package/src/components/DropdownMenuItem.vue +0 -28
- package/src/components/DropdownMenuSeparator.vue +0 -12
- package/src/components/Input.vue +0 -21
- package/src/components/Label.vue +0 -15
- package/src/components/NotificationsMenu.vue +0 -115
- package/src/components/Popover.vue +0 -46
- package/src/components/ProfileSwitcher.vue +0 -113
- package/src/components/Select.vue +0 -97
- package/src/components/Separator.vue +0 -30
- package/src/components/ToastItem.vue +0 -158
- package/src/components/ToastRenderer.vue +0 -113
- package/src/components/Toaster.vue +0 -11
- package/src/components/Tooltip.vue +0 -48
- package/src/components/TopBar.vue +0 -52
- package/src/lib/toast.ts +0 -95
- package/src/lib/types.ts +0 -5
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { CalendarDate, endOfMonth, startOfMonth } from '@internationalized/date'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reads a date typed in almost any English form and returns it as a
|
|
5
|
+
* CalendarDate, or undefined if it can't be understood.
|
|
6
|
+
*
|
|
7
|
+
* The text is read as a starting day plus any adjustments, in any order:
|
|
8
|
+
*
|
|
9
|
+
* Written dates 26/01/2009 · 26-01-2009 · 26.01.2009 · 26012009 · 2009-01-26
|
|
10
|
+
* 26 jan 2009 · 26th of January 2009 · jan 26 2009 · 2009 jan 26
|
|
11
|
+
* twelfth of may · 5/12 · 5th december · the 5th · march
|
|
12
|
+
* Relative days today · now · tonight · tomorrow / tmrw · yesterday
|
|
13
|
+
* the day after tomorrow · the day before yesterday
|
|
14
|
+
* Weekdays tuesday · next tuesday · this friday · last tuesday
|
|
15
|
+
* tuesday next week · last week friday · tuesday after next
|
|
16
|
+
* monday week · a week on friday · a month on friday
|
|
17
|
+
* Parts of time end of the month · start of next week · mid december
|
|
18
|
+
* end of the year · eow / eom / eoy
|
|
19
|
+
* first monday of next month · last friday of december
|
|
20
|
+
* this weekend · next weekend
|
|
21
|
+
* Holidays christmas · christmas eve · boxing day · new year's day / eve
|
|
22
|
+
* easter · good friday · easter monday · valentine's day
|
|
23
|
+
* halloween · bonfire night · mother's day · father's day …
|
|
24
|
+
* Adjustments next / last week, month, year · in 3 days · 3 days ago
|
|
25
|
+
* 2 weeks from now · a week from tomorrow · in a couple of days
|
|
26
|
+
* in 1 year 2 months · a week today
|
|
27
|
+
* Times ignored: "5th december at 3pm", "tomorrow morning"
|
|
28
|
+
*
|
|
29
|
+
* Weeks run Monday to Sunday. Dates that don't say which year, month or week
|
|
30
|
+
* ("5th march", "the 5th", "tuesday", "christmas") are resolved by `prefer`.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
export type DatePart = 'day' | 'month' | 'year'
|
|
34
|
+
export type DatePreference = 'future' | 'past' | 'nearest'
|
|
35
|
+
|
|
36
|
+
export interface ParseDateOptions {
|
|
37
|
+
/** The date to count from. */
|
|
38
|
+
today: CalendarDate
|
|
39
|
+
/** Order of day / month / year in numeric dates. Default day, month, year. */
|
|
40
|
+
order?: DatePart[]
|
|
41
|
+
/**
|
|
42
|
+
* How to resolve dates that don't say which year, month or week:
|
|
43
|
+
* 'future' (default) picks the next one, 'past' the last one, 'nearest' the closest.
|
|
44
|
+
*/
|
|
45
|
+
prefer?: DatePreference
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── Vocabulary ──────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
const weekdayNumbers: Record<string, number> = {
|
|
51
|
+
monday: 1, mon: 1,
|
|
52
|
+
tuesday: 2, tue: 2, tues: 2,
|
|
53
|
+
wednesday: 3, wed: 3, weds: 3,
|
|
54
|
+
thursday: 4, thu: 4, thur: 4, thurs: 4,
|
|
55
|
+
friday: 5, fri: 5,
|
|
56
|
+
saturday: 6, sat: 6,
|
|
57
|
+
sunday: 7, sun: 7
|
|
58
|
+
}
|
|
59
|
+
const WD = `(${Object.keys(weekdayNumbers).join('|')})`
|
|
60
|
+
|
|
61
|
+
const monthNumbers: Record<string, number> = {
|
|
62
|
+
january: 1, jan: 1,
|
|
63
|
+
february: 2, feb: 2,
|
|
64
|
+
march: 3, mar: 3,
|
|
65
|
+
april: 4, apr: 4,
|
|
66
|
+
may: 5,
|
|
67
|
+
june: 6, jun: 6,
|
|
68
|
+
july: 7, jul: 7,
|
|
69
|
+
august: 8, aug: 8,
|
|
70
|
+
september: 9, sept: 9, sep: 9,
|
|
71
|
+
october: 10, oct: 10,
|
|
72
|
+
november: 11, nov: 11,
|
|
73
|
+
december: 12, dec: 12
|
|
74
|
+
}
|
|
75
|
+
// Longest first, so "september" is tried before "sep".
|
|
76
|
+
const MONTH = `(${Object.keys(monthNumbers).sort((a, b) => b.length - a.length).join('|')})`
|
|
77
|
+
|
|
78
|
+
const numberWords: Record<string, number> = {
|
|
79
|
+
a: 1, an: 1, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7,
|
|
80
|
+
eight: 8, nine: 9, ten: 10, eleven: 11, twelve: 12, thirteen: 13, fourteen: 14,
|
|
81
|
+
fifteen: 15, sixteen: 16, seventeen: 17, eighteen: 18, nineteen: 19, twenty: 20,
|
|
82
|
+
thirty: 30
|
|
83
|
+
}
|
|
84
|
+
const AMOUNT = `(\\d+|${Object.keys(numberWords).join('|')})`
|
|
85
|
+
const UNIT = '(day|week|fortnight|month|year)s?'
|
|
86
|
+
|
|
87
|
+
const ordinalWords: Record<string, number> = {
|
|
88
|
+
first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6, seventh: 7,
|
|
89
|
+
eighth: 8, ninth: 9, tenth: 10, eleventh: 11, twelfth: 12, thirteenth: 13,
|
|
90
|
+
fourteenth: 14, fifteenth: 15, sixteenth: 16, seventeenth: 17, eighteenth: 18,
|
|
91
|
+
nineteenth: 19, twentieth: 20, thirtieth: 30
|
|
92
|
+
}
|
|
93
|
+
const ORDINAL_WORD = Object.keys(ordinalWords).join('|')
|
|
94
|
+
|
|
95
|
+
type Unit = 'days' | 'weeks' | 'months' | 'years'
|
|
96
|
+
type Shift = Record<Unit, number>
|
|
97
|
+
|
|
98
|
+
// ── Small date helpers ──────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
/** 1 = Monday … 7 = Sunday */
|
|
101
|
+
function weekdayOf(date: CalendarDate) {
|
|
102
|
+
return new Date(date.year, date.month - 1, date.day).getDay() || 7
|
|
103
|
+
}
|
|
104
|
+
function mondayOf(date: CalendarDate) {
|
|
105
|
+
return date.subtract({ days: weekdayOf(date) - 1 })
|
|
106
|
+
}
|
|
107
|
+
/** A real date, or undefined (e.g. 31 February). */
|
|
108
|
+
function makeDate(year: number, month: number, day: number): CalendarDate | undefined {
|
|
109
|
+
if (!Number.isInteger(year) || month < 1 || month > 12 || day < 1) return undefined
|
|
110
|
+
const first = new CalendarDate(year, month, 1)
|
|
111
|
+
if (day > first.calendar.getDaysInMonth(first)) return undefined
|
|
112
|
+
return new CalendarDate(year, month, day)
|
|
113
|
+
}
|
|
114
|
+
/** The nth (1–5, or -1 for last) weekday of a month. */
|
|
115
|
+
function nthWeekday(year: number, month: number, weekday: number, n: number) {
|
|
116
|
+
if (n === -1) {
|
|
117
|
+
const last = endOfMonth(new CalendarDate(year, month, 1))
|
|
118
|
+
return last.subtract({ days: (weekdayOf(last) - weekday + 7) % 7 })
|
|
119
|
+
}
|
|
120
|
+
const first = new CalendarDate(year, month, 1)
|
|
121
|
+
const date = first.add({ days: (weekday - weekdayOf(first) + 7) % 7 + (n - 1) * 7 })
|
|
122
|
+
return date.month === month ? date : undefined
|
|
123
|
+
}
|
|
124
|
+
/** Easter Sunday (Gregorian calendar). */
|
|
125
|
+
function easterSunday(year: number) {
|
|
126
|
+
const a = year % 19
|
|
127
|
+
const b = Math.floor(year / 100)
|
|
128
|
+
const c = year % 100
|
|
129
|
+
const d = Math.floor(b / 4)
|
|
130
|
+
const e = b % 4
|
|
131
|
+
const f = Math.floor((b + 8) / 25)
|
|
132
|
+
const g = Math.floor((b - f + 1) / 3)
|
|
133
|
+
const h = (19 * a + b - d - g + 15) % 30
|
|
134
|
+
const i = Math.floor(c / 4)
|
|
135
|
+
const k = c % 4
|
|
136
|
+
const l = (32 + 2 * e + 2 * i - h - k) % 7
|
|
137
|
+
const m = Math.floor((a + 11 * h + 22 * l) / 451)
|
|
138
|
+
const month = Math.floor((h + l - 7 * m + 114) / 31)
|
|
139
|
+
const day = ((h + l - 7 * m + 114) % 31) + 1
|
|
140
|
+
return new CalendarDate(year, month, day)
|
|
141
|
+
}
|
|
142
|
+
function expandYear(year: string, today: CalendarDate) {
|
|
143
|
+
if (year.length === 4) return Number(year)
|
|
144
|
+
if (year.length !== 2) return NaN
|
|
145
|
+
// Two-digit years: up to this year → 2000s, otherwise 1900s (09 → 2009, 85 → 1985).
|
|
146
|
+
const n = Number(year)
|
|
147
|
+
return n <= today.year % 100 ? 2000 + n : 1900 + n
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── Holidays ────────────────────────────────────────────────────────────────
|
|
151
|
+
// Typed without apostrophes (they are removed before matching). Order matters:
|
|
152
|
+
// "christmas eve" must be tried before "christmas".
|
|
153
|
+
const holidays: [string, (year: number) => CalendarDate | undefined][] = [
|
|
154
|
+
['(?:christmas|xmas) eve', (y) => makeDate(y, 12, 24)],
|
|
155
|
+
['(?:christmas|xmas)(?: day)?', (y) => makeDate(y, 12, 25)],
|
|
156
|
+
['boxing day', (y) => makeDate(y, 12, 26)],
|
|
157
|
+
['(?:new years eve|nye)', (y) => makeDate(y, 12, 31)],
|
|
158
|
+
['new years?(?: day)?', (y) => makeDate(y, 1, 1)],
|
|
159
|
+
['valentines(?: day)?', (y) => makeDate(y, 2, 14)],
|
|
160
|
+
['(?:st )?(?:patricks|paddys) day', (y) => makeDate(y, 3, 17)],
|
|
161
|
+
['good friday', (y) => easterSunday(y).subtract({ days: 2 })],
|
|
162
|
+
['easter monday', (y) => easterSunday(y).add({ days: 1 })],
|
|
163
|
+
['easter(?: sunday| day)?', (y) => easterSunday(y)],
|
|
164
|
+
// UK Mothering Sunday: three weeks before Easter
|
|
165
|
+
['(?:mothers day|mothering sunday)', (y) => easterSunday(y).subtract({ weeks: 3 })],
|
|
166
|
+
['fathers day', (y) => nthWeekday(y, 6, 7, 3)],
|
|
167
|
+
['halloween', (y) => makeDate(y, 10, 31)],
|
|
168
|
+
['(?:bonfire night|guy fawkes(?: night| day)?)', (y) => makeDate(y, 11, 5)],
|
|
169
|
+
['(?:remembrance|armistice) day', (y) => makeDate(y, 11, 11)]
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
// ── Starting day kinds ──────────────────────────────────────────────────────
|
|
173
|
+
// How the starting day is pinned down decides how `prefer` applies to it.
|
|
174
|
+
type Start =
|
|
175
|
+
| { kind: 'date'; date: CalendarDate }
|
|
176
|
+
/** No year given: "5th march", "christmas", "5/12" */
|
|
177
|
+
| { kind: 'yearless'; resolve: (year: number) => CalendarDate | undefined }
|
|
178
|
+
/** No month given: "the 5th" */
|
|
179
|
+
| { kind: 'dayOfMonth'; day: number }
|
|
180
|
+
/** No week given: "tuesday" */
|
|
181
|
+
| { kind: 'weekday'; weekday: number }
|
|
182
|
+
|
|
183
|
+
function pick(candidates: (CalendarDate | undefined)[], today: CalendarDate, prefer: DatePreference) {
|
|
184
|
+
const dates = candidates.filter((d): d is CalendarDate => !!d).sort((a, b) => a.compare(b))
|
|
185
|
+
if (prefer === 'future') return dates.find((d) => d.compare(today) >= 0)
|
|
186
|
+
if (prefer === 'past') return [...dates].reverse().find((d) => d.compare(today) <= 0)
|
|
187
|
+
// nearest; ties go to the future
|
|
188
|
+
return dates.reduce<CalendarDate | undefined>((best, d) => {
|
|
189
|
+
if (!best) return d
|
|
190
|
+
const distance = Math.abs(d.compare(today))
|
|
191
|
+
const bestDistance = Math.abs(best.compare(today))
|
|
192
|
+
return distance < bestDistance || (distance === bestDistance && d.compare(today) > 0) ? d : best
|
|
193
|
+
}, undefined)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function resolveStart(
|
|
197
|
+
start: Start | undefined,
|
|
198
|
+
shift: Shift,
|
|
199
|
+
explicit: { year: boolean; month: boolean; yearValue?: number },
|
|
200
|
+
today: CalendarDate,
|
|
201
|
+
prefer: DatePreference
|
|
202
|
+
): CalendarDate | undefined {
|
|
203
|
+
if (!start) return explicit.yearValue === undefined ? today.add(shift) : undefined
|
|
204
|
+
|
|
205
|
+
switch (start.kind) {
|
|
206
|
+
case 'date':
|
|
207
|
+
return explicit.yearValue === undefined ? start.date.add(shift) : undefined
|
|
208
|
+
|
|
209
|
+
case 'yearless': {
|
|
210
|
+
// A year was given ("christmas 2027", "christmas next year", "5th march
|
|
211
|
+
// this year"): resolve in that year, so moveable dates like Easter stay correct.
|
|
212
|
+
if (explicit.yearValue !== undefined) {
|
|
213
|
+
return start.resolve(explicit.yearValue + shift.years)?.add({ ...shift, years: 0 })
|
|
214
|
+
}
|
|
215
|
+
if (explicit.year || shift.years) {
|
|
216
|
+
return start.resolve(today.year + shift.years)?.add({ ...shift, years: 0 })
|
|
217
|
+
}
|
|
218
|
+
// Look 4 years either way, so "29 feb" finds the nearest leap year.
|
|
219
|
+
const years = Array.from({ length: 9 }, (_, i) => today.year - 4 + i)
|
|
220
|
+
return pick(years.map(start.resolve), today, prefer)?.add(shift)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
case 'dayOfMonth': {
|
|
224
|
+
if (explicit.yearValue !== undefined) return undefined
|
|
225
|
+
// A month was given ("the 12th of next month"): use that month.
|
|
226
|
+
if (explicit.month || shift.months) {
|
|
227
|
+
const month = startOfMonth(today).add({ months: shift.months })
|
|
228
|
+
return makeDate(month.year, month.month, start.day)?.add({ ...shift, months: 0 })
|
|
229
|
+
}
|
|
230
|
+
const months = [-1, 0, 1].map((offset) => startOfMonth(today).add({ months: offset }))
|
|
231
|
+
return pick(months.map((m) => makeDate(m.year, m.month, start.day)), today, prefer)?.add(shift)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
case 'weekday': {
|
|
235
|
+
if (explicit.yearValue !== undefined) return undefined
|
|
236
|
+
const ahead = (start.weekday - weekdayOf(today) + 7) % 7 || 7
|
|
237
|
+
const back = (weekdayOf(today) - start.weekday + 7) % 7 || 7
|
|
238
|
+
const next = today.add({ days: ahead })
|
|
239
|
+
const last = today.subtract({ days: back })
|
|
240
|
+
// A weekday on its own never means today: it means the next or last one.
|
|
241
|
+
const chosen =
|
|
242
|
+
prefer === 'past' ? last : prefer === 'nearest' ? (back < ahead ? last : next) : next
|
|
243
|
+
return chosen.add(shift)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ── Written dates ───────────────────────────────────────────────────────────
|
|
249
|
+
|
|
250
|
+
function parseWritten(input: string, today: CalendarDate, order: DatePart[]): Start | undefined {
|
|
251
|
+
const raw = input.trim()
|
|
252
|
+
|
|
253
|
+
// 2009-01-26
|
|
254
|
+
const iso = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(raw)
|
|
255
|
+
if (iso) {
|
|
256
|
+
const date = makeDate(Number(iso[1]), Number(iso[2]), Number(iso[3]))
|
|
257
|
+
return date && { kind: 'date', date }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Digits only: 26012009, split by the locale's order (2, 2, 4 digits).
|
|
261
|
+
if (/^\d{8}$/.test(raw)) {
|
|
262
|
+
let index = 0
|
|
263
|
+
const parts = Object.fromEntries(
|
|
264
|
+
order.map((part) => {
|
|
265
|
+
const length = part === 'year' ? 4 : 2
|
|
266
|
+
const piece = raw.slice(index, index + length)
|
|
267
|
+
index += length
|
|
268
|
+
return [part, piece]
|
|
269
|
+
})
|
|
270
|
+
) as Record<DatePart, string>
|
|
271
|
+
const date = makeDate(Number(parts.year), Number(parts.month), Number(parts.day))
|
|
272
|
+
return date && { kind: 'date', date }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Numbers with separators: 26/01/2009, 26-1-09, 26.01.2009, or 5/12 (no year).
|
|
276
|
+
const pieces = raw.split(/[/.\-\s]+/)
|
|
277
|
+
if (pieces.every((p) => /^\d+$/.test(p)) && (pieces.length === 3 || pieces.length === 2)) {
|
|
278
|
+
const withoutYear = order.filter((part) => part !== 'year')
|
|
279
|
+
const parts = Object.fromEntries(
|
|
280
|
+
(pieces.length === 3 ? order : withoutYear).map((part, i) => [part, pieces[i]])
|
|
281
|
+
) as Partial<Record<DatePart, string>>
|
|
282
|
+
const day = Number(parts.day)
|
|
283
|
+
const month = Number(parts.month)
|
|
284
|
+
if (parts.year === undefined) return { kind: 'yearless', resolve: (y) => makeDate(y, month, day) }
|
|
285
|
+
const date = makeDate(expandYear(parts.year, today), month, day)
|
|
286
|
+
return date && { kind: 'date', date }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// With month names. Ordinals and filler words are dropped: "the 26th of
|
|
290
|
+
// January, 2009" → "26 january 2009".
|
|
291
|
+
const text = raw
|
|
292
|
+
.replace(/(\d)(st|nd|rd|th)\b/g, '$1')
|
|
293
|
+
.replace(/\bof\b/g, ' ')
|
|
294
|
+
.replace(/[,/.\-]/g, ' ')
|
|
295
|
+
.replace(/\s+/g, ' ')
|
|
296
|
+
.trim()
|
|
297
|
+
const named = (day: string, month: string, year?: string): Start | undefined => {
|
|
298
|
+
const d = Number(day)
|
|
299
|
+
const m = monthNumbers[month]
|
|
300
|
+
if (year === undefined) return { kind: 'yearless', resolve: (y) => makeDate(y, m, d) }
|
|
301
|
+
const date = makeDate(expandYear(year, today), m, d)
|
|
302
|
+
return date && { kind: 'date', date }
|
|
303
|
+
}
|
|
304
|
+
let match: RegExpExecArray | null
|
|
305
|
+
if ((match = new RegExp(`^(\\d{1,2}) ${MONTH}(?: (\\d{2}|\\d{4}))?$`).exec(text))) return named(match[1], match[2], match[3])
|
|
306
|
+
if ((match = new RegExp(`^${MONTH} (\\d{1,2})(?: (\\d{2}|\\d{4}))?$`).exec(text))) return named(match[2], match[1], match[3])
|
|
307
|
+
if ((match = new RegExp(`^(\\d{4}) ${MONTH} (\\d{1,2})$`).exec(text))) return named(match[3], match[2], match[1])
|
|
308
|
+
|
|
309
|
+
// A month on its own ("march", "march 2027") → the 1st of that month.
|
|
310
|
+
if ((match = new RegExp(`^${MONTH}(?: (\\d{4}))?$`).exec(text))) {
|
|
311
|
+
const m = monthNumbers[match[1]]
|
|
312
|
+
if (match[2]) {
|
|
313
|
+
const date = makeDate(Number(match[2]), m, 1)
|
|
314
|
+
return date && { kind: 'date', date }
|
|
315
|
+
}
|
|
316
|
+
return { kind: 'yearless', resolve: (y) => makeDate(y, m, 1) }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// A day on its own, with an ordinal: "the 5th", "5th"
|
|
320
|
+
if ((match = /^(\d{1,2})(?:st|nd|rd|th)$/.exec(raw.replace(/\bof\b/g, '').trim()))) {
|
|
321
|
+
const day = Number(match[1])
|
|
322
|
+
return day >= 1 && day <= 31 ? { kind: 'dayOfMonth', day } : undefined
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return undefined
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ── Normalising ─────────────────────────────────────────────────────────────
|
|
329
|
+
|
|
330
|
+
/** 1 → "1st", 2 → "2nd", 3 → "3rd", 11 → "11th", 21 → "21st" … */
|
|
331
|
+
function ordinal(n: number) {
|
|
332
|
+
const suffix = n % 100 >= 11 && n % 100 <= 13 ? 'th' : ({ 1: 'st', 2: 'nd', 3: 'rd' } as Record<number, string>)[n % 10] ?? 'th'
|
|
333
|
+
return `${n}${suffix}`
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function normalise(input: string) {
|
|
337
|
+
return (
|
|
338
|
+
input
|
|
339
|
+
.toLowerCase()
|
|
340
|
+
.replace(/['’]/g, '')
|
|
341
|
+
.replace(/,/g, ' ')
|
|
342
|
+
.replace(/\.+$/, '')
|
|
343
|
+
// Ordinal words → numbers: "twelfth" → "12th", "twenty first" → "21st"
|
|
344
|
+
.replace(new RegExp(`\\b(twenty|thirty)[ -](${ORDINAL_WORD})\\b`, 'g'), (_, tens: string, unit: string) =>
|
|
345
|
+
ordinal((tens === 'twenty' ? 20 : 30) + ordinalWords[unit])
|
|
346
|
+
)
|
|
347
|
+
.replace(new RegExp(`\\b(${ORDINAL_WORD})\\b`, 'g'), (word: string) => ordinal(ordinalWords[word]))
|
|
348
|
+
// Loose amounts
|
|
349
|
+
.replace(/\b(?:a )?couple(?: of)?\b/g, '2')
|
|
350
|
+
.replace(/\b(?:a )?few\b/g, '3')
|
|
351
|
+
// Times of day are ignored: "at 3pm", "15:30", "noon", "in the morning".
|
|
352
|
+
// (Not "at 26/01/2009": a number followed by a date separator is a date.)
|
|
353
|
+
.replace(/\b(?:at |@ ?)?\d{1,2}(?::\d{2})? ?(?:am|pm)\b/g, ' ')
|
|
354
|
+
.replace(/\b(?:at |@ ?)\d{1,2}(?::\d{2})?\b(?![/.\-]\d)/g, ' ')
|
|
355
|
+
.replace(/\b\d{1,2}:\d{2}\b/g, ' ')
|
|
356
|
+
.replace(/\b(?:at )?(?:noon|midday|midnight)\b/g, ' ')
|
|
357
|
+
.replace(/\b(?:tonight|this (?:morning|afternoon|evening)|eod|end of (?:the )?day)\b/g, ' today ')
|
|
358
|
+
// ("night" stays in "bonfire night" / "guy fawkes night")
|
|
359
|
+
.replace(/\b(?:in the )?(?:morning|afternoon|evening|(?<!bonfire |fawkes )night)\b/g, ' ')
|
|
360
|
+
.replace(/\bthe\b/g, ' ')
|
|
361
|
+
.replace(/\s+/g, ' ')
|
|
362
|
+
.trim()
|
|
363
|
+
)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ── Main ────────────────────────────────────────────────────────────────────
|
|
367
|
+
|
|
368
|
+
export function parseDateText(input: string, options: ParseDateOptions): CalendarDate | undefined {
|
|
369
|
+
const { today, order = ['day', 'month', 'year'], prefer = 'future' } = options
|
|
370
|
+
const normalised = normalise(input)
|
|
371
|
+
if (!normalised) return undefined
|
|
372
|
+
|
|
373
|
+
// Padded so every phrase can be matched as " phrase ".
|
|
374
|
+
let text = ` ${normalised} `
|
|
375
|
+
let foundWords = false
|
|
376
|
+
let impossible = false
|
|
377
|
+
const starts: Start[] = []
|
|
378
|
+
let startIsBareWeekday = false
|
|
379
|
+
const shift: Shift = { days: 0, weeks: 0, months: 0, years: 0 }
|
|
380
|
+
const explicit: { year: boolean; month: boolean; yearValue?: number } = { year: false, month: false }
|
|
381
|
+
const thisMonday = mondayOf(today)
|
|
382
|
+
|
|
383
|
+
/** Finds a phrase anywhere (as whole words), removes it and returns the match. */
|
|
384
|
+
function take(pattern: string) {
|
|
385
|
+
const match = new RegExp(` ${pattern} `).exec(text)
|
|
386
|
+
if (match) {
|
|
387
|
+
text = text.replace(match[0], ' ')
|
|
388
|
+
foundWords = true
|
|
389
|
+
}
|
|
390
|
+
return match
|
|
391
|
+
}
|
|
392
|
+
// undefined = a phrase that names an impossible date (e.g. the 5th Monday
|
|
393
|
+
// of a month that has four), so the whole text is rejected.
|
|
394
|
+
const addStart = (start: Start | undefined, bareWeekday = false) => {
|
|
395
|
+
if (!start) {
|
|
396
|
+
impossible = true
|
|
397
|
+
return
|
|
398
|
+
}
|
|
399
|
+
starts.push(start)
|
|
400
|
+
startIsBareWeekday = bareWeekday
|
|
401
|
+
}
|
|
402
|
+
const date = (d: CalendarDate | undefined): Start | undefined => d && { kind: 'date', date: d }
|
|
403
|
+
const first = (m: RegExpExecArray, from = 1) => m.slice(from).find((g) => g !== undefined)!
|
|
404
|
+
const toUnit = (word: string): [Unit, number] =>
|
|
405
|
+
word === 'fortnight' ? ['weeks', 2] : [`${word}s` as Unit, 1]
|
|
406
|
+
const toAmount = (word: string) => numberWords[word] ?? Number(word)
|
|
407
|
+
const upcoming = (weekday: number) => today.add({ days: (weekday - weekdayOf(today) + 7) % 7 || 7 })
|
|
408
|
+
let m: RegExpExecArray | null
|
|
409
|
+
|
|
410
|
+
// ── Starting day: most specific phrases first ──
|
|
411
|
+
|
|
412
|
+
// eow / eom / eoy
|
|
413
|
+
if (take('eow')) addStart(date(thisMonday.add({ days: 4 })))
|
|
414
|
+
if (take('eom')) addStart(date(endOfMonth(today)))
|
|
415
|
+
if (take('eoy')) addStart(date(new CalendarDate(today.year, 12, 31)))
|
|
416
|
+
|
|
417
|
+
// first monday of next month · last friday of december · 2nd tuesday in march 2027
|
|
418
|
+
if ((m = take(`(1st|2nd|3rd|4th|5th|last) ${WD} (?:of|in) (?:(this|next|last) month|month|${MONTH}(?: (\\d{4}))?)`))) {
|
|
419
|
+
const n = m[1] === 'last' ? -1 : Number(m[1][0])
|
|
420
|
+
const weekday = weekdayNumbers[m[2]]
|
|
421
|
+
if (m[4]) {
|
|
422
|
+
const month = monthNumbers[m[4]]
|
|
423
|
+
if (m[5]) addStart(date(nthWeekday(Number(m[5]), month, weekday, n)))
|
|
424
|
+
else addStart({ kind: 'yearless', resolve: (y) => nthWeekday(y, month, weekday, n) })
|
|
425
|
+
} else {
|
|
426
|
+
const offset = m[3] === 'next' ? 1 : m[3] === 'last' ? -1 : 0
|
|
427
|
+
const month = startOfMonth(today).add({ months: offset })
|
|
428
|
+
addStart(date(nthWeekday(month.year, month.month, weekday, n)))
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// start / middle / end of (this | next | last) week / month / year
|
|
433
|
+
if ((m = take(`(start|beginning|middle|mid|end) (?:of )?(?:(this|next|last) )?(week|month|year)`))) {
|
|
434
|
+
const offset = m[2] === 'next' ? 1 : m[2] === 'last' ? -1 : 0
|
|
435
|
+
const position = m[1] === 'beginning' ? 'start' : m[1] === 'middle' ? 'mid' : m[1]
|
|
436
|
+
if (m[3] === 'week') {
|
|
437
|
+
// Working week: start Monday, middle Wednesday, end Friday.
|
|
438
|
+
const monday = thisMonday.add({ weeks: offset })
|
|
439
|
+
addStart(date(monday.add({ days: position === 'start' ? 0 : position === 'mid' ? 2 : 4 })))
|
|
440
|
+
} else if (m[3] === 'month') {
|
|
441
|
+
const month = startOfMonth(today).add({ months: offset })
|
|
442
|
+
addStart(date(position === 'start' ? month : position === 'mid' ? month.set({ day: 15 }) : endOfMonth(month)))
|
|
443
|
+
} else {
|
|
444
|
+
const year = today.year + offset
|
|
445
|
+
addStart(date(position === 'start' ? new CalendarDate(year, 1, 1) : position === 'mid' ? new CalendarDate(year, 7, 1) : new CalendarDate(year, 12, 31)))
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// start / early / mid / late / end of december (2027)
|
|
450
|
+
if ((m = take(`(start|beginning|early|middle|mid|late|end) (?:of )?${MONTH}(?: (\\d{4}))?`))) {
|
|
451
|
+
// Copied out of the match: dayIn runs later, after `m` has been reused.
|
|
452
|
+
const position = m[1]
|
|
453
|
+
const month = monthNumbers[m[2]]
|
|
454
|
+
const dayIn = (y: number) => {
|
|
455
|
+
if (['start', 'beginning', 'early'].includes(position)) return makeDate(y, month, 1)
|
|
456
|
+
if (['middle', 'mid'].includes(position)) return makeDate(y, month, 15)
|
|
457
|
+
return endOfMonth(new CalendarDate(y, month, 1))
|
|
458
|
+
}
|
|
459
|
+
addStart(m[3] ? date(dayIn(Number(m[3]))) : { kind: 'yearless', resolve: dayIn })
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Weekends start on Saturday.
|
|
463
|
+
if (take('this weekend')) addStart(date(thisMonday.add({ days: 5 })))
|
|
464
|
+
if (take('next weekend')) addStart(date(thisMonday.add({ weeks: 1, days: 5 })))
|
|
465
|
+
if (take('last weekend')) addStart(date(thisMonday.subtract({ weeks: 1 }).add({ days: 5 })))
|
|
466
|
+
if (take('(?:on )?weekend')) addStart({ kind: 'weekday', weekday: 6 })
|
|
467
|
+
|
|
468
|
+
// Holidays
|
|
469
|
+
for (const [pattern, resolve] of holidays) {
|
|
470
|
+
if (take(pattern)) addStart({ kind: 'yearless', resolve })
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// tuesday after next · monday week · friday fortnight
|
|
474
|
+
if ((m = take(`${WD} after next`))) addStart(date(upcoming(weekdayNumbers[m[1]]).add({ weeks: 1 })))
|
|
475
|
+
if ((m = take(`${WD} (week|fortnight)`))) {
|
|
476
|
+
addStart(date(upcoming(weekdayNumbers[m[1]]).add({ weeks: m[2] === 'fortnight' ? 2 : 1 })))
|
|
477
|
+
}
|
|
478
|
+
// a week on friday · 2 weeks on monday · a month on friday
|
|
479
|
+
if ((m = take(`${AMOUNT} ${UNIT} on ${WD}`))) {
|
|
480
|
+
const [unit, times] = toUnit(m[2])
|
|
481
|
+
addStart(date(upcoming(weekdayNumbers[m[3]]).add({ [unit]: toAmount(m[1]) * times })))
|
|
482
|
+
}
|
|
483
|
+
// a week today · a fortnight tomorrow
|
|
484
|
+
if ((m = take(`${AMOUNT} ${UNIT} (today|tomorrow|tmrw|tmr)`))) {
|
|
485
|
+
const [unit, times] = toUnit(m[2])
|
|
486
|
+
const base = m[3] === 'today' ? today : today.add({ days: 1 })
|
|
487
|
+
addStart(date(base.add({ [unit]: toAmount(m[1]) * times })))
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// next tuesday · next week tuesday · tuesday next week → Tuesday of next week
|
|
491
|
+
if ((m = take(`(?:next week ${WD}|${WD} next week|next ${WD})`))) {
|
|
492
|
+
addStart(date(thisMonday.add({ weeks: 1, days: weekdayNumbers[first(m)] - 1 })))
|
|
493
|
+
}
|
|
494
|
+
// last week friday · friday last week → Friday of last week
|
|
495
|
+
if ((m = take(`(?:last week ${WD}|${WD} last week)`))) {
|
|
496
|
+
addStart(date(thisMonday.subtract({ weeks: 1 }).add({ days: weekdayNumbers[first(m)] - 1 })))
|
|
497
|
+
}
|
|
498
|
+
// this friday · this week friday · friday this week
|
|
499
|
+
if ((m = take(`(?:this week ${WD}|${WD} this week|this ${WD})`))) {
|
|
500
|
+
addStart(date(thisMonday.add({ days: weekdayNumbers[first(m)] - 1 })))
|
|
501
|
+
}
|
|
502
|
+
// last tuesday → the most recent one before today
|
|
503
|
+
if ((m = take(`last ${WD}`))) {
|
|
504
|
+
addStart(date(today.subtract({ days: (weekdayOf(today) - weekdayNumbers[m[1]] + 7) % 7 || 7 })))
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (take('day after (?:tomorrow|tmrw|tmr)')) addStart(date(today.add({ days: 2 })))
|
|
508
|
+
if (take('day before yesterday')) addStart(date(today.subtract({ days: 2 })))
|
|
509
|
+
if (take('(?:tomorrow|tmrw|tmr|tmw|tomoz)')) addStart(date(today.add({ days: 1 })))
|
|
510
|
+
if (take('(?:yesterday|yday|yest)')) addStart(date(today.subtract({ days: 1 })))
|
|
511
|
+
if (take('(?:today|now|tdy)')) addStart(date(today))
|
|
512
|
+
|
|
513
|
+
// A weekday on its own: resolved by `prefer`
|
|
514
|
+
if ((m = take(`(?:on )?${WD}`))) addStart({ kind: 'weekday', weekday: weekdayNumbers[m[1]] }, true)
|
|
515
|
+
|
|
516
|
+
// ── Adjustments ──
|
|
517
|
+
if (take('this year')) explicit.year = true
|
|
518
|
+
if (take('this month')) explicit.month = true
|
|
519
|
+
while ((m = take(`(next|last) ${UNIT}`))) {
|
|
520
|
+
const [unit, times] = toUnit(m[2])
|
|
521
|
+
shift[unit] += (m[1] === 'next' ? 1 : -1) * times
|
|
522
|
+
}
|
|
523
|
+
let foundIn = false
|
|
524
|
+
while ((m = take(`in ${AMOUNT} ${UNIT}`))) {
|
|
525
|
+
const [unit, times] = toUnit(m[2])
|
|
526
|
+
shift[unit] += toAmount(m[1]) * times
|
|
527
|
+
foundIn = true
|
|
528
|
+
}
|
|
529
|
+
// More amounts after "in": "in 1 year 2 months", "in 1 week and 3 days"
|
|
530
|
+
while (foundIn && (m = take(`(?:and )?${AMOUNT} ${UNIT}(?! (?:ago|before|earlier|from|after|later))`))) {
|
|
531
|
+
const [unit, times] = toUnit(m[2])
|
|
532
|
+
shift[unit] += toAmount(m[1]) * times
|
|
533
|
+
}
|
|
534
|
+
while ((m = take(`${AMOUNT} ${UNIT} (ago|before|earlier|from|after|later)`))) {
|
|
535
|
+
const [unit, times] = toUnit(m[2])
|
|
536
|
+
const sign = ['ago', 'before', 'earlier'].includes(m[3]) ? -1 : 1
|
|
537
|
+
shift[unit] += sign * toAmount(m[1]) * times
|
|
538
|
+
}
|
|
539
|
+
// Leftover filler words
|
|
540
|
+
while (take('(?:on|of|at|by|for)'));
|
|
541
|
+
|
|
542
|
+
// ── Whatever is left must be a written date ──
|
|
543
|
+
let rest = text.trim()
|
|
544
|
+
// A year next to a holiday or other yearless phrase: "christmas 2027"
|
|
545
|
+
if (foundWords && /^\d{4}$/.test(rest)) {
|
|
546
|
+
explicit.yearValue = Number(rest)
|
|
547
|
+
rest = ''
|
|
548
|
+
}
|
|
549
|
+
if (rest) {
|
|
550
|
+
const written = parseWritten(rest, today, order)
|
|
551
|
+
if (!written) return undefined
|
|
552
|
+
// A weekday next to a written date ("tue 26 jan 2009") is just decoration.
|
|
553
|
+
if (starts.length === 1 && startIsBareWeekday) starts.pop()
|
|
554
|
+
starts.push(written)
|
|
555
|
+
} else if (!foundWords) {
|
|
556
|
+
return undefined
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (impossible || starts.length > 1) return undefined
|
|
560
|
+
return resolveStart(starts[0], shift, explicit, today, prefer)
|
|
561
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { onBeforeUnmount, onMounted, ref, type Ref } from 'vue'
|
|
2
|
+
|
|
3
|
+
// One timer for the whole page, however many relative times are showing.
|
|
4
|
+
const now = ref(new Date())
|
|
5
|
+
let users = 0
|
|
6
|
+
let timer: ReturnType<typeof setInterval> | undefined
|
|
7
|
+
|
|
8
|
+
/** The current time, updated every 30 seconds while any component uses it. */
|
|
9
|
+
export function useNow(): Readonly<Ref<Date>> {
|
|
10
|
+
onMounted(() => {
|
|
11
|
+
if (users++ === 0) {
|
|
12
|
+
now.value = new Date()
|
|
13
|
+
timer = setInterval(() => (now.value = new Date()), 30_000)
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
onBeforeUnmount(() => {
|
|
17
|
+
if (--users === 0) clearInterval(timer)
|
|
18
|
+
})
|
|
19
|
+
return now
|
|
20
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Keston UI theme
|
|
3
|
+
* ----------------
|
|
4
|
+
* Design tokens for Tailwind v4. Apps import this after Tailwind:
|
|
5
|
+
*
|
|
6
|
+
* @import "tailwindcss";
|
|
7
|
+
* @import "@thekestongroup/ui/theme.css";
|
|
8
|
+
*
|
|
9
|
+
* Only add values that differ from Tailwind's defaults (mainly colours).
|
|
10
|
+
* Every token added here must also be recorded in tokens/ so native
|
|
11
|
+
* platforms (SwiftUI, Android) can use the same values.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* Lato, bundled with the library (self-hosted, no requests to Google).
|
|
16
|
+
* Without this the font only works on machines that have Lato installed, and
|
|
17
|
+
* never in Safari, which ignores user-installed fonts.
|
|
18
|
+
*/
|
|
19
|
+
@import "@fontsource/lato/400.css";
|
|
20
|
+
@import "@fontsource/lato/400-italic.css";
|
|
21
|
+
@import "@fontsource/lato/700.css";
|
|
22
|
+
@import "@fontsource/lato/700-italic.css";
|
|
23
|
+
|
|
24
|
+
/* Let Tailwind find the classes used inside the library's components. */
|
|
25
|
+
@source "../components";
|
|
26
|
+
|
|
27
|
+
@theme {
|
|
28
|
+
--font-sans: "Lato", sans-serif;
|
|
29
|
+
--color-white: #fafafa;
|
|
30
|
+
--color-black: #050505;
|
|
31
|
+
--color-primary: var(--color-kblue);
|
|
32
|
+
--color-kblue: #023750;
|
|
33
|
+
--color-background: #e1e1e1;
|
|
34
|
+
--color-surface: #ebebeb;
|
|
35
|
+
--color-surface-elevated: #f2f2f2;
|
|
36
|
+
--color-green: #007a16;
|
|
37
|
+
--color-yellow: #f1c70b;
|
|
38
|
+
--color-red: #d2042d;
|
|
39
|
+
--color-blue: #035483;
|
|
40
|
+
--color-focus: #007fc9;
|
|
41
|
+
/* Colours e.g. --color-primary: …; */
|
|
42
|
+
|
|
43
|
+
/* Radius e.g. --radius-control: …; */
|
|
44
|
+
|
|
45
|
+
/* Typography e.g. --font-sans: …; */
|
|
46
|
+
|
|
47
|
+
/* Motion e.g. --default-transition-duration: …; */
|
|
48
|
+
/* Fades use Tailwind's default transition timing (150ms, ease-in-out). */
|
|
49
|
+
--animate-fade-in: fade-in var(--default-transition-duration) var(--default-transition-timing-function);
|
|
50
|
+
--animate-fade-out: fade-out var(--default-transition-duration) var(--default-transition-timing-function) forwards;
|
|
51
|
+
|
|
52
|
+
@keyframes fade-in {
|
|
53
|
+
from { opacity: 0; }
|
|
54
|
+
}
|
|
55
|
+
@keyframes fade-out {
|
|
56
|
+
to { opacity: 0; }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* Panels sliding in from / out to an edge (Slideover). In 300ms decelerating,
|
|
60
|
+
out 200ms accelerating, with Tailwind's ease curves. */
|
|
61
|
+
--animate-slide-in-right: slide-in-right 300ms var(--ease-out);
|
|
62
|
+
--animate-slide-out-right: slide-out-right 200ms var(--ease-in) forwards;
|
|
63
|
+
--animate-slide-in-left: slide-in-left 300ms var(--ease-out);
|
|
64
|
+
--animate-slide-out-left: slide-out-left 200ms var(--ease-in) forwards;
|
|
65
|
+
--animate-slide-in-top: slide-in-top 300ms var(--ease-out);
|
|
66
|
+
--animate-slide-out-top: slide-out-top 200ms var(--ease-in) forwards;
|
|
67
|
+
--animate-slide-in-bottom: slide-in-bottom 300ms var(--ease-out);
|
|
68
|
+
--animate-slide-out-bottom: slide-out-bottom 200ms var(--ease-in) forwards;
|
|
69
|
+
|
|
70
|
+
@keyframes slide-in-right { from { translate: 100% 0; } }
|
|
71
|
+
@keyframes slide-out-right { to { translate: 100% 0; } }
|
|
72
|
+
@keyframes slide-in-left { from { translate: -100% 0; } }
|
|
73
|
+
@keyframes slide-out-left { to { translate: -100% 0; } }
|
|
74
|
+
@keyframes slide-in-top { from { translate: 0 -100%; } }
|
|
75
|
+
@keyframes slide-out-top { to { translate: 0 -100%; } }
|
|
76
|
+
@keyframes slide-in-bottom { from { translate: 0 100%; } }
|
|
77
|
+
@keyframes slide-out-bottom { to { translate: 0 100%; } }
|
|
78
|
+
|
|
79
|
+
/* Progress with no value: a short bar sweeping across, again and again. */
|
|
80
|
+
--animate-progress-indeterminate: progress-indeterminate 1.5s var(--ease-in-out) infinite;
|
|
81
|
+
|
|
82
|
+
@keyframes progress-indeterminate {
|
|
83
|
+
from { translate: -100% 0; }
|
|
84
|
+
to { translate: 250% 0; }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* The NotificationPane's empty state arriving after the last one goes: it
|
|
88
|
+
rises into place, then the bell gives a little wobble. */
|
|
89
|
+
--animate-empty-in: empty-in 300ms var(--ease-out);
|
|
90
|
+
--animate-bell-wobble: bell-wobble 700ms 150ms var(--ease-in-out);
|
|
91
|
+
|
|
92
|
+
@keyframes empty-in {
|
|
93
|
+
from { opacity: 0; translate: 0 0.5rem; scale: 0.95; }
|
|
94
|
+
}
|
|
95
|
+
@keyframes bell-wobble {
|
|
96
|
+
20% { rotate: -14deg; }
|
|
97
|
+
40% { rotate: 12deg; }
|
|
98
|
+
60% { rotate: -8deg; }
|
|
99
|
+
80% { rotate: 4deg; }
|
|
100
|
+
}
|
|
101
|
+
}
|