Skip to content

Commit 1af371a

Browse files
committed
docs: add jsdocs
1 parent 850851a commit 1af371a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Get the start and end time boundaries for a given date based on the provided start and end time strings.
3+
* @param start - The start time string in the format "HH:mm".
4+
* @param end - The end time string in the format "HH:mm".
5+
* @param date - The date for which the boundaries are calculated.
6+
* @returns An array containing the start and end time boundaries as Date objects.
7+
*/
18
function getBoundaries(start: string, end: string, date: Date) {
29
const [startHour, startMinute] = start.split(":").map(Number);
310
const [endHour, endMinute] = end.split(":").map(Number);
@@ -11,16 +18,29 @@ function getBoundaries(start: string, end: string, date: Date) {
1118
return [startTime, endTime];
1219
}
1320

21+
/**
22+
* Add a specified number of seconds to a given date.
23+
* @param date - The date to which the seconds are added.
24+
* @param seconds - The number of seconds to add.
25+
* @returns A new Date object with the added seconds.
26+
*/
1427
function addSeconds(date: Date, seconds: number): Date {
1528
return new Date(date.getTime() + seconds * 1000);
1629
}
1730

31+
/**
32+
* Represents a scheduler that manages working hours and holidays.
33+
*/
1834
export class Scheduler {
1935
private hours: {
2036
[day: number]: Array<{ start: string; end: string }> | null;
2137
};
2238
private holidays: Date[];
2339

40+
/**
41+
* Creates a new instance of the Scheduler.
42+
* @param config - The configuration object containing working hours and holidays.
43+
*/
2444
constructor(config: {
2545
hours: { [day: number]: Array<{ start: string; end: string }> | null };
2646
holidays: Date[];
@@ -29,6 +49,11 @@ export class Scheduler {
2949
this.holidays = config.holidays;
3050
}
3151

52+
/**
53+
* Checks if a given date is a working day.
54+
* @param date - The date to check.
55+
* @returns True if the date is a working day, false otherwise.
56+
*/
3257
public isWorkingDay(date: Date): boolean {
3358
if (this.isHoliday(date)) {
3459
return false;
@@ -37,6 +62,11 @@ export class Scheduler {
3762
return workingHours !== null && workingHours.length > 0;
3863
}
3964

65+
/**
66+
* Checks if a given date and time is within working hours.
67+
* @param date - The date and time to check.
68+
* @returns True if the date and time is within working hours, false otherwise.
69+
*/
4070
public isWorkingTime(date: Date): boolean {
4171
if (!this.isWorkingDay(date)) {
4272
return false;
@@ -56,6 +86,11 @@ export class Scheduler {
5686
return false;
5787
}
5888

89+
/**
90+
* Checks if a given date is a holiday.
91+
* @param date - The date to check.
92+
* @returns True if the date is a holiday, false otherwise.
93+
*/
5994
public isHoliday(date: Date): boolean {
6095
const dateOnly = date.toDateString();
6196
return this.holidays.some((holiday) => holiday.toDateString() === dateOnly);
@@ -80,6 +115,11 @@ export class Scheduler {
80115
return nextTime;
81116
}
82117

118+
/**
119+
* Get the next working day after a given date.
120+
* @param date - The date after which the next working day is calculated.
121+
* @returns The next working day as a Date object.
122+
*/
83123
public nextWorkingDay(date: Date): Date {
84124
let nextDay = new Date(date);
85125
nextDay.setDate(nextDay.getDate() + 1);
@@ -91,6 +131,11 @@ export class Scheduler {
91131
return nextDay;
92132
}
93133

134+
/**
135+
* Get the remaining working time for a given date.
136+
* @param date - The date for which the remaining working time is calculated.
137+
* @returns The remaining working time in seconds.
138+
*/
94139
public getRemainingWorkingTime(date: Date): number {
95140
const workingHours = this.getWorkingHours(date);
96141

@@ -107,6 +152,11 @@ export class Scheduler {
107152
return 0;
108153
}
109154

155+
/**
156+
* Get the elapsed working time for a given date.
157+
* @param date - The date for which the elapsed working time is calculated.
158+
* @returns The elapsed working time in seconds.
159+
*/
110160
public getElapsedWorkingTime(date: Date): number {
111161
const workingHours = this.getWorkingHours(date);
112162

@@ -123,6 +173,12 @@ export class Scheduler {
123173
return 0;
124174
}
125175

176+
/**
177+
* Add a specified number of seconds to a given date.
178+
* @param date - The date to which the seconds are added.
179+
* @param seconds - The number of seconds to add.
180+
* @returns A new Date object with the added seconds.
181+
*/
126182
public addTime(date: Date, seconds: number): Date {
127183
let newDate = new Date(date);
128184
let remainingSeconds = seconds;
@@ -142,6 +198,11 @@ export class Scheduler {
142198
return newDate;
143199
}
144200

201+
/**
202+
* Get the working hours for a given date.
203+
* @param date - The date for which the working hours are retrieved.
204+
* @returns The working hours for the given date.
205+
*/
145206
private getWorkingHours(date: Date) {
146207
const dayOfWeek = date.getDay();
147208
return this.hours[dayOfWeek];

0 commit comments

Comments
 (0)