refactor: split app into modular src architecture
This commit is contained in:
25
src/utils/date.js
Normal file
25
src/utils/date.js
Normal file
@@ -0,0 +1,25 @@
|
||||
export function makeId(prefix) {
|
||||
return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
export function todayYMD() {
|
||||
const now = new Date();
|
||||
return `${now.getFullYear()}-${`${now.getMonth() + 1}`.padStart(2, '0')}-${`${now.getDate()}`.padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
export function parseYMD(value) {
|
||||
if (!value || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return null;
|
||||
const d = new Date(`${value}T00:00:00`);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
export function findActiveTripId(trips) {
|
||||
const today = parseYMD(todayYMD());
|
||||
if (!today) return null;
|
||||
const active = trips.find((trip) => {
|
||||
const start = parseYMD(trip.startDate);
|
||||
const end = parseYMD(trip.endDate);
|
||||
return !!start && !!end && today >= start && today <= end;
|
||||
});
|
||||
return active?.id || null;
|
||||
}
|
||||
Reference in New Issue
Block a user