UNPKG

@gouvfr-anct/mednum

Version:

✨ Permet de transformer une source de données vers le schéma des lieux de mediation numerique

51 lines (50 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processHoraires = void 0; const timetable_to_osm_opening_hours_1 = require("@gouvfr-anct/timetable-to-osm-opening-hours"); const to_osm_hours_1 = require("../../to-osm-hours/to-osm-hours"); const invalid_hours_error_1 = require("./errors/invalid-hours-error"); const process_horaires_field_1 = require("./process-horaires.field"); const opening_hours_from_week_1 = require("./opening-hours-from-week"); const SEMAINE_IMPAIRE = 'week 1-53/2 '; const SEMAINE_PAIRE = 'week 2-52/2 '; const OPENING_HOURS_REGEXP = /^\d{2}:\d{2}-\d{2}:\d{2}(?:,\d{2}:\d{2}-\d{2}:\d{2})?$/; const OSM_OPENING_HOURS_TRIVIAL_REGEXP = /(?:(?:Mo|Tu|We|Th|Fr|Sa|Su)(?:[-,](?:Mo|Tu|We|Th|Fr|Sa|Su))?\s)?(?:[0-1]\d|2[0-3]):[0-5]\d-(?:[0-1]\d|2[0-3]):[0-5]\d.*/; const fixOsmHours = (osmHours) => osmHours?.replace(/,\s/g, ',').replace(/(\d)h(\d)/g, '$1:$2') ?? ''; const throwInvalidHours = (osmHours, day, hours) => { throw new invalid_hours_error_1.InvalidHoursError(osmHours, hours, day); }; const checkOsmOpeningHoursFormat = (osmHours, day, hours) => OPENING_HOURS_REGEXP.test(osmHours) || osmHours === '' ? { day, osmHours } : throwInvalidHours(osmHours, day, hours); const toSingleOsmOpeningHours = (day, hours) => checkOsmOpeningHoursFormat((0, to_osm_hours_1.toOsmHours)(hours), day, hours); const wrapInArray = (osmOpeningHours) => osmOpeningHours.osmHours === '' ? [] : [osmOpeningHours]; const processDay = (day, hours) => hours == null || hours === '' ? [] : wrapInArray(toSingleOsmOpeningHours(day, hours)); const openingHoursFromDays = (matching, source) => (0, process_horaires_field_1.osmOpeningHoursString)((0, timetable_to_osm_opening_hours_1.fromTimetableOsmOpeningHours)([ ...(matching.horaires?.jours?.reduce((processedDay, currentValue) => [ ...processedDay, ...processDay(currentValue.osm, Array.isArray(currentValue.colonne) ? currentValue.colonne.map((col) => source[col]?.toString()).join(', ') : (source[currentValue.colonne]?.toString() ?? '')) ], []) ?? []) ])); const applyWeek = (osmHoraires, weekOpening) => weekOpening === 'Semaine paire' ? SEMAINE_PAIRE.concat(osmHoraires) : SEMAINE_IMPAIRE.concat(osmHoraires); const applyWeekOpeningIfAny = (osmHoraires, weekOpening) => weekOpening != null && osmHoraires != null ? applyWeek(osmHoraires, weekOpening) : osmHoraires; const alreadyHaveOsmOpeningHours = (matching, source) => matching.horaires?.osm != null && source[matching.horaires.osm] != null && OSM_OPENING_HOURS_TRIVIAL_REGEXP.test(fixOsmHours(source[matching.horaires.osm]?.toString() ?? '')); const processHoraires = (source, matching) => { try { if (alreadyHaveOsmOpeningHours(matching, source)) { return applyWeekOpeningIfAny(fixOsmHours(source[matching.horaires?.osm ?? '']?.toString()), source[matching.semaine_ouverture?.colonne ?? '']?.toString()); } const osmOpeningHours = openingHoursFromDays(matching, source); return osmOpeningHours === process_horaires_field_1.NO_OSM_OPENING_HOURS && matching.horaires?.semaine != null ? (0, opening_hours_from_week_1.openingHoursFromWeek)(source[matching.horaires.semaine]?.toString()) : osmOpeningHours; } catch (error) { if (error instanceof invalid_hours_error_1.InvalidHoursError) return process_horaires_field_1.NO_OSM_OPENING_HOURS; throw error; } }; exports.processHoraires = processHoraires;