refactor: apply new eslint rules

This commit is contained in:
Rainer Killinger
2021-08-03 17:34:38 +02:00
parent bb8a9f6ba5
commit 7afa66e01a
15 changed files with 229 additions and 113 deletions

View File

@@ -19,11 +19,9 @@ import moment from 'moment';
import {Subscription} from 'rxjs';
import {logger} from '../_helpers/ts-logger';
// tslint:disable-next-line: no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const openingHoursFn = require('opening_hours');
// tslint:disable: completed-docs
@Injectable()
@Pipe({
name: 'join',
@@ -58,13 +56,13 @@ export class SentenceCasePipe implements PipeTransform {
value = '';
transform(aString: string | unknown): string {
if (typeof aString !== 'string'){
throw new SyntaxError(`Wrong parameter in StringSplitPipe. Expected a valid String, received: ${aString}`);
if (typeof aString !== 'string') {
throw new SyntaxError(
`Wrong parameter in StringSplitPipe. Expected a valid String, received: ${aString}`,
);
}
this.value = aString.substr(0,1)
.toUpperCase() + aString.substr(1);
this.value = aString.slice(0, 1).toUpperCase() + aString.slice(1);
return this.value;
}
@@ -102,7 +100,9 @@ export class StringSplitPipe implements PipeTransform {
})
export class OpeningHoursPipe implements PipeTransform {
locale: string;
onLangChange?: Subscription;
value = '';
constructor(private readonly translate: TranslateService) {
@@ -115,29 +115,30 @@ export class OpeningHoursPipe implements PipeTransform {
}
}
transform(aString: string | unknown): string {
this.updateValue(aString);
this._dispose();
if (this.onLangChange?.closed === true) {
this.onLangChange = this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(aString);
});
this.onLangChange = this.translate.onLangChange.subscribe(
(event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(aString);
},
);
}
return this.value;
}
updateValue(aString: string | unknown) {
if (typeof aString !== 'string'){
if (typeof aString !== 'string') {
logger.warn(`openingHours pipe unable to parse input: ${aString}`);
return;
}
const openingHours = new openingHoursFn(aString);
if ((openingHours.getWarnings() as string[]).length > 0){
if ((openingHours.getWarnings() as string[]).length > 0) {
logger.warn((openingHours.getWarnings() as string[]).join('. '));
return;
@@ -146,27 +147,24 @@ export class OpeningHoursPipe implements PipeTransform {
const isOpen: boolean = openingHours.getState();
const nextChange: Date = openingHours.getNextChange();
let prefixKey = isOpen ?
'common.openingHours.open_until' :
'common.openingHours.closed_until';
let prefixKey = isOpen
? 'common.openingHours.open_until'
: 'common.openingHours.closed_until';
let formattedCalender = moment(nextChange)
.calendar();
let formattedCalender = moment(nextChange).calendar();
if (moment(nextChange)
.isBefore(moment()
.add(1, 'hours'))) {
prefixKey= isOpen ?
'common.openingHours.closing_soon' :
'common.openingHours.opening_soon';
formattedCalender = formattedCalender.substr(0,1)
.toUpperCase() + formattedCalender.substr(1);
if (moment(nextChange).isBefore(moment().add(1, 'hours'))) {
prefixKey = isOpen
? 'common.openingHours.closing_soon'
: 'common.openingHours.opening_soon';
formattedCalender =
formattedCalender.slice(0, 1).toUpperCase() +
formattedCalender.slice(1);
}
this.value = `${this.translate.instant(prefixKey)} ${formattedCalender}`;
}
}
@Injectable()
@Pipe({
name: 'numberLocalized',
@@ -174,7 +172,9 @@ export class OpeningHoursPipe implements PipeTransform {
})
export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
locale: string;
onLangChange?: Subscription;
value: string;
constructor(private readonly translate: TranslateService) {
@@ -200,10 +200,12 @@ export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
this.updateValue(value, formatOptions);
this._dispose();
if (this.onLangChange?.closed === true) {
this.onLangChange = this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(value, formatOptions);
});
this.onLangChange = this.translate.onLangChange.subscribe(
(event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(value, formatOptions);
},
);
}
return this.value;
@@ -215,10 +217,19 @@ export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
return;
}
const options = formatOptions?.split(',')
.map((element) => element.split(':'))
.reduce((acc, [key, val]) => ({...acc, [key.trim()]: val.trim()}),{}) as Intl.NumberFormatOptions;
const float = typeof value === 'string' ? Number.parseFloat(value) : value as number;
const options = formatOptions
?.split(',')
.map(element => element.split(':'))
// eslint-disable-next-line unicorn/no-array-reduce
.reduce(
(accumulator, [key, value_]) => ({
...accumulator,
[key.trim()]: value_.trim(),
}),
{},
) as Intl.NumberFormatOptions;
const float =
typeof value === 'string' ? Number.parseFloat(value) : (value as number);
this.value = new Intl.NumberFormat(this.locale, options).format(float);
}
}
@@ -230,7 +241,9 @@ export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
})
export class DateLocalizedFormatPipe implements PipeTransform, OnDestroy {
locale: string;
onLangChange?: Subscription;
value: string;
constructor(private readonly translate: TranslateService) {
@@ -257,32 +270,48 @@ export class DateLocalizedFormatPipe implements PipeTransform, OnDestroy {
this.updateValue(value, formatOptions);
this._dispose();
if (this.onLangChange?.closed === true) {
this.onLangChange = this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(value, formatOptions);
});
this.onLangChange = this.translate.onLangChange.subscribe(
(event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(value, formatOptions);
},
);
}
return this.value;
}
updateValue(value: string | Date | unknown, formatOptions?: string): void {
if (typeof value !== 'string' && Object.prototype.toString.call(value) !== '[object Date]') {
if (
typeof value !== 'string' &&
Object.prototype.toString.call(value) !== '[object Date]'
) {
logger.warn(`dateFormat pipe unable to parse input: ${value}`);
return;
}
const options = formatOptions?.split(',')
.map((element) => element.split(':'))
.reduce((acc, [key, val]) => ({...acc, [key.trim()]: val.trim()}),{}) as Intl.DateTimeFormatOptions;
const date = typeof value === 'string' ? Date.parse(value) : value as Date;
this.value = new Intl.DateTimeFormat(this.locale, options ?? {
day: 'numeric',
month: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
})
.format(date);
const options = formatOptions
?.split(',')
.map(element => element.split(':'))
// eslint-disable-next-line unicorn/no-array-reduce
.reduce(
(accumulator, [key, value_]) => ({
...accumulator,
[key.trim()]: value_.trim(),
}),
{},
) as Intl.DateTimeFormatOptions;
const date =
typeof value === 'string' ? Date.parse(value) : (value as Date);
this.value = new Intl.DateTimeFormat(
this.locale,
options ?? {
day: 'numeric',
month: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
},
).format(date);
}
}

View File

@@ -14,9 +14,22 @@
*/
import {ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {ArrayJoinPipe, DateLocalizedFormatPipe, NumberLocalizedPipe, SentenceCasePipe, StringSplitPipe, OpeningHoursPipe} from './common-string-pipes';
import {ThingTranslateDefaultParser, ThingTranslateParser} from './thing-translate.parser';
import {ThingPropertyNameTranslatePipe, ThingTranslatePipe} from './thing-translate.pipe';
import {
ArrayJoinPipe,
DateLocalizedFormatPipe,
NumberLocalizedPipe,
SentenceCasePipe,
StringSplitPipe,
OpeningHoursPipe,
} from './common-string-pipes';
import {
ThingTranslateDefaultParser,
ThingTranslateParser,
} from './thing-translate.parser';
import {
ThingPropertyNameTranslatePipe,
ThingTranslatePipe,
} from './thing-translate.pipe';
import {ThingTranslateService} from './thing-translate.service';
export interface ThingTranslateModuleConfig {

View File

@@ -19,8 +19,6 @@ import {isThing, SCThings, SCThingType} from '@openstapps/core';
import {Subscription} from 'rxjs';
import {ThingTranslateService} from './thing-translate.service';
// tslint:disable: member-ordering prefer-function-over-method completed-docs
@Injectable()
@Pipe({
name: 'thingTranslate',