refactor: replace ignored numeric literals with constants

This commit is contained in:
2026-09-28 16:37:01 +03:00
committed by ksv741
parent a970eaaf7a
commit b78c215ff1
60 changed files with 510 additions and 283 deletions
@@ -1,3 +1,8 @@
import { ISO_DATE_LENGTH, ISO_DATE_START } from '../../common/time';
const UTC_MONTH_INDEX_OFFSET = 1;
const NEXT_DAY_OFFSET = 1;
const MOSCOW_UTC_OFFSET_HOURS = 3;
export type DateOnlyRange = { from: string; to: string };
const moscow = 'Europe/Moscow';
@@ -21,11 +26,11 @@ export function dateOnlyBounds(
const from = dateParts(range.from);
const end = dateParts(range.to);
const to = new Date(
Date.UTC(end[0], end[1] - 1, end[2] + 1, -MOSCOW_UTC_OFFSET_HOURS),
Date.UTC(end[0], end[1] - UTC_MONTH_INDEX_OFFSET, end[2] + NEXT_DAY_OFFSET, -MOSCOW_UTC_OFFSET_HOURS),
);
return {
from: new Date(
Date.UTC(from[0], from[1] - 1, from[2], -MOSCOW_UTC_OFFSET_HOURS),
Date.UTC(from[0], from[1] - UTC_MONTH_INDEX_OFFSET, from[2], -MOSCOW_UTC_OFFSET_HOURS),
),
to: to > now ? now : to,
};
@@ -39,7 +44,7 @@ export function dateParts(value: string): [number, number, number] {
number,
number,
];
if (utcDate(value).toISOString().slice(0, 10) !== value) {
if (utcDate(value).toISOString().slice(ISO_DATE_START, ISO_DATE_LENGTH) !== value) {
throw new Error('Invalid date');
}
return parts;
@@ -49,6 +54,6 @@ export function utcDate(value: string): Date {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (!match) return new Date(NaN);
return new Date(
Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3])),
Date.UTC(Number(match[1]), Number(match[2]) - UTC_MONTH_INDEX_OFFSET, Number(match[3])),
);
}