Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
141 lines (116 loc) · 3.83 KB
/
Copy pathutils.js
File metadata and controls
141 lines (116 loc) · 3.83 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* global chrome */
/**
* Shared utilities for Memento Mori extension
* Load this file before other scripts that need these utilities
*/
// Simple logger with levels
const Logger = {
level: 'info', // 'debug', 'info', 'warn', 'error'
isProduction: true,
setLevel(level) {
this.level = level;
},
shouldLog(level) {
const levels = { debug: 0, info: 1, warn: 2, error: 3 };
return levels[level] >= levels[this.level];
},
debug(message, ...args) {
if (this.shouldLog('debug')) {
console.log(`[DEBUG] ${message}`, ...args);
}
},
info(message, ...args) {
if (this.shouldLog('info')) {
console.log(`[INFO] ${message}`, ...args);
}
},
warn(message, ...args) {
if (this.shouldLog('warn')) {
console.warn(`[WARN] ${message}`, ...args);
}
},
error(message, ...args) {
if (this.shouldLog('error')) {
console.error(`[ERROR] ${message}`, ...args);
}
}
};
// Helper functions
const Helpers = {
calculateAge(birthdate) {
if (!birthdate) return 0;
const today = new Date();
const birth = new Date(birthdate);
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
},
calculateDaysLeft(age, continent, birthdate) {
const lifeExpectancy = {
'North America': 79,
'Europe': 78,
'Asia': 73,
'South America': 76,
'Africa': 64,
'Oceania': 81,
'Antarctica': 79
};
const expectedLifespan = lifeExpectancy[continent] || 79;
// If we have a birthdate, calculate precise days
if (birthdate) {
const birth = new Date(birthdate);
const today = new Date();
// Calculate expected death date
const deathDate = new Date(birth);
deathDate.setFullYear(birth.getFullYear() + expectedLifespan);
// Calculate difference in milliseconds
const diffTime = deathDate - today;
if (diffTime < 0) {
return -1; // Borrowed time
}
// Convert to days
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
// Fallback to age-based calculation if no birthdate (less precise)
if (age === 0) return 0;
if (age >= expectedLifespan) {
return -1; // Borrowed time
}
const yearsLeft = Math.max(0, expectedLifespan - age);
return Math.floor(yearsLeft * 365.25);
},
parseTime(timeString) {
if (!timeString) return 0;
const [hours, minutes] = timeString.split(':').map(Number);
return (hours || 0) * 60 + (minutes || 0);
},
formatTime(minutes) {
if (minutes < 60) {
return `${minutes}m`;
} else if (minutes < 1440) {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`;
} else {
const days = Math.floor(minutes / 1440);
const hours = Math.floor((minutes % 1440) / 60);
return hours > 0 ? `${days}d ${hours}h` : `${days}d`;
}
},
cleanHostname(hostname) {
if (!hostname) return '';
return hostname.toLowerCase().replace(/^www\./, '');
},
isValidPIN(pin) {
return /^\d{4}$/.test(pin);
},
daysUntilDeadline(deadline) {
if (!deadline) return null;
const deadlineDate = new Date(deadline);
const now = new Date();
return Math.ceil((deadlineDate - now) / (1000 * 60 * 60 * 24));
}
};