UNPKG

@gouvfr-anct/timetable-to-osm-opening-hours

Version:

📚 Bibliothèque pour la transformation d'une série de plages horaires représentant un format emploi du temps vers le standard OSM pour les horaires d'ouverture.

63 lines (62 loc) • 3.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTimetableOsmOpeningHours = exports.lastDayOfTheWeek = exports.firstDayOfTheWeek = exports.dateTimeFor = exports.dayOfTheWeek = void 0; /* eslint-disable-next-line camelcase */ const opening_hours_1 = __importDefault(require("opening_hours")); const utilities_1 = require("../utilities"); const MINUTE_TO_MILLISECONDS = 60 * 1000; const HOUR_TO_MILLISECONDS = 60 * MINUTE_TO_MILLISECONDS; const DAY_TO_MILLISECONDS = 24 * HOUR_TO_MILLISECONDS; const dayOfTheWeek = (date, weekDay) => /* eslint-disable-next-line no-mixed-operators */ new Date(date.getTime() + (weekDay + 1 - date.getDay()) * DAY_TO_MILLISECONDS); exports.dayOfTheWeek = dayOfTheWeek; const timeToMilliseconds = (time) => { const [hours, minutes] = time.split(':').map(Number); /* eslint-disable-next-line no-mixed-operators */ return (hours ?? 0) * HOUR_TO_MILLISECONDS + (minutes ?? 0) * MINUTE_TO_MILLISECONDS; }; const firstTimeOfTheDay = (date) => { const dateWithoutTime = new Date(date); dateWithoutTime.setHours(0, 0, 0, 0); return dateWithoutTime; }; const lastTimeOfTheDay = (date) => { const dateWithoutTime = new Date(date); dateWithoutTime.setHours(23, 59, 59, 999); return dateWithoutTime; }; const dateTimeFor = (date) => (weekDay, time) => new Date((0, exports.dayOfTheWeek)(firstTimeOfTheDay(date), weekDay).getTime() + timeToMilliseconds(time)); exports.dateTimeFor = dateTimeFor; const formatTime = (intervalStart) => intervalStart?.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }); const appendOsmOpeningHours = (day, [intervalStart, intervalEnd]) => ({ day, osmHours: `${formatTime(intervalStart)}-${formatTime(intervalEnd)}` }); const matchingDay = (day) => (openingHours) => openingHours.day === day; const toUpdatedOsmOpeningHours = (day, [intervalStart, intervalEnd]) => (openingHours) => openingHours.day === day ? { ...openingHours, osmHours: `${openingHours.osmHours},${formatTime(intervalStart)}-${formatTime(intervalEnd)}` } : openingHours; const appendTimeTableInterval = (timeTableOpeningHours, osmInterval, day) => timeTableOpeningHours.find(matchingDay(day)) == null ? [...timeTableOpeningHours, appendOsmOpeningHours(day, osmInterval)] : timeTableOpeningHours.map(toUpdatedOsmOpeningHours(day, osmInterval)); const firstDayOfTheWeek = (date) => (0, exports.dayOfTheWeek)(date, utilities_1.WeekDay.Monday); exports.firstDayOfTheWeek = firstDayOfTheWeek; const lastDayOfTheWeek = (date) => (0, exports.dayOfTheWeek)(date, utilities_1.WeekDay.Sunday); exports.lastDayOfTheWeek = lastDayOfTheWeek; const dayOfWeek = ([intervalStart]) => utilities_1.OSM_DAYS_OF_WEEK[intervalStart.getDay() - 1] ?? 'Su'; const toTimetableOsmOpeningHours = (date) => (horairesOSM) => horairesOSM == null ? [] : new opening_hours_1.default(horairesOSM, null) .getOpenIntervals(firstTimeOfTheDay((0, exports.firstDayOfTheWeek)(date)), lastTimeOfTheDay((0, exports.lastDayOfTheWeek)(date))) .reduce((timeTableOpeningHours, osmInterval) => appendTimeTableInterval(timeTableOpeningHours, osmInterval, dayOfWeek(osmInterval)), []); exports.toTimetableOsmOpeningHours = toTimetableOsmOpeningHours;