refactor: centralize shared date/name helpers in utils.js (#12)
New js/utils.js exports toISO, monthPrefix, getWeekday, formatDateDE and buildNameMap. algorithm.js, calendar.js, excel.js and history.js now import these instead of re-implementing ISO construction, weekday math, DD.MM.YYYY formatting and the id→name map inline. Adds tests/utils.test.mjs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
0efb9cb853
commit
fd9870871b
7 files changed
+71
-26
No files matched your search
+28
@@ -0,0 +1,28 @@
|
||||
// Shared pure helpers used across modules.
|
||||
|
||||
// "YYYY-MM-DD" from numeric parts.
|
||||
export function toISO(year, month, day) {
|
||||
return year + '-' + String(month).padStart(2, '0') + '-' + String(day).padStart(2, '0');
|
||||
}
|
||||
|
||||
// "YYYY-MM" month prefix (used to filter history by month).
|
||||
export function monthPrefix(year, month) {
|
||||
return year + '-' + String(month).padStart(2, '0');
|
||||
}
|
||||
|
||||
// Weekday for an ISO date string: 1=Mo, 2=Di, 3=Mi, 4=Do, 5=Fr, 6=Sa, 7=So.
|
||||
export function getWeekday(isoDate) {
|
||||
const d = new Date(isoDate + 'T12:00:00Z').getUTCDay(); // 0=Sun
|
||||
return d === 0 ? 7 : d;
|
||||
}
|
||||
|
||||
// "DD.MM.YYYY" from an ISO date string.
|
||||
export function formatDateDE(isoDate) {
|
||||
const [y, m, d] = isoDate.split('-');
|
||||
return d + '.' + m + '.' + y;
|
||||
}
|
||||
|
||||
// { [id]: name } lookup map from an employees array.
|
||||
export function buildNameMap(employees) {
|
||||
return Object.fromEntries(employees.map(e => [e.id, e.name]));
|
||||
}
|
||||
Reference in new issue
Block a user