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
@@ -0,0 +1,26 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { toISO, monthPrefix, getWeekday, formatDateDE, buildNameMap } from '../js/utils.js';
|
||||
|
||||
// toISO: zero-pads month and day
|
||||
assert.equal(toISO(2026, 4, 1), '2026-04-01', 'toISO pads single digits');
|
||||
assert.equal(toISO(2026, 12, 31), '2026-12-31', 'toISO keeps two-digit parts');
|
||||
|
||||
// monthPrefix: YYYY-MM
|
||||
assert.equal(monthPrefix(2026, 5), '2026-05', 'monthPrefix pads month');
|
||||
|
||||
// getWeekday: 1=Mon … 7=Sun (2026-04-01 is a Wednesday → 3; 2026-04-05 Sunday → 7)
|
||||
assert.equal(getWeekday('2026-04-01'), 3, 'Wednesday → 3');
|
||||
assert.equal(getWeekday('2026-04-05'), 7, 'Sunday → 7');
|
||||
assert.equal(getWeekday('2026-04-06'), 1, 'Monday → 1');
|
||||
|
||||
// formatDateDE: ISO → DD.MM.YYYY
|
||||
assert.equal(formatDateDE('2026-04-01'), '01.04.2026', 'formatDateDE DD.MM.YYYY');
|
||||
|
||||
// buildNameMap: id → name lookup
|
||||
assert.deepEqual(
|
||||
buildNameMap([{ id: 'a', name: 'Alice' }, { id: 'b', name: 'Bob' }]),
|
||||
{ a: 'Alice', b: 'Bob' },
|
||||
'buildNameMap maps id to name'
|
||||
);
|
||||
|
||||
console.log('All utils tests passed.');
|
||||
Reference in new issue
Block a user