Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: osm fix automatically extra spaces and h instead of : #243

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading