@webamoki/web-svelte 2.0.0 → 2.1.1
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.
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { CalendarDate, CalendarDateTime, Time, ZonedDateTime } from '@internationalized/date';
|
|
2
2
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
import { checkOverlap, dateDiffWeeks, datesWithin, formatAbsolute, formatDateFull, formatDateISO, formatDateNum, formatDateShort, formatDayLetter, formatDayShort, formatMonth, formatTimeEnd, formatTimeFull,
|
|
3
|
+
import { checkOverlap, dateDiffWeeks, datesWithin, formatAbsolute, formatDateFull, formatDateISO, formatDateNum, formatDateShort, formatDayLetter, formatDayShort, formatMonth, formatTime, formatTimeEnd, formatTimeFull, getDayIndexOfDate, getDayOfDate, getLastDateOfDay, getLastDatesOfDay, getLastMonths, getNextDateOfDay, isDateDay, LocalDateF } from './index.js';
|
|
4
4
|
const SERVER_TIME_ZONE = 'Europe/London';
|
|
5
5
|
const dt = new LocalDateF(SERVER_TIME_ZONE);
|
|
6
6
|
describe('getDayIndex', () => {
|
|
7
7
|
it('returns the correct day index (0 = Monday)', () => {
|
|
8
8
|
// Test specific dates with known day indices
|
|
9
|
-
expect(
|
|
10
|
-
expect(
|
|
11
|
-
expect(
|
|
12
|
-
expect(
|
|
13
|
-
expect(
|
|
14
|
-
expect(
|
|
15
|
-
expect(
|
|
9
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 1))).toBe(0); // May 1, 2023 was a Monday
|
|
10
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 2))).toBe(1); // May 2, 2023 was a Tuesday
|
|
11
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 3))).toBe(2); // May 3, 2023 was a Wednesday
|
|
12
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 4))).toBe(3); // May 4, 2023 was a Thursday
|
|
13
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 5))).toBe(4); // May 5, 2023 was a Friday
|
|
14
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 6))).toBe(5); // May 6, 2023 was a Saturday
|
|
15
|
+
expect(getDayIndexOfDate(new CalendarDate(2023, 5, 7))).toBe(6); // May 7, 2023 was a Sunday
|
|
16
16
|
});
|
|
17
17
|
});
|
|
18
18
|
describe('getDayOfDate', () => {
|
|
@@ -592,7 +592,7 @@ describe('formatDateNum', () => {
|
|
|
592
592
|
expect(formatDateNum(new CalendarDate(2023, 12, 5))).toBe('05/12/2023');
|
|
593
593
|
});
|
|
594
594
|
});
|
|
595
|
-
describe('
|
|
595
|
+
describe('formatMonthShort', () => {
|
|
596
596
|
it('should format month correctly for all months', () => {
|
|
597
597
|
expect(formatMonth(new CalendarDate(2023, 1, 1))).toBe('Jan 23');
|
|
598
598
|
expect(formatMonth(new CalendarDate(2023, 2, 1))).toBe('Feb 23');
|
|
@@ -612,30 +612,30 @@ describe('formatMonth', () => {
|
|
|
612
612
|
expect(formatMonth(new CalendarDate(2024, 1, 1))).toBe('Jan 24');
|
|
613
613
|
});
|
|
614
614
|
});
|
|
615
|
-
describe('
|
|
615
|
+
describe('formatTime', () => {
|
|
616
616
|
it('should format time correctly with 2-digit hours and 2-digit minutes', () => {
|
|
617
617
|
const time = new Time(14, 30);
|
|
618
|
-
expect(
|
|
618
|
+
expect(formatTime(time)).toBe('14:30');
|
|
619
619
|
});
|
|
620
620
|
it('should pad single digit hours with leading zero', () => {
|
|
621
621
|
const time = new Time(9, 45);
|
|
622
|
-
expect(
|
|
622
|
+
expect(formatTime(time)).toBe('09:45');
|
|
623
623
|
});
|
|
624
624
|
it('should pad single digit minutes with leading zero', () => {
|
|
625
625
|
const time = new Time(12, 5);
|
|
626
|
-
expect(
|
|
626
|
+
expect(formatTime(time)).toBe('12:05');
|
|
627
627
|
});
|
|
628
628
|
it('should pad both single digit hours and minutes with leading zeros', () => {
|
|
629
629
|
const time = new Time(1, 7);
|
|
630
|
-
expect(
|
|
630
|
+
expect(formatTime(time)).toBe('01:07');
|
|
631
631
|
});
|
|
632
632
|
it('should format midnight correctly', () => {
|
|
633
633
|
const time = new Time(0, 0);
|
|
634
|
-
expect(
|
|
634
|
+
expect(formatTime(time)).toBe('00:00');
|
|
635
635
|
});
|
|
636
636
|
it('should format end of day correctly', () => {
|
|
637
637
|
const time = new Time(23, 59);
|
|
638
|
-
expect(
|
|
638
|
+
expect(formatTime(time)).toBe('23:59');
|
|
639
639
|
});
|
|
640
640
|
});
|
|
641
641
|
describe('formatTimeFull', () => {
|
|
@@ -19,11 +19,30 @@ export declare class LocalDateF {
|
|
|
19
19
|
* @returns True if the date is today, false otherwise.
|
|
20
20
|
*/
|
|
21
21
|
isDateToday(date: CalendarDate): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* @returns The current date and time.
|
|
24
|
+
*/
|
|
25
|
+
now(): ZonedDateTime;
|
|
22
26
|
/**
|
|
23
27
|
* @returns The current date.
|
|
24
28
|
*/
|
|
25
29
|
today(): CalendarDate;
|
|
26
30
|
}
|
|
31
|
+
/** Gets the day index of the date */
|
|
32
|
+
export declare function getDayIndexOfDate(date: CalendarDate): number;
|
|
33
|
+
/**
|
|
34
|
+
* Gets the day of the week for a given date.
|
|
35
|
+
* @param date - The date to get the day of the week for.
|
|
36
|
+
* @returns The day of the week
|
|
37
|
+
*/
|
|
38
|
+
export declare function getDayOfDate(date: CalendarDate): Day;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a given date is a specific day of the week.
|
|
41
|
+
* @param date - The date to check.
|
|
42
|
+
* @param dayOfWeek - The day of the week to check against.
|
|
43
|
+
* @returns True if the date is the specified day, false otherwise.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isDateDay(date: CalendarDate, dayOfWeek: Day): boolean;
|
|
27
46
|
/**
|
|
28
47
|
* Checks if two time ranges overlap, boundaries are not considered overlapping.
|
|
29
48
|
* @param start1 - The start time of the first range.
|
|
@@ -41,14 +60,6 @@ export declare function checkOverlap(start1: Time, end1: Time, start2: Time, end
|
|
|
41
60
|
* @returns True if the dates are within duration, false otherwise.
|
|
42
61
|
*/
|
|
43
62
|
export declare function datesWithin(date1: CalendarDate, date2: CalendarDate, duration: DateDuration): boolean;
|
|
44
|
-
/** Gets the day index of the date */
|
|
45
|
-
export declare function getDayIndex(date: CalendarDate): number;
|
|
46
|
-
/**
|
|
47
|
-
* Gets the day of the week for a given date.
|
|
48
|
-
* @param date - The date to get the day of the week for.
|
|
49
|
-
* @returns The day of the week
|
|
50
|
-
*/
|
|
51
|
-
export declare function getDayOfDate(date: CalendarDate): Day;
|
|
52
63
|
/**
|
|
53
64
|
* Gets the most recent occurrence of a day of the week.
|
|
54
65
|
* @param dayOfWeek - The day of the week
|
|
@@ -80,13 +91,6 @@ export declare function getLastMonths(count: number, startDate: CalendarDate): C
|
|
|
80
91
|
* @returns The date of the next occurrence of the specified day.
|
|
81
92
|
*/
|
|
82
93
|
export declare function getNextDateOfDay(dayOfWeek: Day, startDate: CalendarDate): CalendarDate;
|
|
83
|
-
/**
|
|
84
|
-
* Checks if a given date is a specific day of the week.
|
|
85
|
-
* @param date - The date to check.
|
|
86
|
-
* @param dayOfWeek - The day of the week to check against.
|
|
87
|
-
* @returns True if the date is the specified day, false otherwise.
|
|
88
|
-
*/
|
|
89
|
-
export declare function isDateDay(date: CalendarDate, dayOfWeek: Day): boolean;
|
|
90
94
|
/**
|
|
91
95
|
* Calculates the difference in weeks between two dates.
|
|
92
96
|
* @param date1 - The first date in order.
|
|
@@ -143,9 +147,15 @@ export declare function formatDateShort(date: CalendarDate): string;
|
|
|
143
147
|
* Formats the month only.
|
|
144
148
|
* @param date - The date to format.
|
|
145
149
|
* @returns The formatted month string.
|
|
146
|
-
* @example "Oct"
|
|
150
|
+
* @example "Oct 23"
|
|
147
151
|
*/
|
|
148
152
|
export declare function formatMonth(date: CalendarDate): string;
|
|
153
|
+
/**
|
|
154
|
+
* Gives time in HH:MM format
|
|
155
|
+
* @param time
|
|
156
|
+
* @returns string of time in that format
|
|
157
|
+
*/
|
|
158
|
+
export declare function formatTime(time: Time): string;
|
|
149
159
|
/**
|
|
150
160
|
* Calculates the end time given a starting time and duration.
|
|
151
161
|
* @param timeStart starting time
|
|
@@ -159,12 +169,6 @@ export declare function formatTimeEnd(timeStart: Time, durationMinutes: number):
|
|
|
159
169
|
* @returns string of time in that format
|
|
160
170
|
*/
|
|
161
171
|
export declare function formatTimeFull(time: Time): string;
|
|
162
|
-
/**
|
|
163
|
-
* Gives time in HH:MM format
|
|
164
|
-
* @param time
|
|
165
|
-
* @returns string of time in that format
|
|
166
|
-
*/
|
|
167
|
-
export declare function formatTimeShort(time: Time): string;
|
|
168
172
|
/**
|
|
169
173
|
* Unfreezes a CalendarDate object from a snapshot.
|
|
170
174
|
* @param raw - The snapshot of the CalendarDate object.
|
|
@@ -177,4 +181,10 @@ export declare function unfreezeDate(raw: ReturnType<typeof $state.snapshot<Cale
|
|
|
177
181
|
* @returns The unfrozen Time object.
|
|
178
182
|
*/
|
|
179
183
|
export declare function unfreezeTime(raw: ReturnType<typeof $state.snapshot<Time>>): Time;
|
|
184
|
+
/**
|
|
185
|
+
* Unfreezes a ZonedDateTime object from a snapshot.
|
|
186
|
+
* @param date - The snapshot of the ZonedDateTime object.
|
|
187
|
+
* @returns The unfrozen ZonedDateTime object.
|
|
188
|
+
*/
|
|
189
|
+
export declare function unfreezeAbsoluteDate(date: ReturnType<typeof $state.snapshot<ZonedDateTime>>): ZonedDateTime;
|
|
180
190
|
export declare const dateTransport: Transport;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CalendarDate, DateFormatter, fromDate, getDayOfWeek, getLocalTimeZone, startOfMonth, Time, toCalendarDate, today as todayFn, toTime, ZonedDateTime } from '@internationalized/date';
|
|
1
|
+
import { CalendarDate, DateFormatter, fromDate, getDayOfWeek, getLocalTimeZone, now as nowFn, startOfMonth, Time, toCalendarDate, today as todayFn, toTime, ZonedDateTime } from '@internationalized/date';
|
|
2
2
|
import { map, range } from 'ramda';
|
|
3
3
|
const DEFAULT_LOCALE = 'en-GB';
|
|
4
4
|
// Day of the week
|
|
@@ -46,6 +46,12 @@ export class LocalDateF {
|
|
|
46
46
|
isDateToday(date) {
|
|
47
47
|
return this.today().compare(date) === 0;
|
|
48
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* @returns The current date and time.
|
|
51
|
+
*/
|
|
52
|
+
now() {
|
|
53
|
+
return nowFn(this.timezone);
|
|
54
|
+
}
|
|
49
55
|
/**
|
|
50
56
|
* @returns The current date.
|
|
51
57
|
*/
|
|
@@ -53,6 +59,30 @@ export class LocalDateF {
|
|
|
53
59
|
return todayFn(this.timezone);
|
|
54
60
|
}
|
|
55
61
|
}
|
|
62
|
+
/** Gets the day index of the date */
|
|
63
|
+
export function getDayIndexOfDate(date) {
|
|
64
|
+
// Always start 0 on Monday
|
|
65
|
+
return getDayOfWeek(date, DEFAULT_LOCALE, 'mon');
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the day of the week for a given date.
|
|
69
|
+
* @param date - The date to get the day of the week for.
|
|
70
|
+
* @returns The day of the week
|
|
71
|
+
*/
|
|
72
|
+
export function getDayOfDate(date) {
|
|
73
|
+
return Days[getDayIndexOfDate(date)];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Checks if a given date is a specific day of the week.
|
|
77
|
+
* @param date - The date to check.
|
|
78
|
+
* @param dayOfWeek - The day of the week to check against.
|
|
79
|
+
* @returns True if the date is the specified day, false otherwise.
|
|
80
|
+
*/
|
|
81
|
+
export function isDateDay(date, dayOfWeek) {
|
|
82
|
+
const dateDay = getDayOfDate(date);
|
|
83
|
+
return dateDay === dayOfWeek;
|
|
84
|
+
}
|
|
85
|
+
/* Intervals */
|
|
56
86
|
/**
|
|
57
87
|
* Checks if two time ranges overlap, boundaries are not considered overlapping.
|
|
58
88
|
* @param start1 - The start time of the first range.
|
|
@@ -77,19 +107,6 @@ export function datesWithin(date1, date2, duration) {
|
|
|
77
107
|
return false;
|
|
78
108
|
return date1.add(duration).compare(date2) >= 0;
|
|
79
109
|
}
|
|
80
|
-
/** Gets the day index of the date */
|
|
81
|
-
export function getDayIndex(date) {
|
|
82
|
-
// Always start 0 on Monday
|
|
83
|
-
return getDayOfWeek(date, DEFAULT_LOCALE, 'mon');
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Gets the day of the week for a given date.
|
|
87
|
-
* @param date - The date to get the day of the week for.
|
|
88
|
-
* @returns The day of the week
|
|
89
|
-
*/
|
|
90
|
-
export function getDayOfDate(date) {
|
|
91
|
-
return Days[getDayIndex(date)];
|
|
92
|
-
}
|
|
93
110
|
/**
|
|
94
111
|
* Gets the most recent occurrence of a day of the week.
|
|
95
112
|
* @param dayOfWeek - The day of the week
|
|
@@ -99,7 +116,7 @@ export function getDayOfDate(date) {
|
|
|
99
116
|
*/
|
|
100
117
|
export function getLastDateOfDay(dayOfWeek, startDate) {
|
|
101
118
|
const dayIndex = DayIndex[dayOfWeek];
|
|
102
|
-
const startIndex =
|
|
119
|
+
const startIndex = getDayIndexOfDate(startDate);
|
|
103
120
|
// Already on the day
|
|
104
121
|
if (startIndex === dayIndex)
|
|
105
122
|
return startDate;
|
|
@@ -146,7 +163,7 @@ export function getLastMonths(count, startDate) {
|
|
|
146
163
|
*/
|
|
147
164
|
export function getNextDateOfDay(dayOfWeek, startDate) {
|
|
148
165
|
const dayIndex = DayIndex[dayOfWeek];
|
|
149
|
-
const startIndex =
|
|
166
|
+
const startIndex = getDayIndexOfDate(startDate);
|
|
150
167
|
// Already on the day
|
|
151
168
|
if (startIndex === dayIndex)
|
|
152
169
|
return startDate;
|
|
@@ -154,17 +171,6 @@ export function getNextDateOfDay(dayOfWeek, startDate) {
|
|
|
154
171
|
const addition = (dayIndex - startIndex + 7) % 7;
|
|
155
172
|
return startDate.add({ days: addition });
|
|
156
173
|
}
|
|
157
|
-
/* Intervals */
|
|
158
|
-
/**
|
|
159
|
-
* Checks if a given date is a specific day of the week.
|
|
160
|
-
* @param date - The date to check.
|
|
161
|
-
* @param dayOfWeek - The day of the week to check against.
|
|
162
|
-
* @returns True if the date is the specified day, false otherwise.
|
|
163
|
-
*/
|
|
164
|
-
export function isDateDay(date, dayOfWeek) {
|
|
165
|
-
const dateDay = getDayOfDate(date);
|
|
166
|
-
return dateDay === dayOfWeek;
|
|
167
|
-
}
|
|
168
174
|
const msPerWeek = 7 * 24 * 60 * 60 * 1000;
|
|
169
175
|
/**
|
|
170
176
|
* Calculates the difference in weeks between two dates.
|
|
@@ -266,12 +272,22 @@ export function formatDateShort(date) {
|
|
|
266
272
|
* Formats the month only.
|
|
267
273
|
* @param date - The date to format.
|
|
268
274
|
* @returns The formatted month string.
|
|
269
|
-
* @example "Oct"
|
|
275
|
+
* @example "Oct 23"
|
|
270
276
|
*/
|
|
271
277
|
export function formatMonth(date) {
|
|
272
278
|
return formatDate(date, MonthFormatter);
|
|
273
279
|
}
|
|
274
280
|
/* Times */
|
|
281
|
+
/**
|
|
282
|
+
* Gives time in HH:MM format
|
|
283
|
+
* @param time
|
|
284
|
+
* @returns string of time in that format
|
|
285
|
+
*/
|
|
286
|
+
export function formatTime(time) {
|
|
287
|
+
const hours = padNum(time.hour, 2);
|
|
288
|
+
const minutes = padNum(time.minute, 2);
|
|
289
|
+
return `${hours}:${minutes}`;
|
|
290
|
+
}
|
|
275
291
|
/**
|
|
276
292
|
* Calculates the end time given a starting time and duration.
|
|
277
293
|
* @param timeStart starting time
|
|
@@ -280,7 +296,7 @@ export function formatMonth(date) {
|
|
|
280
296
|
*/
|
|
281
297
|
export function formatTimeEnd(timeStart, durationMinutes) {
|
|
282
298
|
const timeEnd = timeStart.add({ minutes: durationMinutes });
|
|
283
|
-
return
|
|
299
|
+
return formatTime(timeEnd);
|
|
284
300
|
}
|
|
285
301
|
/**
|
|
286
302
|
* Gives time in HH:MM:SS format
|
|
@@ -293,16 +309,18 @@ export function formatTimeFull(time) {
|
|
|
293
309
|
const seconds = padNum(time.second, 2);
|
|
294
310
|
return `${hours}:${minutes}:${seconds}`;
|
|
295
311
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
*/
|
|
301
|
-
export function formatTimeShort(time) {
|
|
302
|
-
const hours = padNum(time.hour, 2);
|
|
303
|
-
const minutes = padNum(time.minute, 2);
|
|
304
|
-
return `${hours}:${minutes}`;
|
|
312
|
+
/* Helpers */
|
|
313
|
+
function formatDate(date, formatter) {
|
|
314
|
+
const nativeDate = date.toDate(getLocalTimeZone());
|
|
315
|
+
return formatter.format(nativeDate);
|
|
305
316
|
}
|
|
317
|
+
// Pad number with zeroes to the left
|
|
318
|
+
function padNum(num, len) {
|
|
319
|
+
if (isNaN(num))
|
|
320
|
+
return '0'.repeat(len);
|
|
321
|
+
return num.toString().padStart(len, '0');
|
|
322
|
+
}
|
|
323
|
+
/* State handling */
|
|
306
324
|
/**
|
|
307
325
|
* Unfreezes a CalendarDate object from a snapshot.
|
|
308
326
|
* @param raw - The snapshot of the CalendarDate object.
|
|
@@ -319,16 +337,13 @@ export function unfreezeDate(raw) {
|
|
|
319
337
|
export function unfreezeTime(raw) {
|
|
320
338
|
return new Time(raw.hour, raw.minute, raw.second, raw.millisecond);
|
|
321
339
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (isNaN(num))
|
|
330
|
-
return '0'.repeat(len);
|
|
331
|
-
return num.toString().padStart(len, '0');
|
|
340
|
+
/**
|
|
341
|
+
* Unfreezes a ZonedDateTime object from a snapshot.
|
|
342
|
+
* @param date - The snapshot of the ZonedDateTime object.
|
|
343
|
+
* @returns The unfrozen ZonedDateTime object.
|
|
344
|
+
*/
|
|
345
|
+
export function unfreezeAbsoluteDate(date) {
|
|
346
|
+
return new ZonedDateTime(date.year, date.month, date.day, date.timeZone, date.offset, date.hour, date.minute, date.second, date.millisecond);
|
|
332
347
|
}
|
|
333
348
|
// SerDe
|
|
334
349
|
export const dateTransport = {
|