-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
83 lines (62 loc) · 2.49 KB
/
helper.js
File metadata and controls
83 lines (62 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
let fs
try {
fs = await import('fs')
} catch (error) {
console.warn("Could import module 'fs'. This error can occur when using this module inside the browser or 'fs' is not installed. If you are working inside node try run 'npm install fs'")
}
export function generateCookieString(jsonObject) {
const entries = Object.entries(jsonObject)
return entries.map(([key, val]) => `${key}=${val}`).join("; ")
}
export function loadJsonFile(path) {
const file = fs.readFileSync(path, 'utf8')
return JSON.parse(file)
}
export function getNextDayDate(day) {
const today = new Date();
let targetDay;
// Allow both numbers (0–6) and names ("monday")
if (typeof day === "number") {
targetDay = day;
} else if (typeof day === "string") {
const days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
targetDay = days.indexOf(day.toLowerCase());
if (targetDay === -1) {
throw new Error("Invalid day name");
}
} else {
throw new Error("Day must be a number (0–6) or weekday name");
}
const currentDay = today.getDay();
// Calculate days until next occurrence (never today → always future)
const daysUntil = (targetDay - currentDay + 7) % 7 || 7;
const nextDate = new Date(today);
nextDate.setDate(today.getDate() + daysUntil);
// Format YYYY-MM-DD
const year = nextDate.getFullYear();
const month = String(nextDate.getMonth() + 1).padStart(2, "0");
const dayNum = String(nextDate.getDate()).padStart(2, "0");
return `${year}-${month}-${dayNum}`;
}
export function offsetWeekday(weekday, offset) {
const days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
// Normalize input to lowercase
const index = days.indexOf(weekday.toLowerCase());
if (index === -1) {
throw new Error("Invalid weekday: " + weekday);
}
// Calculate new index using modulo that works with negatives
const newIndex = (index + offset % 7 + 7) % 7;
return days[newIndex];
}
export function offsetWeek(dateString, offset) {
// Convert to Date object
const date = new Date(dateString);
// Add offset * 7 days
date.setDate(date.getDate() + offset * 7);
// Convert back to YYYY-MM-DD
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}