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

1413
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -89,7 +89,7 @@
"ngx-logger": "4.1.9", "ngx-logger": "4.1.9",
"ngx-markdown": "9.1.1", "ngx-markdown": "9.1.1",
"ngx-moment": "5.0.0", "ngx-moment": "5.0.0",
"opening_hours": "3.6.0", "opening_hours": "3.7.0",
"rxjs": "6.6.3", "rxjs": "6.6.3",
"tslib": "1.14.1", "tslib": "1.14.1",
"zone.js": "0.11.4" "zone.js": "0.11.4"

View File

@@ -20,7 +20,7 @@ import {Subscription} from 'rxjs';
import {logger} from '../_helpers/ts-logger'; import {logger} from '../_helpers/ts-logger';
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module // eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const openingHoursFn = require('opening_hours'); const ohFunction = require('opening_hours');
@Injectable() @Injectable()
@Pipe({ @Pipe({
@@ -96,7 +96,7 @@ export class StringSplitPipe implements PipeTransform {
@Injectable() @Injectable()
@Pipe({ @Pipe({
name: 'openingHours', name: 'openingHours',
pure: false, pure: true,
}) })
export class OpeningHoursPipe implements PipeTransform, OnDestroy { export class OpeningHoursPipe implements PipeTransform, OnDestroy {
locale: string; locale: string;
@@ -140,10 +140,20 @@ export class OpeningHoursPipe implements PipeTransform, OnDestroy {
return; return;
} }
const openingHours = new openingHoursFn(aString); let openingHours;
if ((openingHours.getWarnings() as string[]).length > 0) { try {
logger.warn((openingHours.getWarnings() as string[]).join('. ')); 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; return;
} }