Files
accounted/lib/reports/period-dates.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

79 lines
1.9 KiB
TypeScript

/**
* Period date helpers shared between report generators (momsdeklaration,
* periodisk sammanställning, etc.). Kept tiny on purpose: anything domain-
* specific belongs in the calling module.
*/
export type PeriodType = 'monthly' | 'quarterly' | 'yearly'
/**
* Calculate inclusive start and end dates for a fiscal-calendar period.
*
* monthly: period 1-12, one calendar month
* quarterly: period 1-4, three calendar months
* yearly: period 1, full calendar year
*/
export function calculatePeriodDates(
periodType: PeriodType,
year: number,
period: number,
): { start: string; end: string } {
let startMonth: number
let endMonth: number
switch (periodType) {
case 'monthly':
startMonth = period
endMonth = period
break
case 'quarterly':
startMonth = (period - 1) * 3 + 1
endMonth = period * 3
break
case 'yearly':
startMonth = 1
endMonth = 12
break
default:
startMonth = 1
endMonth = 12
}
const startDate = new Date(year, startMonth - 1, 1)
const endDate = new Date(year, endMonth, 0)
return {
start: formatDate(startDate),
end: formatDate(endDate),
}
}
export function formatDate(date: Date): string {
const y = date.getFullYear()
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
return `${y}-${m}-${d}`
}
const SWEDISH_MONTHS = [
'Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni',
'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December',
] as const
export function formatPeriodLabel(
periodType: PeriodType,
year: number,
period: number,
): string {
switch (periodType) {
case 'monthly':
return `${SWEDISH_MONTHS[period - 1]} ${year}`
case 'quarterly':
return `Kvartal ${period} ${year}`
case 'yearly':
return `Helår ${year}`
default:
return `${year}`
}
}