@dereekb/date 13.0.7 → 13.2.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.
Files changed (40) hide show
  1. package/index.cjs.js +3526 -993
  2. package/index.esm.js +3515 -983
  3. package/package.json +5 -6
  4. package/src/lib/date/date.calendar.d.ts +63 -9
  5. package/src/lib/date/date.cell.d.ts +203 -104
  6. package/src/lib/date/date.cell.factory.d.ts +319 -86
  7. package/src/lib/date/date.cell.filter.d.ts +104 -2
  8. package/src/lib/date/date.cell.index.d.ts +202 -92
  9. package/src/lib/date/date.cell.schedule.d.ts +285 -102
  10. package/src/lib/date/date.cell.schedule.day.d.ts +13 -3
  11. package/src/lib/date/date.cell.validator.d.ts +17 -8
  12. package/src/lib/date/date.cell.week.d.ts +24 -3
  13. package/src/lib/date/date.d.ts +481 -54
  14. package/src/lib/date/date.day.d.ts +139 -49
  15. package/src/lib/date/date.duration.d.ts +49 -11
  16. package/src/lib/date/date.format.d.ts +355 -36
  17. package/src/lib/date/date.hashset.d.ts +11 -0
  18. package/src/lib/date/date.logical.d.ts +61 -3
  19. package/src/lib/date/date.range.d.ts +355 -77
  20. package/src/lib/date/date.range.string.d.ts +39 -0
  21. package/src/lib/date/date.range.timezone.d.ts +18 -6
  22. package/src/lib/date/date.round.d.ts +46 -1
  23. package/src/lib/date/date.rxjs.d.ts +29 -7
  24. package/src/lib/date/date.sort.d.ts +36 -9
  25. package/src/lib/date/date.time.d.ts +197 -26
  26. package/src/lib/date/date.time.limit.d.ts +67 -4
  27. package/src/lib/date/date.time.minute.d.ts +269 -30
  28. package/src/lib/date/date.timezone.d.ts +286 -70
  29. package/src/lib/date/date.unix.d.ts +3 -0
  30. package/src/lib/date/date.week.d.ts +115 -51
  31. package/src/lib/query/query.builder.d.ts +91 -0
  32. package/src/lib/query/query.builder.mongo.d.ts +50 -0
  33. package/src/lib/query/query.filter.d.ts +29 -2
  34. package/src/lib/query/query.request.d.ts +16 -0
  35. package/src/lib/rrule/date.recurrence.d.ts +66 -22
  36. package/src/lib/rrule/date.rrule.d.ts +131 -8
  37. package/src/lib/rrule/date.rrule.extension.d.ts +50 -9
  38. package/src/lib/rrule/date.rrule.parse.d.ts +85 -2
  39. package/src/lib/timezone/timezone.d.ts +102 -2
  40. package/src/lib/timezone/timezone.validator.d.ts +9 -4
@@ -2,18 +2,24 @@ import { type DayOfMonth, type MapFunction, type Maybe, type MonthOfYear, type Y
2
2
  import { type DateOrDateRange } from './date.range';
3
3
  import { DateTimezoneUtcNormalInstance, type DateTimezoneUtcNormalInstanceInput } from './date.timezone';
4
4
  /**
5
- * A Day/Month/Year number combination used to refer to a specific Day on a specific Year.
5
+ * Encodes a calendar date as a single YYYYMMDD integer for efficient date-day comparisons and grouping.
6
6
  *
7
- * 20220101 is January 1st 2022
7
+ * The encoding uses `year * 10000 + month * 100 + day`, so January 1st 2022 becomes `20220101`.
8
+ * This allows simple numeric comparisons to determine chronological order between dates.
8
9
  */
9
10
  export type YearMonthDayCode = number;
10
11
  /**
11
- * Used for default YearMonthDay values
12
+ * Sentinel value representing an unset or unknown date. Used as a default/fallback
13
+ * when a valid YearMonthDayCode is not available.
12
14
  */
13
15
  export declare const UNKNOWN_YEAR_MONTH_DAY_CODE = 0;
16
+ /**
17
+ * Type representing the sentinel unknown/unset YearMonthDayCode value (0).
18
+ */
14
19
  export type UnknownYearMonthDayCode = typeof UNKNOWN_YEAR_MONTH_DAY_CODE;
15
20
  /**
16
- * YearMonthDay values in an object.
21
+ * Decomposed representation of a {@link YearMonthDayCode} as individual year, month, and day fields.
22
+ * Useful when the individual date components need to be accessed or manipulated independently.
17
23
  */
18
24
  export interface YearMonthDayCodePair {
19
25
  day: number;
@@ -21,61 +27,67 @@ export interface YearMonthDayCodePair {
21
27
  year: number;
22
28
  }
23
29
  /**
24
- * Returns the YearNumber for the YearMonthDayCode.
30
+ * Extracts the year component from a {@link YearMonthDayCode} by dividing out the month and day digits.
25
31
  *
26
- * @param yearMonthDayCode
27
- * @returns
32
+ * @param yearMonthDayCode - Encoded YYYYMMDD value to extract the year from
28
33
  */
29
34
  export declare function yearMonthDayCodeYear(yearMonthDayCode: YearMonthDayCode): YearNumber;
30
35
  /**
31
- * Returns the MonthOfYear for the YearMonthDayCode.
36
+ * Extracts the month component (1-12) from a {@link YearMonthDayCode} by isolating the middle two digits.
32
37
  *
33
- * @param yearMonthDayCode
34
- * @returns
38
+ * @param yearMonthDayCode - Encoded YYYYMMDD value to extract the month from
35
39
  */
36
40
  export declare function yearMonthDayCodeMonth(yearMonthDayCode: YearMonthDayCode): MonthOfYear;
37
41
  /**
38
- * Returns the DayOfMonth for the YearMonthDayCode.
42
+ * Extracts the day-of-month component (1-31) from a {@link YearMonthDayCode} by isolating the last two digits.
39
43
  *
40
- * @param yearMonthDayCode
41
- * @returns
44
+ * @param yearMonthDayCode - Encoded YYYYMMDD value to extract the day from
42
45
  */
43
46
  export declare function yearMonthDayCodeDay(yearMonthDayCode: YearMonthDayCode): DayOfMonth;
44
47
  /**
45
- * Returns the YearMonthDayCodePair for the YearMonthDayCode.
48
+ * Decomposes a {@link YearMonthDayCode} into its individual year, month, and day components.
46
49
  *
47
- * @param yearMonthDayCode
48
- * @returns
50
+ * @param yearMonthDayCode - Encoded YYYYMMDD value to decompose
49
51
  */
50
52
  export declare function yearMonthDayCodePair(yearMonthDayCode: YearMonthDayCode): YearMonthDayCodePair;
51
53
  /**
52
- * Creates a YearMonthDayCodePair using the input date and the current timezone.
54
+ * Creates a {@link YearMonthDayCodePair} by reading year, month, and day directly from the Date
55
+ * using the system's local timezone. No timezone normalization is applied here; callers
56
+ * that need timezone-aware conversion should use {@link yearMonthDayCodeFactory} instead.
53
57
  *
54
- * @param date
55
- * @returns
58
+ * @param date - Date to extract components from using local timezone
56
59
  */
57
60
  export declare function yearMonthDayCodePairFromDate(date: Date): YearMonthDayCodePair;
58
61
  /**
59
- * Creates a YearMonthDayCode from the input pair.
62
+ * Encodes a {@link YearMonthDayCodePair} into a single {@link YearMonthDayCode} integer
63
+ * using the formula `year * 10000 + month * 100 + day`.
60
64
  *
61
- * @param pair
62
- * @returns
65
+ * @param pair - Decomposed date components to encode
63
66
  */
64
67
  export declare function yearMonthDayCodeFromPair(pair: YearMonthDayCodePair): YearMonthDayCode;
65
68
  /**
66
- * Creates a YearMonthDayCode from the input Date.
69
+ * Convenience function that converts a Date directly to a {@link YearMonthDayCode}
70
+ * using the system's local timezone. For timezone-aware conversion, use {@link yearMonthDayCodeFactory}.
67
71
  *
68
- * @param date
69
- * @returns
72
+ * @param date - Date to encode using local timezone
70
73
  */
71
74
  export declare function yearMonthDayCodeFromDate(date: Date): YearMonthDayCode;
72
75
  /**
73
- * Used to convert the input to a YearMonthDayCode.
76
+ * Timezone-aware function that converts either a Date or explicit year/month/day components
77
+ * into a {@link YearMonthDayCode}. Exposes its internal {@link DateTimezoneUtcNormalInstance}
78
+ * via `_normal` so related utilities can share the same timezone configuration.
74
79
  */
75
80
  export type YearMonthDayCodeFactory = ((dateOrYear: Date | number, month?: MonthOfYear, day?: DayOfMonth) => YearMonthDayCode) & {
76
81
  _normal: DateTimezoneUtcNormalInstance;
77
82
  };
83
+ /**
84
+ * Accepted timezone input for YearMonthDayCode utilities. Can be either raw configuration
85
+ * for creating a {@link DateTimezoneUtcNormalInstance} or an existing instance to reuse.
86
+ */
78
87
  export type YearMonthDayCodeDateTimezoneInput = DateTimezoneUtcNormalInstanceInput | DateTimezoneUtcNormalInstance;
88
+ /**
89
+ * Configuration for creating timezone-aware YearMonthDayCode utilities.
90
+ */
79
91
  export interface YearMonthDayCodeConfig {
80
92
  /**
81
93
  * (Optional) Input timezone configuration for a DateTimezoneUtcNormalInstance.
@@ -84,77 +96,155 @@ export interface YearMonthDayCodeConfig {
84
96
  */
85
97
  timezone?: YearMonthDayCodeDateTimezoneInput;
86
98
  }
99
+ /**
100
+ * Resolves a {@link YearMonthDayCodeDateTimezoneInput} into a {@link DateTimezoneUtcNormalInstance},
101
+ * falling back to the system timezone when no input is provided.
102
+ *
103
+ * @param input - Timezone configuration or existing instance to resolve
104
+ */
87
105
  export declare function yearMonthDayCodeDateTimezoneInstance(input: YearMonthDayCodeDateTimezoneInput): DateTimezoneUtcNormalInstance;
88
106
  /**
89
- * Returns the yearMonthDayCode for the input Date or Year/Month/Day combo.
107
+ * Converts a Date or explicit year/month/day components into a {@link YearMonthDayCode}
108
+ * using the system timezone. This is the simplest entry point for one-off conversions;
109
+ * for repeated conversions or custom timezones, prefer {@link yearMonthDayCodeFactory}.
90
110
  *
91
- * The date is expected to be relative to UTC.
111
+ * @example
112
+ * ```ts
113
+ * // From a Date
114
+ * const code = yearMonthDayCode(new Date(2022, 0, 1)); // 20220101
92
115
  *
93
- * @param date
116
+ * // From explicit components (year, month 1-12, day)
117
+ * const code2 = yearMonthDayCode(2022, 1, 15); // 20220115
118
+ * ```
94
119
  */
95
120
  export declare function yearMonthDayCode(date: Date): YearMonthDayCode;
96
121
  export declare function yearMonthDayCode(year: number, month: MonthOfYear, day: DayOfMonth): YearMonthDayCode;
97
122
  /**
98
- * Creates a YearMonthDayCodeFactory using the optional input config.
123
+ * Creates a reusable, timezone-aware {@link YearMonthDayCodeFactory}. The factory normalizes
124
+ * dates to the configured timezone before encoding, ensuring consistent day boundaries
125
+ * regardless of the system's local timezone.
99
126
  *
100
- * @param config
101
- * @returns
127
+ * @example
128
+ * ```ts
129
+ * // Factory for America/Denver timezone
130
+ * const toCode = yearMonthDayCodeFactory({ timezone: { timezone: 'America/Denver' } });
131
+ * const code = toCode(new Date('2022-01-02T01:00:00Z')); // 20220101 (still Jan 1 in Denver)
132
+ *
133
+ * // From explicit components (no timezone normalization needed)
134
+ * const code2 = toCode(2022, 6, 15); // 20220615
135
+ * ```
136
+ *
137
+ * @param config - Optional timezone configuration; defaults to system timezone
102
138
  */
103
139
  export declare function yearMonthDayCodeFactory(config?: YearMonthDayCodeConfig): YearMonthDayCodeFactory;
104
140
  /**
105
- * Used for returning an array of YearMonthDayCode values for a pre-configured date range.
141
+ * Pre-configured function that produces an array of {@link YearMonthDayCode} values
142
+ * for every day within a given date range, using consistent timezone normalization.
106
143
  */
107
144
  export type YearMonthDayCodesForDateRangeFactory = (dateOrDateRange: DateOrDateRange) => YearMonthDayCode[];
108
145
  /**
109
- * Returns the yearMonthDayCodes for the input Date's calendar month.
146
+ * Returns a {@link YearMonthDayCode} for every day in the given date range using the system timezone.
147
+ * For a single Date input, the range covers that date's full calendar month.
148
+ * For repeated use or custom timezones, prefer {@link yearMonthDayCodesForDateRangeFactory}.
110
149
  *
111
- * The date is expected to be relative to UTC.
150
+ * @example
151
+ * ```ts
152
+ * // Get codes for a 3-day range
153
+ * const codes = yearMonthDayCodesForDateRange({
154
+ * start: new Date(2022, 0, 1),
155
+ * end: new Date(2022, 0, 3)
156
+ * });
157
+ * // [20220101, 20220102, 20220103]
158
+ * ```
112
159
  *
113
- * @param date
160
+ * @param dateOrDateRange - A single Date (expanded to its calendar month) or an explicit date range
114
161
  */
115
162
  export declare function yearMonthDayCodesForDateRange(dateOrDateRange: DateOrDateRange): YearMonthDayCode[];
116
163
  /**
117
- * Create a YearMonthDayCodesForDateRangeFactory.
164
+ * Creates a reusable {@link YearMonthDayCodesForDateRangeFactory} that enumerates every day
165
+ * in a date range and returns the corresponding {@link YearMonthDayCode} values.
166
+ * Shares timezone normalization with the provided factory for consistent day boundaries.
118
167
  *
119
- * @param factory
120
- * @returns
168
+ * @param factory - YearMonthDayCodeFactory to use for encoding; defaults to system timezone
121
169
  */
122
170
  export declare function yearMonthDayCodesForDateRangeFactory(factory?: YearMonthDayCodeFactory): YearMonthDayCodesForDateRangeFactory;
123
171
  /**
124
- * Used to convert the input to a YearMonthDayCode.
172
+ * Converts a {@link YearMonthDayCode} back into a Date, applying timezone normalization
173
+ * to produce a Date representing midnight of that day in the configured timezone.
125
174
  */
126
175
  export type YearMonthDayCodeDateFactory = (yearMonthDayCode: YearMonthDayCode) => Date;
176
+ /**
177
+ * Configuration for {@link yearMonthDayCodeDateFactory}, controlling which timezone
178
+ * the resulting Date objects are normalized to.
179
+ */
127
180
  export type YearMonthDayCodeDateConfig = Pick<YearMonthDayCodeConfig, 'timezone'>;
128
181
  /**
129
- * Creates a YearMonthDayCodeDateFactory using the optional input config.
182
+ * Creates a {@link YearMonthDayCodeDateFactory} that decodes a {@link YearMonthDayCode} back
183
+ * into a Date at midnight in the configured timezone. This is the inverse of
184
+ * {@link yearMonthDayCodeFactory}: `yearMonthDayCodeDateFactory(cfg)(yearMonthDayCodeFactory(cfg)(date))`
185
+ * returns a Date representing midnight of the original date's day.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * const toDate = yearMonthDayCodeDateFactory({ timezone: { timezone: 'America/Denver' } });
190
+ * const date = toDate(20220115); // Date representing Jan 15, 2022 midnight in Denver
191
+ * ```
130
192
  *
131
- * @param config
132
- * @returns
193
+ * @param config - Optional timezone configuration; defaults to system timezone
133
194
  */
134
195
  export declare function yearMonthDayCodeDateFactory(config?: YearMonthDayCodeDateConfig): YearMonthDayCodeDateFactory;
135
196
  /**
136
- *
197
+ * A group of items that share the same {@link YearMonthDayCode}, produced by
198
+ * {@link yearMonthDayCodeGroupFactory}. Useful for rendering day-based groupings
199
+ * such as activity feeds or calendar views.
137
200
  */
138
201
  export interface YearMonthDayCodeGroup<B> {
139
202
  readonly items: B[];
140
203
  readonly dayCode: YearMonthDayCode;
141
204
  }
142
205
  /**
143
- * Used to group the input items into an array of YearMonthDayCodeGroup values.
206
+ * Groups an array of items by their associated day, returning an array of
207
+ * {@link YearMonthDayCodeGroup} entries. Items whose date is null/undefined
208
+ * are grouped under {@link UNKNOWN_YEAR_MONTH_DAY_CODE}.
144
209
  */
145
210
  export type YearMonthDayCodeGroupFactory<B> = (items: B[]) => YearMonthDayCodeGroup<B>[];
146
211
  /**
147
- * MapFunction that reads the relevant date to use for the YearMonthDayCode calculation from the input item.
212
+ * Extracts the Date to use for {@link YearMonthDayCode} grouping from an item.
213
+ * Returning null/undefined causes the item to be placed in the unknown group.
148
214
  */
149
215
  export type YearMonthDayCodeDateReader<B> = MapFunction<B, Maybe<Date>>;
216
+ /**
217
+ * Configuration for {@link yearMonthDayCodeGroupFactory}.
218
+ */
150
219
  export interface YearMonthDayCodeGroupFactoryConfig<B> {
220
+ /**
221
+ * Factory or config used to convert dates to day codes. Accepts either a pre-built
222
+ * {@link YearMonthDayCodeFactory} or raw {@link YearMonthDayCodeConfig} to create one.
223
+ */
151
224
  yearMonthDayCodeFactory?: YearMonthDayCodeFactory | YearMonthDayCodeConfig;
225
+ /**
226
+ * Function that extracts the relevant Date from each item for day-code grouping.
227
+ */
152
228
  dateReader: YearMonthDayCodeDateReader<B>;
153
229
  }
154
230
  /**
155
- * Creates a YearMonthDayCodeGroupFactory.
231
+ * Creates a {@link YearMonthDayCodeGroupFactory} that partitions items into day-based groups.
232
+ * Each item's date is read via the configured `dateReader`, converted to a {@link YearMonthDayCode},
233
+ * and used as the grouping key. Items with no date fall into the `0` (unknown) group.
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * interface Event { name: string; date: Date }
238
+ *
239
+ * const groupByDay = yearMonthDayCodeGroupFactory<Event>({
240
+ * dateReader: (event) => event.date,
241
+ * yearMonthDayCodeFactory: { timezone: { timezone: 'America/Denver' } }
242
+ * });
243
+ *
244
+ * const groups = groupByDay(events);
245
+ * // [{ dayCode: 20220115, items: [...] }, { dayCode: 20220116, items: [...] }]
246
+ * ```
156
247
  *
157
- * @param config
158
- * @returns
248
+ * @param config - Grouping configuration including the date reader and optional timezone
159
249
  */
160
250
  export declare function yearMonthDayCodeGroupFactory<B>(config: YearMonthDayCodeGroupFactoryConfig<B>): YearMonthDayCodeGroupFactory<B>;
@@ -1,31 +1,69 @@
1
1
  import { type DateRelativeState, type FractionalHour, type Minutes, type Maybe } from '@dereekb/util';
2
2
  import { type DateRange } from './date.range';
3
+ /**
4
+ * Represents a time span with a start date and a duration in minutes.
5
+ *
6
+ * Used throughout the date cell system to define when events begin and how long they last.
7
+ */
3
8
  export interface DateDurationSpan {
4
9
  startsAt: Date;
5
10
  duration: Minutes;
6
11
  }
7
- export declare class DateDurationSpan {
12
+ /**
13
+ * ArkType schema for {@link DateDurationSpan}.
14
+ */
15
+ export declare const dateDurationSpanType: import("arktype/internal/variants/object.ts").ObjectType<{
8
16
  startsAt: Date;
9
- duration: Minutes;
10
- constructor(template?: DateDurationSpan);
11
- }
17
+ duration: number;
18
+ }, {}>;
12
19
  /**
13
- * Returns the Date for the end time for the input DateDurationSpan.
20
+ * Computes the end date for a duration span by adding the duration to the start time.
21
+ *
22
+ * @param span - the duration span to compute the end for
23
+ * @returns the date when the span ends
14
24
  *
15
- * @param span
16
- * @returns
25
+ * @example
26
+ * ```ts
27
+ * const span = { startsAt: new Date('2024-01-01T10:00:00Z'), duration: 60 };
28
+ * dateDurationSpanEndDate(span); // 2024-01-01T11:00:00Z
29
+ * ```
17
30
  */
18
31
  export declare function dateDurationSpanEndDate(span: DateDurationSpan): Date;
32
+ /**
33
+ * Converts a {@link DateDurationSpan} to a {@link DateRange} with start and end dates.
34
+ *
35
+ * @param span - the duration span to convert
36
+ * @returns a date range from startsAt to startsAt + duration
37
+ */
19
38
  export declare function durationSpanToDateRange(span: DateDurationSpan): DateRange;
39
+ /**
40
+ * Creates a {@link DateDurationSpan} from a {@link DateRange} by computing the duration in minutes between start and end.
41
+ *
42
+ * @param dateRange - the date range to convert
43
+ * @returns a duration span with the range's start as startsAt
44
+ */
20
45
  export declare function durationSpanFromDateRange(dateRange: DateRange): DateDurationSpan;
46
+ /**
47
+ * Determines whether a duration span is in the past, present, or future relative to the given time.
48
+ *
49
+ * @param span - the duration span to check
50
+ * @param now - reference time (defaults to current time)
51
+ * @returns 'past', 'present', or 'future'
52
+ */
21
53
  export declare function durationSpanDateRelativeState(span: DateDurationSpan, now?: Date): DateRelativeState;
54
+ /**
55
+ * Converts a duration span's duration from minutes to fractional hours.
56
+ *
57
+ * @param span - the duration span to measure
58
+ * @returns the duration expressed as fractional hours (e.g. 90 minutes = 1.5)
59
+ */
22
60
  export declare function fractionalHoursInDurationSpan(span: DateDurationSpan): FractionalHour;
23
61
  /**
24
- * EqualityComparatorFunction for two input DateDurationSpan values.
62
+ * Null-safe equality check for two {@link DateDurationSpan} values, comparing both startsAt and duration.
25
63
  *
26
- * @param a
27
- * @param b
28
- * @returns
64
+ * @param a - first span
65
+ * @param b - second span
66
+ * @returns whether the spans are equal (or both nullish)
29
67
  */
30
68
  export declare function isSameDurationSpan(a: DateDurationSpan, b: DateDurationSpan): boolean;
31
69
  export declare function isSameDurationSpan(a: Maybe<DateDurationSpan>, b: Maybe<DateDurationSpan>): boolean;