@webamoki/web-svelte 0.1.0 → 0.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.
- package/README.md +4 -55
- package/dist/components/form/fields/TextField.svelte +10 -2
- package/dist/components/form/fields/TextField.svelte.d.ts +2 -0
- package/dist/utils/datetime/index.d.ts +160 -0
- package/dist/utils/datetime/index.js +310 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -2
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,58 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Setup
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
in the main css file, e.g. `app.css`, add the following line:
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Creating a project
|
|
8
|
-
|
|
9
|
-
If you're seeing this, you've probably already done this step. Congrats!
|
|
10
|
-
|
|
11
|
-
```sh
|
|
12
|
-
# create a new project in the current directory
|
|
13
|
-
npx sv create
|
|
14
|
-
|
|
15
|
-
# create a new project in my-app
|
|
16
|
-
npx sv create my-app
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Developing
|
|
20
|
-
|
|
21
|
-
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
22
|
-
|
|
23
|
-
```sh
|
|
24
|
-
npm run dev
|
|
25
|
-
|
|
26
|
-
# or start the server and open the app in a new browser tab
|
|
27
|
-
npm run dev -- --open
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
|
|
31
|
-
|
|
32
|
-
## Building
|
|
33
|
-
|
|
34
|
-
To build your library:
|
|
35
|
-
|
|
36
|
-
```sh
|
|
37
|
-
npm pack
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
To create a production version of your showcase app:
|
|
41
|
-
|
|
42
|
-
```sh
|
|
43
|
-
npm run build
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
You can preview the production build with `npm run preview`.
|
|
47
|
-
|
|
48
|
-
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
|
49
|
-
|
|
50
|
-
## Publishing
|
|
51
|
-
|
|
52
|
-
Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
|
|
53
|
-
|
|
54
|
-
To publish your library to [npm](https://www.npmjs.com):
|
|
55
|
-
|
|
56
|
-
```sh
|
|
57
|
-
npm publish
|
|
5
|
+
```css
|
|
6
|
+
@source "../...path/node_modules/@webamoki/web-svelte/dist/**/*.{js,svelte,ts}";
|
|
58
7
|
```
|
|
@@ -6,12 +6,20 @@
|
|
|
6
6
|
interface Props extends FieldWrapperProps<T, U, M> {
|
|
7
7
|
value?: string;
|
|
8
8
|
type?: HTMLInputElement['type'];
|
|
9
|
+
class?: string;
|
|
10
|
+
placeholder?: string;
|
|
9
11
|
}
|
|
10
|
-
let {
|
|
12
|
+
let {
|
|
13
|
+
value = $bindable(),
|
|
14
|
+
type = 'text',
|
|
15
|
+
class: className,
|
|
16
|
+
placeholder,
|
|
17
|
+
...fieldProps
|
|
18
|
+
}: Props = $props();
|
|
11
19
|
</script>
|
|
12
20
|
|
|
13
21
|
<FieldWrapper {...fieldProps}>
|
|
14
22
|
{#snippet formElem(props)}
|
|
15
|
-
<Input {type} bind:value {...props} />
|
|
23
|
+
<Input {type} bind:value class={className} {placeholder} {...props} />
|
|
16
24
|
{/snippet}
|
|
17
25
|
</FieldWrapper>
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { CalendarDate, Time, ZonedDateTime, type DateDuration } from '@internationalized/date';
|
|
2
|
+
export declare const Days: readonly ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
|
|
3
|
+
export type Day = (typeof Days)[number];
|
|
4
|
+
/**
|
|
5
|
+
* Gets the day of the week for a given date.
|
|
6
|
+
* @param date - The date to get the day of the week for.
|
|
7
|
+
* @returns The day of the week
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDayOfDate(date: CalendarDate): Day;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a given date is a specific day of the week.
|
|
12
|
+
* @param date - The date to check.
|
|
13
|
+
* @param dayOfWeek - The day of the week to check against.
|
|
14
|
+
* @returns True if the date is the specified day, false otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isDateDay(date: CalendarDate, dayOfWeek: Day): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a given date is today.
|
|
19
|
+
* @param date - The date to check.
|
|
20
|
+
* @returns True if the date is today, false otherwise.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isDateToday(date: CalendarDate, timezone: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Calculates the age from a date of birth.
|
|
25
|
+
* @param dob - The date of birth.
|
|
26
|
+
* @returns The age in years.
|
|
27
|
+
* @throws Error if the date of birth is in the future.
|
|
28
|
+
*/
|
|
29
|
+
export declare function ageFromDob(dob: CalendarDate, timezone: string): number;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the date of the next occurrence of a day of the week.
|
|
32
|
+
* @param dayOfWeek - The day of the week to get the next occurrence for.
|
|
33
|
+
* @param startDate - The date to check from. Inclusive.
|
|
34
|
+
* @returns The date of the next occurrence of the specified day.
|
|
35
|
+
*/
|
|
36
|
+
export declare function getNextDateOfDay(dayOfWeek: Day, startDate: CalendarDate): CalendarDate;
|
|
37
|
+
/**
|
|
38
|
+
* Gets the most recent occurrence of a day of the week.
|
|
39
|
+
* @param dayOfWeek - The day of the week
|
|
40
|
+
* @param startDate - The date to check from. Inclusive.
|
|
41
|
+
* @returns The most recent date for the specified day.
|
|
42
|
+
* @throws An error if the day of the week is invalid.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getLastDateOfDay(dayOfWeek: Day, startDate: CalendarDate): CalendarDate;
|
|
45
|
+
/**
|
|
46
|
+
* Gets an array of the last dates of the day of the week.
|
|
47
|
+
* @param dayOfWeek - The day of the week.
|
|
48
|
+
* @param count - The number of dates to get.
|
|
49
|
+
* @param startDate - The date to check from. Inclusive.
|
|
50
|
+
* @returns The array of dates from oldest to most recent.
|
|
51
|
+
* @throws An error if the day of the week is invalid.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getLastDatesOfDay(dayOfWeek: Day, count: number, startDate: CalendarDate): CalendarDate[];
|
|
54
|
+
/**
|
|
55
|
+
* Gets an array of dates of the last few months (first day) from a date.
|
|
56
|
+
* @param count - The number of months to get.
|
|
57
|
+
* @param startDate - The date to start from (defaults to today).
|
|
58
|
+
* @returns The array of dates from oldest to most recent.
|
|
59
|
+
*/
|
|
60
|
+
export declare function getLastMonths(count: number, startDate: CalendarDate): CalendarDate[];
|
|
61
|
+
/**
|
|
62
|
+
* Checks if two time ranges overlap
|
|
63
|
+
* @param start1 - The start time of the first range.
|
|
64
|
+
* @param end1 - The end time of the first range.
|
|
65
|
+
* @param start2 - The start time of the second range.
|
|
66
|
+
* @param end2 - The end time of the second range.
|
|
67
|
+
* @returns True if the ranges overlap, false otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export declare function checkOverlap(start1: Time, end1: Time, start2: Time, end2: Time): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Determines if the given dates are within the given duration of each other.
|
|
72
|
+
* @param date1 - The first date in order.
|
|
73
|
+
* @param date2 - The second date in order.
|
|
74
|
+
* @param duration - The duration to check against. Inclusive of boundaries.
|
|
75
|
+
* @returns True if the dates are within duration, false otherwise.
|
|
76
|
+
*/
|
|
77
|
+
export declare function datesWithin(date1: CalendarDate, date2: CalendarDate, duration: DateDuration): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Calculates the difference in weeks between two dates.
|
|
80
|
+
* @param date1 - The first date in order.
|
|
81
|
+
* @param date2 - The second date in order.
|
|
82
|
+
*/
|
|
83
|
+
export declare function dateDiffWeeks(date1: CalendarDate, date2: CalendarDate): number;
|
|
84
|
+
/**
|
|
85
|
+
* Formats a day of the week.
|
|
86
|
+
* @param day - The day of the week to format.
|
|
87
|
+
* @example "Monday" -> "Mon"
|
|
88
|
+
* @returns Formatted string of the day of the week.
|
|
89
|
+
*/
|
|
90
|
+
export declare function formatDayShort(day: Day): string;
|
|
91
|
+
/**
|
|
92
|
+
* @param date The CalendarDate object to format.
|
|
93
|
+
* @returns string of date in shortened format
|
|
94
|
+
* @example "Oct 5"
|
|
95
|
+
*/
|
|
96
|
+
export declare function formatDateShort(date: CalendarDate): string;
|
|
97
|
+
/**
|
|
98
|
+
* @param date The CalendarDate object to format.
|
|
99
|
+
* @returns The formatted date string.
|
|
100
|
+
* @example "5 Oct 2023"
|
|
101
|
+
*/
|
|
102
|
+
export declare function formatDateFull(date: CalendarDate): string;
|
|
103
|
+
/**
|
|
104
|
+
* ISO format
|
|
105
|
+
* @param date The CalendarDate object to format.
|
|
106
|
+
* @returns The formatted date string in YYYY-MM-DD format.
|
|
107
|
+
* @example "2023-10-05"
|
|
108
|
+
*/
|
|
109
|
+
export declare function formatDateISO(date: CalendarDate): string;
|
|
110
|
+
/**
|
|
111
|
+
* @param date The CalendarDate object to format.
|
|
112
|
+
* @returns The formatted date string in dd/mm/yyyy format.
|
|
113
|
+
* @example "05/10/2023"
|
|
114
|
+
*/
|
|
115
|
+
export declare function formatDateNum(date: CalendarDate): string;
|
|
116
|
+
/**
|
|
117
|
+
* Formats the month only.
|
|
118
|
+
* @param date - The date to format.
|
|
119
|
+
* @returns The formatted month string.
|
|
120
|
+
* @example "Oct"
|
|
121
|
+
*/
|
|
122
|
+
export declare function formatMonth(date: CalendarDate): string;
|
|
123
|
+
/**
|
|
124
|
+
* Gives time in HH:MM format
|
|
125
|
+
* @param time
|
|
126
|
+
* @returns string of time in that format
|
|
127
|
+
*/
|
|
128
|
+
export declare function formatTimeShort(time: Time): string;
|
|
129
|
+
/**
|
|
130
|
+
* Gives time in HH:MM:SS format
|
|
131
|
+
* @param time
|
|
132
|
+
* @returns string of time in that format
|
|
133
|
+
*/
|
|
134
|
+
export declare function formatTimeFull(time: Time): string;
|
|
135
|
+
/**
|
|
136
|
+
* Calculates the end time given a starting time and duration.
|
|
137
|
+
* @param timeStart starting time
|
|
138
|
+
* @param durationMinutes duration in minutes
|
|
139
|
+
* @returns end time in HH:MM format
|
|
140
|
+
*/
|
|
141
|
+
export declare function formatTimeEnd(timeStart: Time, durationMinutes: number): string;
|
|
142
|
+
/**
|
|
143
|
+
* Formats a full date and time.
|
|
144
|
+
* @param datetime The ZonedDateTime object to format.
|
|
145
|
+
* @returns The formatted date and time string.
|
|
146
|
+
* @example "05/10/2023 14:30:00"
|
|
147
|
+
*/
|
|
148
|
+
export declare function formatAbsolute(datetime: ZonedDateTime): string;
|
|
149
|
+
/**
|
|
150
|
+
* Unfreezes a CalendarDate object from a snapshot.
|
|
151
|
+
* @param raw - The snapshot of the CalendarDate object.
|
|
152
|
+
* @returns The unfrozen CalendarDate object.
|
|
153
|
+
*/
|
|
154
|
+
export declare function unfreezeDate(raw: $state.Snapshot<CalendarDate>): CalendarDate;
|
|
155
|
+
/**
|
|
156
|
+
* Unfreezes a Time object from a snapshot.
|
|
157
|
+
* @param raw - The snapshot of the Time object.
|
|
158
|
+
* @returns The unfrozen Time object.
|
|
159
|
+
*/
|
|
160
|
+
export declare function unfreezeTime(raw: $state.Snapshot<Time>): Time;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { CalendarDate, DateFormatter, getDayOfWeek, getLocalTimeZone, startOfMonth, Time, toCalendarDate, today, toTime, ZonedDateTime } from '@internationalized/date';
|
|
2
|
+
import { map, range } from 'ramda';
|
|
3
|
+
const DEFAULT_TIME_ZONE = 'Europe/London';
|
|
4
|
+
const DEFAULT_LOCALE = 'en-GB';
|
|
5
|
+
// Day of the week
|
|
6
|
+
export const Days = [
|
|
7
|
+
'Monday',
|
|
8
|
+
'Tuesday',
|
|
9
|
+
'Wednesday',
|
|
10
|
+
'Thursday',
|
|
11
|
+
'Friday',
|
|
12
|
+
'Saturday',
|
|
13
|
+
'Sunday'
|
|
14
|
+
];
|
|
15
|
+
const DayIndex = Object.fromEntries(Days.map((day, index) => [day, index]));
|
|
16
|
+
/** Gets the day index of the date */
|
|
17
|
+
function getDayIndex(date) {
|
|
18
|
+
// Always start 0 on Monday
|
|
19
|
+
return getDayOfWeek(date, DEFAULT_LOCALE, 'mon');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Gets the day of the week for a given date.
|
|
23
|
+
* @param date - The date to get the day of the week for.
|
|
24
|
+
* @returns The day of the week
|
|
25
|
+
*/
|
|
26
|
+
export function getDayOfDate(date) {
|
|
27
|
+
return Days[getDayIndex(date)];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a given date is a specific day of the week.
|
|
31
|
+
* @param date - The date to check.
|
|
32
|
+
* @param dayOfWeek - The day of the week to check against.
|
|
33
|
+
* @returns True if the date is the specified day, false otherwise.
|
|
34
|
+
*/
|
|
35
|
+
export function isDateDay(date, dayOfWeek) {
|
|
36
|
+
const dateDay = getDayOfDate(date);
|
|
37
|
+
return dateDay === dayOfWeek;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a given date is today.
|
|
41
|
+
* @param date - The date to check.
|
|
42
|
+
* @returns True if the date is today, false otherwise.
|
|
43
|
+
*/
|
|
44
|
+
export function isDateToday(date, timezone) {
|
|
45
|
+
return today(timezone).compare(date) === 0;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Calculates the age from a date of birth.
|
|
49
|
+
* @param dob - The date of birth.
|
|
50
|
+
* @returns The age in years.
|
|
51
|
+
* @throws Error if the date of birth is in the future.
|
|
52
|
+
*/
|
|
53
|
+
export function ageFromDob(dob, timezone) {
|
|
54
|
+
const todayDate = today(timezone);
|
|
55
|
+
if (todayDate.compare(dob) < 0) {
|
|
56
|
+
throw new Error('Date of birth is in the future');
|
|
57
|
+
}
|
|
58
|
+
let years = todayDate.year - dob.year;
|
|
59
|
+
const monthDiff = todayDate.month - dob.month;
|
|
60
|
+
const dayDiff = todayDate.day - dob.day;
|
|
61
|
+
// Adjust years down if birthday hasn't occurred this year
|
|
62
|
+
if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
|
|
63
|
+
years--;
|
|
64
|
+
}
|
|
65
|
+
return years;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the date of the next occurrence of a day of the week.
|
|
69
|
+
* @param dayOfWeek - The day of the week to get the next occurrence for.
|
|
70
|
+
* @param startDate - The date to check from. Inclusive.
|
|
71
|
+
* @returns The date of the next occurrence of the specified day.
|
|
72
|
+
*/
|
|
73
|
+
export function getNextDateOfDay(dayOfWeek, startDate) {
|
|
74
|
+
const dayIndex = DayIndex[dayOfWeek];
|
|
75
|
+
const startIndex = getDayIndex(startDate);
|
|
76
|
+
// Already on the day
|
|
77
|
+
if (startIndex === dayIndex)
|
|
78
|
+
return startDate;
|
|
79
|
+
// Calculate how many days to add to get to the next occurrence
|
|
80
|
+
const addition = (dayIndex - startIndex + 7) % 7;
|
|
81
|
+
return startDate.add({ days: addition });
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Gets the most recent occurrence of a day of the week.
|
|
85
|
+
* @param dayOfWeek - The day of the week
|
|
86
|
+
* @param startDate - The date to check from. Inclusive.
|
|
87
|
+
* @returns The most recent date for the specified day.
|
|
88
|
+
* @throws An error if the day of the week is invalid.
|
|
89
|
+
*/
|
|
90
|
+
export function getLastDateOfDay(dayOfWeek, startDate) {
|
|
91
|
+
const dayIndex = DayIndex[dayOfWeek];
|
|
92
|
+
const startIndex = getDayIndex(startDate);
|
|
93
|
+
// Already on the day
|
|
94
|
+
if (startIndex === dayIndex)
|
|
95
|
+
return startDate;
|
|
96
|
+
// Calculate how many days to subtract to get to the previous occurrence
|
|
97
|
+
const subtraction = (startIndex + 7 - dayIndex) % 7;
|
|
98
|
+
return startDate.subtract({ days: subtraction });
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Gets an array of the last dates of the day of the week.
|
|
102
|
+
* @param dayOfWeek - The day of the week.
|
|
103
|
+
* @param count - The number of dates to get.
|
|
104
|
+
* @param startDate - The date to check from. Inclusive.
|
|
105
|
+
* @returns The array of dates from oldest to most recent.
|
|
106
|
+
* @throws An error if the day of the week is invalid.
|
|
107
|
+
*/
|
|
108
|
+
export function getLastDatesOfDay(dayOfWeek, count, startDate) {
|
|
109
|
+
// Set up the array of dates
|
|
110
|
+
if (count < 1)
|
|
111
|
+
return [];
|
|
112
|
+
// Get the most recent date
|
|
113
|
+
const latestDate = getLastDateOfDay(dayOfWeek, startDate);
|
|
114
|
+
// Calculate all dates subtracted, oldest -> most recent
|
|
115
|
+
return map((i) => latestDate.subtract({ weeks: count - 1 - i }), range(0, count));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Gets an array of dates of the last few months (first day) from a date.
|
|
119
|
+
* @param count - The number of months to get.
|
|
120
|
+
* @param startDate - The date to start from (defaults to today).
|
|
121
|
+
* @returns The array of dates from oldest to most recent.
|
|
122
|
+
*/
|
|
123
|
+
export function getLastMonths(count, startDate) {
|
|
124
|
+
if (count < 1)
|
|
125
|
+
return [];
|
|
126
|
+
// Get the most recent date
|
|
127
|
+
const latestDate = startOfMonth(startDate);
|
|
128
|
+
// Calculate the previous dates
|
|
129
|
+
return map((i) => latestDate.subtract({ months: count - 1 - i }), range(0, count));
|
|
130
|
+
}
|
|
131
|
+
/* Intervals */
|
|
132
|
+
/**
|
|
133
|
+
* Checks if two time ranges overlap
|
|
134
|
+
* @param start1 - The start time of the first range.
|
|
135
|
+
* @param end1 - The end time of the first range.
|
|
136
|
+
* @param start2 - The start time of the second range.
|
|
137
|
+
* @param end2 - The end time of the second range.
|
|
138
|
+
* @returns True if the ranges overlap, false otherwise.
|
|
139
|
+
*/
|
|
140
|
+
export function checkOverlap(start1, end1, start2, end2) {
|
|
141
|
+
return start1.compare(end2) < 0 && start2.compare(end1) < 0;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Determines if the given dates are within the given duration of each other.
|
|
145
|
+
* @param date1 - The first date in order.
|
|
146
|
+
* @param date2 - The second date in order.
|
|
147
|
+
* @param duration - The duration to check against. Inclusive of boundaries.
|
|
148
|
+
* @returns True if the dates are within duration, false otherwise.
|
|
149
|
+
*/
|
|
150
|
+
export function datesWithin(date1, date2, duration) {
|
|
151
|
+
// reject invalid order
|
|
152
|
+
if (date1.compare(date2) > 0)
|
|
153
|
+
return false;
|
|
154
|
+
return date1.add(duration).compare(date2) >= 0;
|
|
155
|
+
}
|
|
156
|
+
const msPerWeek = 7 * 24 * 60 * 60 * 1000;
|
|
157
|
+
/**
|
|
158
|
+
* Calculates the difference in weeks between two dates.
|
|
159
|
+
* @param date1 - The first date in order.
|
|
160
|
+
* @param date2 - The second date in order.
|
|
161
|
+
*/
|
|
162
|
+
export function dateDiffWeeks(date1, date2) {
|
|
163
|
+
const date1Abs = date1.toDate(DEFAULT_TIME_ZONE).getTime();
|
|
164
|
+
const date2Abs = date2.toDate(DEFAULT_TIME_ZONE).getTime();
|
|
165
|
+
return Math.floor((date2Abs - date1Abs) / msPerWeek);
|
|
166
|
+
}
|
|
167
|
+
/* Formatting */
|
|
168
|
+
/* Day of the week*/
|
|
169
|
+
/**
|
|
170
|
+
* Formats a day of the week.
|
|
171
|
+
* @param day - The day of the week to format.
|
|
172
|
+
* @example "Monday" -> "Mon"
|
|
173
|
+
* @returns Formatted string of the day of the week.
|
|
174
|
+
*/
|
|
175
|
+
export function formatDayShort(day) {
|
|
176
|
+
// Use the first three letters of the day
|
|
177
|
+
return day.slice(0, 3);
|
|
178
|
+
}
|
|
179
|
+
/* Calendar Dates */
|
|
180
|
+
const FullDateFormatter = new DateFormatter(DEFAULT_LOCALE, {
|
|
181
|
+
day: 'numeric',
|
|
182
|
+
month: 'short',
|
|
183
|
+
year: 'numeric'
|
|
184
|
+
});
|
|
185
|
+
const ShortDateFormatter = new DateFormatter(DEFAULT_LOCALE, {
|
|
186
|
+
month: 'short',
|
|
187
|
+
day: 'numeric'
|
|
188
|
+
});
|
|
189
|
+
const MonthFormatter = new DateFormatter(DEFAULT_LOCALE, {
|
|
190
|
+
month: 'short',
|
|
191
|
+
year: '2-digit'
|
|
192
|
+
});
|
|
193
|
+
const NumFormatter = new DateFormatter(DEFAULT_LOCALE, {
|
|
194
|
+
day: '2-digit',
|
|
195
|
+
month: '2-digit',
|
|
196
|
+
year: 'numeric'
|
|
197
|
+
});
|
|
198
|
+
function formatDate(date, formatter) {
|
|
199
|
+
const nativeDate = date.toDate(getLocalTimeZone());
|
|
200
|
+
return formatter.format(nativeDate);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* @param date The CalendarDate object to format.
|
|
204
|
+
* @returns string of date in shortened format
|
|
205
|
+
* @example "Oct 5"
|
|
206
|
+
*/
|
|
207
|
+
export function formatDateShort(date) {
|
|
208
|
+
return formatDate(date, ShortDateFormatter);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* @param date The CalendarDate object to format.
|
|
212
|
+
* @returns The formatted date string.
|
|
213
|
+
* @example "5 Oct 2023"
|
|
214
|
+
*/
|
|
215
|
+
export function formatDateFull(date) {
|
|
216
|
+
return formatDate(date, FullDateFormatter);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* ISO format
|
|
220
|
+
* @param date The CalendarDate object to format.
|
|
221
|
+
* @returns The formatted date string in YYYY-MM-DD format.
|
|
222
|
+
* @example "2023-10-05"
|
|
223
|
+
*/
|
|
224
|
+
export function formatDateISO(date) {
|
|
225
|
+
return date.toString();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* @param date The CalendarDate object to format.
|
|
229
|
+
* @returns The formatted date string in dd/mm/yyyy format.
|
|
230
|
+
* @example "05/10/2023"
|
|
231
|
+
*/
|
|
232
|
+
export function formatDateNum(date) {
|
|
233
|
+
return formatDate(date, NumFormatter);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Formats the month only.
|
|
237
|
+
* @param date - The date to format.
|
|
238
|
+
* @returns The formatted month string.
|
|
239
|
+
* @example "Oct"
|
|
240
|
+
*/
|
|
241
|
+
export function formatMonth(date) {
|
|
242
|
+
return formatDate(date, MonthFormatter);
|
|
243
|
+
}
|
|
244
|
+
/* Times */
|
|
245
|
+
// Pad number with zeroes to the left
|
|
246
|
+
function padNum(num, len) {
|
|
247
|
+
if (isNaN(num)) {
|
|
248
|
+
return '0'.repeat(len);
|
|
249
|
+
}
|
|
250
|
+
return num.toString().padStart(len, '0');
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Gives time in HH:MM format
|
|
254
|
+
* @param time
|
|
255
|
+
* @returns string of time in that format
|
|
256
|
+
*/
|
|
257
|
+
export function formatTimeShort(time) {
|
|
258
|
+
const hours = padNum(time.hour, 2);
|
|
259
|
+
const minutes = padNum(time.minute, 2);
|
|
260
|
+
return `${hours}:${minutes}`;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Gives time in HH:MM:SS format
|
|
264
|
+
* @param time
|
|
265
|
+
* @returns string of time in that format
|
|
266
|
+
*/
|
|
267
|
+
export function formatTimeFull(time) {
|
|
268
|
+
const hours = padNum(time.hour, 2);
|
|
269
|
+
const minutes = padNum(time.minute, 2);
|
|
270
|
+
const seconds = padNum(time.second, 2);
|
|
271
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Calculates the end time given a starting time and duration.
|
|
275
|
+
* @param timeStart starting time
|
|
276
|
+
* @param durationMinutes duration in minutes
|
|
277
|
+
* @returns end time in HH:MM format
|
|
278
|
+
*/
|
|
279
|
+
export function formatTimeEnd(timeStart, durationMinutes) {
|
|
280
|
+
const timeEnd = timeStart.add({ minutes: durationMinutes });
|
|
281
|
+
return formatTimeShort(timeEnd);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Formats a full date and time.
|
|
285
|
+
* @param datetime The ZonedDateTime object to format.
|
|
286
|
+
* @returns The formatted date and time string.
|
|
287
|
+
* @example "05/10/2023 14:30:00"
|
|
288
|
+
*/
|
|
289
|
+
export function formatAbsolute(datetime) {
|
|
290
|
+
const date = toCalendarDate(datetime);
|
|
291
|
+
const time = toTime(datetime);
|
|
292
|
+
return `${formatDateNum(date)} ${formatTimeFull(time)}`;
|
|
293
|
+
}
|
|
294
|
+
/* State handling */
|
|
295
|
+
/**
|
|
296
|
+
* Unfreezes a CalendarDate object from a snapshot.
|
|
297
|
+
* @param raw - The snapshot of the CalendarDate object.
|
|
298
|
+
* @returns The unfrozen CalendarDate object.
|
|
299
|
+
*/
|
|
300
|
+
export function unfreezeDate(raw) {
|
|
301
|
+
return new CalendarDate(raw.year, raw.month, raw.day);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Unfreezes a Time object from a snapshot.
|
|
305
|
+
* @param raw - The snapshot of the Time object.
|
|
306
|
+
* @returns The unfrozen Time object.
|
|
307
|
+
*/
|
|
308
|
+
export function unfreezeTime(raw) {
|
|
309
|
+
return new Time(raw.hour, raw.minute, raw.second, raw.millisecond);
|
|
310
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './datetime/index.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// Reexport your entry utils here
|
|
1
|
+
export * from './datetime/index.js';
|
package/package.json
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.2.0",
|
|
7
|
+
"license": "MIT",
|
|
7
8
|
"files": [
|
|
8
9
|
"dist",
|
|
9
10
|
"!dist/**/*.test.*",
|
|
@@ -59,7 +60,6 @@
|
|
|
59
60
|
"prettier-plugin-svelte": "^3.3.3",
|
|
60
61
|
"prettier-plugin-tailwindcss": "^0.6.11",
|
|
61
62
|
"publint": "^0.3.2",
|
|
62
|
-
"ramda": "^0.31.3",
|
|
63
63
|
"svelte": "^5.0.0",
|
|
64
64
|
"svelte-check": "^4.0.0",
|
|
65
65
|
"sveltekit-superforms": "^2.27.1",
|
|
@@ -76,7 +76,8 @@
|
|
|
76
76
|
"svelte"
|
|
77
77
|
],
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"formsnap": "^2.0.1"
|
|
79
|
+
"formsnap": "^2.0.1",
|
|
80
|
+
"ramda": "^0.31.3"
|
|
80
81
|
},
|
|
81
82
|
"scripts": {
|
|
82
83
|
"dev": "vite dev",
|