All files / src/app/shared/pipes/customtext customtext.pipe.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 6/6
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                1x 45x     54x   54x 57x   57x     13x       57x          
import { Pipe, PipeTransform } from '@angular/core';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { CustomtextService } from '../../services/customtext/customtext.service';
 
@Pipe({
  name: 'customtext'
})
export class CustomtextPipe implements PipeTransform {
  constructor(private cts: CustomtextService) {}
 
  transform(defaultValue: string, key: string, ...replacements: string[]): Observable<string> {
    return of('...')
      .pipe(
        switchMap(() => this.cts.getCustomText$(key)),
        map(customText => (!customText ? (defaultValue || key) : customText)),
        map(customText => {
          replacements
            .forEach(replacement => {
              // eslint-disable-next-line no-param-reassign
              customText = customText
                .replace('%s', replacement)
                .replace('%date', new Date(replacement).toLocaleString());
            });
          return customText;
        })
      );
  }
}