Skip to content

Commit

Permalink
feat: osm fix automatically extra spaces and h instead of :
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-gavanier committed Jan 16, 2025
1 parent 5e40bd0 commit a0ce004
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/transformer/fields/horaires/horaires.field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,21 @@ describe('horaires field', (): void => {
expect(openingHours).toBe('Tu,Th 14:00-18:30; We,Fr 10:00-18:30; Sa 10:00-18:00');
});

it('should fix extra spaces in osm hours', (): void => {
const openingHours: OsmOpeningHoursString = processHoraires(
{ OSM: 'Tu, Th, Fr 13:30-18:00; We, Sa 10:30-12:30, 13:30-18:00' },
matching
);

expect(openingHours).toBe('Tu,Th,Fr 13:30-18:00; We,Sa 10:30-12:30,13:30-18:00');
});

it('should fix h instead of : in osm hours', (): void => {
const openingHours: OsmOpeningHoursString = processHoraires({ OSM: 'Mo-Fr 14h00-19h00;Sa 14h00-18h00' }, matching);

expect(openingHours).toBe('Mo-Fr 14:00-19:00;Sa 14:00-18:00');
});

it('should replace unexpected charactere like + and newline by single comma', (): void => {
const openingHours: OsmOpeningHoursString = processHoraires(
{
Expand Down
8 changes: 5 additions & 3 deletions src/transformer/fields/horaires/horaires.field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const SEMAINE_PAIRE: string = 'week 2-52/2 ';
const OPENING_HOURS_REGEXP: RegExp = /^\d{2}:\d{2}-\d{2}:\d{2}(?:,\d{2}:\d{2}-\d{2}:\d{2})?$/;

const OSM_OPENING_HOURS_TRIVIAL_REGEXP: 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.*/;
/(?:(?: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?: string): string => osmHours?.replace(/,\s/g, ',').replace(/(\d)h(\d)/g, '$1:$2') ?? '';

const throwInvalidHours = (osmHours: string, day: OsmDaysOfWeek, hours: string): OsmOpeningHours => {
throw new InvalidHoursError(osmHours, hours, day);
Expand Down Expand Up @@ -59,13 +61,13 @@ const applyWeekOpeningIfAny = (osmHoraires?: string, weekOpening?: string): OsmO
const alreadyHaveOsmOpeningHours = (matching: LieuxMediationNumeriqueMatching, source: DataSource): boolean =>
matching.horaires?.osm != null &&
source[matching.horaires.osm] != null &&
OSM_OPENING_HOURS_TRIVIAL_REGEXP.test(source[matching.horaires.osm]?.toString() ?? '');
OSM_OPENING_HOURS_TRIVIAL_REGEXP.test(fixOsmHours(source[matching.horaires.osm]?.toString() ?? ''));

export const processHoraires = (source: DataSource, matching: LieuxMediationNumeriqueMatching): OsmOpeningHoursString => {
try {
if (alreadyHaveOsmOpeningHours(matching, source)) {
return applyWeekOpeningIfAny(
source[matching.horaires?.osm ?? '']?.toString(),
fixOsmHours(source[matching.horaires?.osm ?? '']?.toString()),
source[matching.semaine_ouverture?.colonne ?? '']?.toString()
);
}
Expand Down

0 comments on commit a0ce004

Please sign in to comment.