@gobrand/calendar-core 0.0.7 → 0.0.8

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/dist/index.cjs CHANGED
@@ -661,6 +661,16 @@ function createCalendar(options) {
661
661
  setOptions(updater) {
662
662
  _options = functionalUpdate(updater, _options);
663
663
  },
664
+ // Type predicates for runtime view checking
665
+ hasMonthView() {
666
+ return "month" in _options.views && _options.views.month !== void 0;
667
+ },
668
+ hasWeekView() {
669
+ return "week" in _options.views && _options.views.week !== void 0;
670
+ },
671
+ hasDayView() {
672
+ return "day" in _options.views && _options.views.day !== void 0;
673
+ },
664
674
  store
665
675
  };
666
676
  return calendar;
package/dist/index.d.cts CHANGED
@@ -1,44 +1,44 @@
1
1
  import * as _tanstack_store from '@tanstack/store';
2
2
  import { Temporal } from '@js-temporal/polyfill';
3
3
 
4
- type CalendarAccessor<T> = {
5
- getDate: (item: T) => Temporal.PlainDate;
6
- getStart?: (item: T) => Temporal.ZonedDateTime;
7
- getEnd?: (item: T) => Temporal.ZonedDateTime;
4
+ type CalendarAccessor<TItem> = {
5
+ getDate: (item: TItem) => Temporal.PlainDate;
6
+ getStart?: (item: TItem) => Temporal.ZonedDateTime;
7
+ getEnd?: (item: TItem) => Temporal.ZonedDateTime;
8
8
  };
9
- type CalendarDay<T = unknown> = {
9
+ type CalendarDay<TItem = unknown> = {
10
10
  date: Temporal.PlainDate;
11
11
  isCurrentMonth: boolean;
12
12
  isToday: boolean;
13
- items: T[];
13
+ items: TItem[];
14
14
  };
15
- type CalendarWeek<T = unknown> = CalendarDay<T>[];
16
- type CalendarMonth<T = unknown> = {
17
- weeks: CalendarWeek<T>[];
15
+ type CalendarWeek<TItem = unknown> = CalendarDay<TItem>[];
16
+ type CalendarMonth<TItem = unknown> = {
17
+ weeks: CalendarWeek<TItem>[];
18
18
  month: Temporal.PlainYearMonth;
19
19
  };
20
- type WeekDay<T = unknown> = {
20
+ type WeekDay<TItem = unknown> = {
21
21
  date: Temporal.PlainDate;
22
22
  isToday: boolean;
23
- items: T[];
24
- timeSlots?: TimeSlot<T>[];
23
+ items: TItem[];
24
+ timeSlots?: TimeSlot<TItem>[];
25
25
  };
26
- type CalendarWeekView<T = unknown> = {
27
- days: WeekDay<T>[];
26
+ type CalendarWeekView<TItem = unknown> = {
27
+ days: WeekDay<TItem>[];
28
28
  weekStart: Temporal.PlainDate;
29
29
  weekEnd: Temporal.PlainDate;
30
30
  };
31
- type TimeSlot<T = unknown> = {
31
+ type TimeSlot<TItem = unknown> = {
32
32
  hour: number;
33
33
  minute: number;
34
34
  time: Temporal.PlainTime;
35
- items: T[];
35
+ items: TItem[];
36
36
  };
37
- type CalendarDayView<T = unknown> = {
37
+ type CalendarDayView<TItem = unknown> = {
38
38
  date: Temporal.PlainDate;
39
39
  isToday: boolean;
40
- timeSlots: TimeSlot<T>[];
41
- items: T[];
40
+ timeSlots: TimeSlot<TItem>[];
41
+ items: TItem[];
42
42
  };
43
43
  type DateRange = {
44
44
  start: Temporal.ZonedDateTime;
@@ -50,31 +50,31 @@ type CalendarState = {
50
50
  currentView?: string;
51
51
  dateRange: DateRange;
52
52
  };
53
- type MonthViewOptions<T> = {
54
- accessor: CalendarAccessor<T>;
53
+ type MonthViewOptions<TItem> = {
54
+ accessor: CalendarAccessor<TItem>;
55
55
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
56
56
  };
57
- type WeekViewOptions<T> = {
58
- accessor: CalendarAccessor<T>;
57
+ type WeekViewOptions<TItem> = {
58
+ accessor: CalendarAccessor<TItem>;
59
59
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
60
60
  startHour?: number;
61
61
  endHour?: number;
62
62
  slotDuration?: number;
63
63
  };
64
- type DayViewOptions<T> = {
65
- accessor: CalendarAccessor<T>;
64
+ type DayViewOptions<TItem> = {
65
+ accessor: CalendarAccessor<TItem>;
66
66
  startHour?: number;
67
67
  endHour?: number;
68
68
  slotDuration?: number;
69
69
  };
70
- type CalendarViewOptions<T> = {
71
- month?: MonthViewOptions<T>;
72
- week?: WeekViewOptions<T>;
73
- day?: DayViewOptions<T>;
70
+ type CalendarViewOptions<TItem> = {
71
+ month?: MonthViewOptions<TItem>;
72
+ week?: WeekViewOptions<TItem>;
73
+ day?: DayViewOptions<TItem>;
74
74
  };
75
- type CalendarOptions<T> = {
76
- data: T[];
77
- views: CalendarViewOptions<T>;
75
+ type CalendarOptions<TItem> = {
76
+ data: TItem[];
77
+ views: CalendarViewOptions<TItem>;
78
78
  timeZone?: 'UTC' | string;
79
79
  state?: Partial<CalendarState>;
80
80
  onStateChange?: (updater: Updater<CalendarState>) => void;
@@ -82,8 +82,8 @@ type CalendarOptions<T> = {
82
82
  type HasView<Options, View extends 'month' | 'week' | 'day'> = Options extends {
83
83
  views: infer V;
84
84
  } ? V extends Record<View, unknown> ? V[View] extends undefined ? false : true : false : false;
85
- type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
86
- getMonth(): CalendarMonth<T>;
85
+ type MonthMethods<TItem, TOptions> = HasView<TOptions, 'month'> extends true ? {
86
+ getMonth(): CalendarMonth<TItem>;
87
87
  nextMonth(): void;
88
88
  previousMonth(): void;
89
89
  goToMonth(year: number, month: number): void;
@@ -93,8 +93,8 @@ type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
93
93
  previousMonth?: never;
94
94
  goToMonth?: never;
95
95
  };
96
- type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
97
- getWeek(): CalendarWeekView<T>;
96
+ type WeekMethods<TItem, TOptions> = HasView<TOptions, 'week'> extends true ? {
97
+ getWeek(): CalendarWeekView<TItem>;
98
98
  nextWeek(): void;
99
99
  previousWeek(): void;
100
100
  } : {
@@ -102,8 +102,8 @@ type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
102
102
  nextWeek?: never;
103
103
  previousWeek?: never;
104
104
  };
105
- type DayMethods<T, Options> = HasView<Options, 'day'> extends true ? {
106
- getDay(): CalendarDayView<T>;
105
+ type DayMethods<TItem, TOptions> = HasView<TOptions, 'day'> extends true ? {
106
+ getDay(): CalendarDayView<TItem>;
107
107
  nextDay(): void;
108
108
  previousDay(): void;
109
109
  } : {
@@ -129,17 +129,75 @@ type BaseCalendarMethods<Options> = {
129
129
  options: Options;
130
130
  setOptions(updater: (old: Options) => Options): void;
131
131
  store: _tanstack_store.Store<CalendarState>;
132
+ hasMonthView(): boolean;
133
+ hasWeekView(): boolean;
134
+ hasDayView(): boolean;
132
135
  };
133
- type ViewsToOptions<T, V extends CalendarViewOptions<T>> = {
134
- data: T[];
135
- views: V;
136
+ type ViewsToOptions<TItem, TViews extends CalendarViewOptions<TItem>> = {
137
+ data: TItem[];
138
+ views: TViews;
136
139
  };
137
- type Calendar<T, OptionsOrViews = CalendarOptions<T>> = OptionsOrViews extends CalendarOptions<T> ? BaseCalendarMethods<OptionsOrViews> & MonthMethods<T, OptionsOrViews> & WeekMethods<T, OptionsOrViews> & DayMethods<T, OptionsOrViews> : OptionsOrViews extends CalendarViewOptions<T> ? BaseCalendarMethods<ViewsToOptions<T, OptionsOrViews>> & MonthMethods<T, ViewsToOptions<T, OptionsOrViews>> & WeekMethods<T, ViewsToOptions<T, OptionsOrViews>> & DayMethods<T, ViewsToOptions<T, OptionsOrViews>> : BaseCalendarMethods<CalendarOptions<T>> & MonthMethods<T, CalendarOptions<T>> & WeekMethods<T, CalendarOptions<T>> & DayMethods<T, CalendarOptions<T>>;
140
+ type Calendar<TItem, TOptionsOrViews = CalendarOptions<TItem>> = TOptionsOrViews extends CalendarOptions<TItem> ? BaseCalendarMethods<TOptionsOrViews> & MonthMethods<TItem, TOptionsOrViews> & WeekMethods<TItem, TOptionsOrViews> & DayMethods<TItem, TOptionsOrViews> : TOptionsOrViews extends CalendarViewOptions<TItem> ? BaseCalendarMethods<ViewsToOptions<TItem, TOptionsOrViews>> & MonthMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & WeekMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & DayMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> : BaseCalendarMethods<CalendarOptions<TItem>> & MonthMethods<TItem, CalendarOptions<TItem>> & WeekMethods<TItem, CalendarOptions<TItem>> & DayMethods<TItem, CalendarOptions<TItem>>;
138
141
  type InferViewNames<V extends CalendarViewOptions<unknown>> = keyof V & string;
139
142
  type InferViewConfig<V extends CalendarViewOptions<unknown>, Name extends keyof V> = V[Name] extends infer Config ? {
140
143
  name: Name;
141
144
  config: Config;
142
145
  } : never;
146
+ /**
147
+ * Extract the item type from a Calendar type
148
+ * @example type Item = CalendarItemType<typeof calendar>; // Post
149
+ */
150
+ type CalendarItemType<C extends Calendar<any, any>> = C extends Calendar<infer TItem, any> ? TItem : never;
151
+ /**
152
+ * Extract the views configuration from a Calendar type
153
+ * @example type Views = CalendarViewsConfig<typeof calendar>;
154
+ */
155
+ type CalendarViewsConfig<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? TOptions extends {
156
+ views: infer V;
157
+ } ? V : TOptions extends CalendarViewOptions<any> ? TOptions : never : never;
158
+ /**
159
+ * Check if a Calendar has a specific view configured
160
+ * @example type HasMonth = HasViewType<typeof calendar, 'month'>; // true | false
161
+ */
162
+ type HasViewType<C extends Calendar<any, any>, V extends 'month' | 'week' | 'day'> = C extends Calendar<any, infer TOptions> ? HasView<TOptions, V> : false;
163
+ /**
164
+ * Extract valid view names as a union from a Calendar
165
+ * @example type Views = ValidViewNames<typeof calendar>; // 'month' | 'week'
166
+ */
167
+ type ValidViewNames<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? ValidViews<TOptions> : never;
168
+ /**
169
+ * Extract the item type from CalendarOptions before creating a calendar
170
+ * @example type Item = ItemTypeFromOptions<typeof options>; // Post
171
+ */
172
+ type ItemTypeFromOptions<O extends CalendarOptions<any>> = O extends CalendarOptions<infer TItem> ? TItem : never;
173
+ /**
174
+ * Base props for components that receive a Calendar
175
+ * @example
176
+ * function MyComponent<C extends Calendar<any, any>>(
177
+ * props: CalendarComponentProps<C>
178
+ * ) { ... }
179
+ */
180
+ type CalendarComponentProps<C extends Calendar<any, any>> = {
181
+ calendar: C;
182
+ };
183
+ /**
184
+ * Require that a Calendar has month view configured
185
+ * @example
186
+ * function MonthView<C extends Calendar<any, any>>(
187
+ * props: { calendar: RequireMonthView<C> }
188
+ * ) {
189
+ * // calendar.getMonth() is guaranteed to exist
190
+ * }
191
+ */
192
+ type RequireMonthView<C extends Calendar<any, any>> = HasViewType<C, 'month'> extends true ? C : never;
193
+ /**
194
+ * Require that a Calendar has week view configured
195
+ */
196
+ type RequireWeekView<C extends Calendar<any, any>> = HasViewType<C, 'week'> extends true ? C : never;
197
+ /**
198
+ * Require that a Calendar has day view configured
199
+ */
200
+ type RequireDayView<C extends Calendar<any, any>> = HasViewType<C, 'day'> extends true ? C : never;
143
201
 
144
202
  declare function functionalUpdate<T>(updater: T | ((old: T) => T), input: T): T;
145
203
  declare function createCalendarAccessor<T, A extends CalendarAccessor<T> = CalendarAccessor<T>>(accessor: A): A;
@@ -234,12 +292,12 @@ declare function getWeekDateRange(date: Temporal.PlainDate, timeZone: string, op
234
292
  */
235
293
  declare function getDayDateRange(date: Temporal.PlainDate, timeZone: string): DateRange;
236
294
 
237
- declare function createCalendar<T, Options extends CalendarOptions<T>>(options: Options): Calendar<T, Options>;
295
+ declare function createCalendar<TItem, TOptions extends CalendarOptions<TItem>>(options: TOptions): Calendar<TItem, TOptions>;
238
296
 
239
- declare function createCalendarViews<T>(): <const V extends {
240
- month?: MonthViewOptions<T>;
241
- week?: WeekViewOptions<T>;
242
- day?: DayViewOptions<T>;
243
- }>(views: V) => V;
297
+ declare function createCalendarViews<TItem>(): <const TViews extends {
298
+ month?: MonthViewOptions<TItem>;
299
+ week?: WeekViewOptions<TItem>;
300
+ day?: DayViewOptions<TItem>;
301
+ }>(views: TViews) => TViews;
244
302
 
245
- export { type Calendar, type CalendarAccessor, type CalendarDay, type CalendarDayView, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type InferViewConfig, type InferViewNames, type MonthViewOptions, type TimeSlot, type Updater, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
303
+ export { type Calendar, type CalendarAccessor, type CalendarComponentProps, type CalendarDay, type CalendarDayView, type CalendarItemType, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarViewsConfig, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type HasViewType, type InferViewConfig, type InferViewNames, type ItemTypeFromOptions, type MonthViewOptions, type RequireDayView, type RequireMonthView, type RequireWeekView, type TimeSlot, type Updater, type ValidViewNames, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
package/dist/index.d.ts CHANGED
@@ -1,44 +1,44 @@
1
1
  import * as _tanstack_store from '@tanstack/store';
2
2
  import { Temporal } from '@js-temporal/polyfill';
3
3
 
4
- type CalendarAccessor<T> = {
5
- getDate: (item: T) => Temporal.PlainDate;
6
- getStart?: (item: T) => Temporal.ZonedDateTime;
7
- getEnd?: (item: T) => Temporal.ZonedDateTime;
4
+ type CalendarAccessor<TItem> = {
5
+ getDate: (item: TItem) => Temporal.PlainDate;
6
+ getStart?: (item: TItem) => Temporal.ZonedDateTime;
7
+ getEnd?: (item: TItem) => Temporal.ZonedDateTime;
8
8
  };
9
- type CalendarDay<T = unknown> = {
9
+ type CalendarDay<TItem = unknown> = {
10
10
  date: Temporal.PlainDate;
11
11
  isCurrentMonth: boolean;
12
12
  isToday: boolean;
13
- items: T[];
13
+ items: TItem[];
14
14
  };
15
- type CalendarWeek<T = unknown> = CalendarDay<T>[];
16
- type CalendarMonth<T = unknown> = {
17
- weeks: CalendarWeek<T>[];
15
+ type CalendarWeek<TItem = unknown> = CalendarDay<TItem>[];
16
+ type CalendarMonth<TItem = unknown> = {
17
+ weeks: CalendarWeek<TItem>[];
18
18
  month: Temporal.PlainYearMonth;
19
19
  };
20
- type WeekDay<T = unknown> = {
20
+ type WeekDay<TItem = unknown> = {
21
21
  date: Temporal.PlainDate;
22
22
  isToday: boolean;
23
- items: T[];
24
- timeSlots?: TimeSlot<T>[];
23
+ items: TItem[];
24
+ timeSlots?: TimeSlot<TItem>[];
25
25
  };
26
- type CalendarWeekView<T = unknown> = {
27
- days: WeekDay<T>[];
26
+ type CalendarWeekView<TItem = unknown> = {
27
+ days: WeekDay<TItem>[];
28
28
  weekStart: Temporal.PlainDate;
29
29
  weekEnd: Temporal.PlainDate;
30
30
  };
31
- type TimeSlot<T = unknown> = {
31
+ type TimeSlot<TItem = unknown> = {
32
32
  hour: number;
33
33
  minute: number;
34
34
  time: Temporal.PlainTime;
35
- items: T[];
35
+ items: TItem[];
36
36
  };
37
- type CalendarDayView<T = unknown> = {
37
+ type CalendarDayView<TItem = unknown> = {
38
38
  date: Temporal.PlainDate;
39
39
  isToday: boolean;
40
- timeSlots: TimeSlot<T>[];
41
- items: T[];
40
+ timeSlots: TimeSlot<TItem>[];
41
+ items: TItem[];
42
42
  };
43
43
  type DateRange = {
44
44
  start: Temporal.ZonedDateTime;
@@ -50,31 +50,31 @@ type CalendarState = {
50
50
  currentView?: string;
51
51
  dateRange: DateRange;
52
52
  };
53
- type MonthViewOptions<T> = {
54
- accessor: CalendarAccessor<T>;
53
+ type MonthViewOptions<TItem> = {
54
+ accessor: CalendarAccessor<TItem>;
55
55
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
56
56
  };
57
- type WeekViewOptions<T> = {
58
- accessor: CalendarAccessor<T>;
57
+ type WeekViewOptions<TItem> = {
58
+ accessor: CalendarAccessor<TItem>;
59
59
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
60
60
  startHour?: number;
61
61
  endHour?: number;
62
62
  slotDuration?: number;
63
63
  };
64
- type DayViewOptions<T> = {
65
- accessor: CalendarAccessor<T>;
64
+ type DayViewOptions<TItem> = {
65
+ accessor: CalendarAccessor<TItem>;
66
66
  startHour?: number;
67
67
  endHour?: number;
68
68
  slotDuration?: number;
69
69
  };
70
- type CalendarViewOptions<T> = {
71
- month?: MonthViewOptions<T>;
72
- week?: WeekViewOptions<T>;
73
- day?: DayViewOptions<T>;
70
+ type CalendarViewOptions<TItem> = {
71
+ month?: MonthViewOptions<TItem>;
72
+ week?: WeekViewOptions<TItem>;
73
+ day?: DayViewOptions<TItem>;
74
74
  };
75
- type CalendarOptions<T> = {
76
- data: T[];
77
- views: CalendarViewOptions<T>;
75
+ type CalendarOptions<TItem> = {
76
+ data: TItem[];
77
+ views: CalendarViewOptions<TItem>;
78
78
  timeZone?: 'UTC' | string;
79
79
  state?: Partial<CalendarState>;
80
80
  onStateChange?: (updater: Updater<CalendarState>) => void;
@@ -82,8 +82,8 @@ type CalendarOptions<T> = {
82
82
  type HasView<Options, View extends 'month' | 'week' | 'day'> = Options extends {
83
83
  views: infer V;
84
84
  } ? V extends Record<View, unknown> ? V[View] extends undefined ? false : true : false : false;
85
- type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
86
- getMonth(): CalendarMonth<T>;
85
+ type MonthMethods<TItem, TOptions> = HasView<TOptions, 'month'> extends true ? {
86
+ getMonth(): CalendarMonth<TItem>;
87
87
  nextMonth(): void;
88
88
  previousMonth(): void;
89
89
  goToMonth(year: number, month: number): void;
@@ -93,8 +93,8 @@ type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
93
93
  previousMonth?: never;
94
94
  goToMonth?: never;
95
95
  };
96
- type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
97
- getWeek(): CalendarWeekView<T>;
96
+ type WeekMethods<TItem, TOptions> = HasView<TOptions, 'week'> extends true ? {
97
+ getWeek(): CalendarWeekView<TItem>;
98
98
  nextWeek(): void;
99
99
  previousWeek(): void;
100
100
  } : {
@@ -102,8 +102,8 @@ type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
102
102
  nextWeek?: never;
103
103
  previousWeek?: never;
104
104
  };
105
- type DayMethods<T, Options> = HasView<Options, 'day'> extends true ? {
106
- getDay(): CalendarDayView<T>;
105
+ type DayMethods<TItem, TOptions> = HasView<TOptions, 'day'> extends true ? {
106
+ getDay(): CalendarDayView<TItem>;
107
107
  nextDay(): void;
108
108
  previousDay(): void;
109
109
  } : {
@@ -129,17 +129,75 @@ type BaseCalendarMethods<Options> = {
129
129
  options: Options;
130
130
  setOptions(updater: (old: Options) => Options): void;
131
131
  store: _tanstack_store.Store<CalendarState>;
132
+ hasMonthView(): boolean;
133
+ hasWeekView(): boolean;
134
+ hasDayView(): boolean;
132
135
  };
133
- type ViewsToOptions<T, V extends CalendarViewOptions<T>> = {
134
- data: T[];
135
- views: V;
136
+ type ViewsToOptions<TItem, TViews extends CalendarViewOptions<TItem>> = {
137
+ data: TItem[];
138
+ views: TViews;
136
139
  };
137
- type Calendar<T, OptionsOrViews = CalendarOptions<T>> = OptionsOrViews extends CalendarOptions<T> ? BaseCalendarMethods<OptionsOrViews> & MonthMethods<T, OptionsOrViews> & WeekMethods<T, OptionsOrViews> & DayMethods<T, OptionsOrViews> : OptionsOrViews extends CalendarViewOptions<T> ? BaseCalendarMethods<ViewsToOptions<T, OptionsOrViews>> & MonthMethods<T, ViewsToOptions<T, OptionsOrViews>> & WeekMethods<T, ViewsToOptions<T, OptionsOrViews>> & DayMethods<T, ViewsToOptions<T, OptionsOrViews>> : BaseCalendarMethods<CalendarOptions<T>> & MonthMethods<T, CalendarOptions<T>> & WeekMethods<T, CalendarOptions<T>> & DayMethods<T, CalendarOptions<T>>;
140
+ type Calendar<TItem, TOptionsOrViews = CalendarOptions<TItem>> = TOptionsOrViews extends CalendarOptions<TItem> ? BaseCalendarMethods<TOptionsOrViews> & MonthMethods<TItem, TOptionsOrViews> & WeekMethods<TItem, TOptionsOrViews> & DayMethods<TItem, TOptionsOrViews> : TOptionsOrViews extends CalendarViewOptions<TItem> ? BaseCalendarMethods<ViewsToOptions<TItem, TOptionsOrViews>> & MonthMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & WeekMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & DayMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> : BaseCalendarMethods<CalendarOptions<TItem>> & MonthMethods<TItem, CalendarOptions<TItem>> & WeekMethods<TItem, CalendarOptions<TItem>> & DayMethods<TItem, CalendarOptions<TItem>>;
138
141
  type InferViewNames<V extends CalendarViewOptions<unknown>> = keyof V & string;
139
142
  type InferViewConfig<V extends CalendarViewOptions<unknown>, Name extends keyof V> = V[Name] extends infer Config ? {
140
143
  name: Name;
141
144
  config: Config;
142
145
  } : never;
146
+ /**
147
+ * Extract the item type from a Calendar type
148
+ * @example type Item = CalendarItemType<typeof calendar>; // Post
149
+ */
150
+ type CalendarItemType<C extends Calendar<any, any>> = C extends Calendar<infer TItem, any> ? TItem : never;
151
+ /**
152
+ * Extract the views configuration from a Calendar type
153
+ * @example type Views = CalendarViewsConfig<typeof calendar>;
154
+ */
155
+ type CalendarViewsConfig<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? TOptions extends {
156
+ views: infer V;
157
+ } ? V : TOptions extends CalendarViewOptions<any> ? TOptions : never : never;
158
+ /**
159
+ * Check if a Calendar has a specific view configured
160
+ * @example type HasMonth = HasViewType<typeof calendar, 'month'>; // true | false
161
+ */
162
+ type HasViewType<C extends Calendar<any, any>, V extends 'month' | 'week' | 'day'> = C extends Calendar<any, infer TOptions> ? HasView<TOptions, V> : false;
163
+ /**
164
+ * Extract valid view names as a union from a Calendar
165
+ * @example type Views = ValidViewNames<typeof calendar>; // 'month' | 'week'
166
+ */
167
+ type ValidViewNames<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? ValidViews<TOptions> : never;
168
+ /**
169
+ * Extract the item type from CalendarOptions before creating a calendar
170
+ * @example type Item = ItemTypeFromOptions<typeof options>; // Post
171
+ */
172
+ type ItemTypeFromOptions<O extends CalendarOptions<any>> = O extends CalendarOptions<infer TItem> ? TItem : never;
173
+ /**
174
+ * Base props for components that receive a Calendar
175
+ * @example
176
+ * function MyComponent<C extends Calendar<any, any>>(
177
+ * props: CalendarComponentProps<C>
178
+ * ) { ... }
179
+ */
180
+ type CalendarComponentProps<C extends Calendar<any, any>> = {
181
+ calendar: C;
182
+ };
183
+ /**
184
+ * Require that a Calendar has month view configured
185
+ * @example
186
+ * function MonthView<C extends Calendar<any, any>>(
187
+ * props: { calendar: RequireMonthView<C> }
188
+ * ) {
189
+ * // calendar.getMonth() is guaranteed to exist
190
+ * }
191
+ */
192
+ type RequireMonthView<C extends Calendar<any, any>> = HasViewType<C, 'month'> extends true ? C : never;
193
+ /**
194
+ * Require that a Calendar has week view configured
195
+ */
196
+ type RequireWeekView<C extends Calendar<any, any>> = HasViewType<C, 'week'> extends true ? C : never;
197
+ /**
198
+ * Require that a Calendar has day view configured
199
+ */
200
+ type RequireDayView<C extends Calendar<any, any>> = HasViewType<C, 'day'> extends true ? C : never;
143
201
 
144
202
  declare function functionalUpdate<T>(updater: T | ((old: T) => T), input: T): T;
145
203
  declare function createCalendarAccessor<T, A extends CalendarAccessor<T> = CalendarAccessor<T>>(accessor: A): A;
@@ -234,12 +292,12 @@ declare function getWeekDateRange(date: Temporal.PlainDate, timeZone: string, op
234
292
  */
235
293
  declare function getDayDateRange(date: Temporal.PlainDate, timeZone: string): DateRange;
236
294
 
237
- declare function createCalendar<T, Options extends CalendarOptions<T>>(options: Options): Calendar<T, Options>;
295
+ declare function createCalendar<TItem, TOptions extends CalendarOptions<TItem>>(options: TOptions): Calendar<TItem, TOptions>;
238
296
 
239
- declare function createCalendarViews<T>(): <const V extends {
240
- month?: MonthViewOptions<T>;
241
- week?: WeekViewOptions<T>;
242
- day?: DayViewOptions<T>;
243
- }>(views: V) => V;
297
+ declare function createCalendarViews<TItem>(): <const TViews extends {
298
+ month?: MonthViewOptions<TItem>;
299
+ week?: WeekViewOptions<TItem>;
300
+ day?: DayViewOptions<TItem>;
301
+ }>(views: TViews) => TViews;
244
302
 
245
- export { type Calendar, type CalendarAccessor, type CalendarDay, type CalendarDayView, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type InferViewConfig, type InferViewNames, type MonthViewOptions, type TimeSlot, type Updater, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
303
+ export { type Calendar, type CalendarAccessor, type CalendarComponentProps, type CalendarDay, type CalendarDayView, type CalendarItemType, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarViewsConfig, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type HasViewType, type InferViewConfig, type InferViewNames, type ItemTypeFromOptions, type MonthViewOptions, type RequireDayView, type RequireMonthView, type RequireWeekView, type TimeSlot, type Updater, type ValidViewNames, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
package/dist/index.js CHANGED
@@ -607,6 +607,16 @@ function createCalendar(options) {
607
607
  setOptions(updater) {
608
608
  _options = functionalUpdate(updater, _options);
609
609
  },
610
+ // Type predicates for runtime view checking
611
+ hasMonthView() {
612
+ return "month" in _options.views && _options.views.month !== void 0;
613
+ },
614
+ hasWeekView() {
615
+ return "week" in _options.views && _options.views.week !== void 0;
616
+ },
617
+ hasDayView() {
618
+ return "day" in _options.views && _options.views.day !== void 0;
619
+ },
610
620
  store
611
621
  };
612
622
  return calendar;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gobrand/calendar-core",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Lightweight utility library for building calendars using the Temporal API",
5
5
  "private": false,
6
6
  "publishConfig": {