fix: add openingHours config and catch its errors

This commit is contained in:
Rainer Killinger
2021-09-06 18:00:06 +02:00
parent e9452d6520
commit 6125d43e8c
3 changed files with 701 additions and 734 deletions

View File

@@ -20,7 +20,7 @@ import {Subscription} from 'rxjs';
import {logger} from '../_helpers/ts-logger';
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const openingHoursFn = require('opening_hours');
const ohFunction = require('opening_hours');
@Injectable()
@Pipe({
@@ -96,7 +96,7 @@ export class StringSplitPipe implements PipeTransform {
@Injectable()
@Pipe({
name: 'openingHours',
pure: false,
pure: true,
})
export class OpeningHoursPipe implements PipeTransform, OnDestroy {
locale: string;
@@ -140,10 +140,20 @@ export class OpeningHoursPipe implements PipeTransform, OnDestroy {
return;
}
const openingHours = new openingHoursFn(aString);
let openingHours;
if ((openingHours.getWarnings() as string[]).length > 0) {
logger.warn((openingHours.getWarnings() as string[]).join('. '));
try {
openingHours = new ohFunction(aString, {
address: {
country_code: 'de',
state: 'Hessen',
},
lon: 8.667_97,
lat: 50.129_16,
});
} catch (error) {
logger.warn(error);
this.value = '';
return;
}