77 lines
2.8 KiB
JavaScript
77 lines
2.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.resolveAnalyticsDateRange = resolveAnalyticsDateRange;
|
|
exports.parseAnalyticsGameKey = parseAnalyticsGameKey;
|
|
function startOfDay(d) {
|
|
const x = new Date(d);
|
|
x.setUTCHours(0, 0, 0, 0);
|
|
return x;
|
|
}
|
|
function endOfDay(d) {
|
|
const x = new Date(d);
|
|
x.setUTCHours(23, 59, 59, 999);
|
|
return x;
|
|
}
|
|
function startOfWeekMonday(d) {
|
|
const x = startOfDay(d);
|
|
const day = x.getUTCDay();
|
|
const diff = day === 0 ? 6 : day - 1;
|
|
x.setUTCDate(x.getUTCDate() - diff);
|
|
return x;
|
|
}
|
|
/** Resolves preset or custom ISO bounds into inclusive UTC range. */
|
|
function resolveAnalyticsDateRange(presetInput, fromInput, toInput) {
|
|
const now = new Date();
|
|
const preset = (presetInput ?? 'today');
|
|
if (preset === 'custom') {
|
|
if (!fromInput || !toInput) {
|
|
throw new Error('Custom range requires from and to (ISO date)');
|
|
}
|
|
const from = startOfDay(new Date(fromInput));
|
|
const to = endOfDay(new Date(toInput));
|
|
if (Number.isNaN(from.getTime()) || Number.isNaN(to.getTime())) {
|
|
throw new Error('Invalid custom date range');
|
|
}
|
|
if (from > to)
|
|
throw new Error('from must be before to');
|
|
return { preset, from, to };
|
|
}
|
|
switch (preset) {
|
|
case 'yesterday': {
|
|
const y = new Date(now);
|
|
y.setUTCDate(y.getUTCDate() - 1);
|
|
return { preset, from: startOfDay(y), to: endOfDay(y) };
|
|
}
|
|
case 'this_week':
|
|
return { preset, from: startOfWeekMonday(now), to: endOfDay(now) };
|
|
case 'this_month': {
|
|
const from = startOfDay(new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)));
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'last_30_days': {
|
|
const from = startOfDay(new Date(now));
|
|
from.setUTCDate(from.getUTCDate() - 29);
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'last_90_days': {
|
|
const from = startOfDay(new Date(now));
|
|
from.setUTCDate(from.getUTCDate() - 89);
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'this_year': {
|
|
const from = startOfDay(new Date(Date.UTC(now.getUTCFullYear(), 0, 1)));
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'today':
|
|
default:
|
|
return { preset: 'today', from: startOfDay(now), to: endOfDay(now) };
|
|
}
|
|
}
|
|
function parseAnalyticsGameKey(value) {
|
|
if (!value || value === 'all')
|
|
return 'all';
|
|
if (value === 'candle-predict')
|
|
return 'candle-predict';
|
|
throw new Error('gameKey must be all or candle-predict');
|
|
}
|
|
//# sourceMappingURL=analyticsDateRange.js.map
|