File

projects/angular-calendar/src/modules/common/calendar-date/calendar-date.pipe.ts

Description

This pipe is primarily for rendering the current view title. Example usage:

// where `viewDate` is a `Date` and view is `'month' | 'week' | 'day'`
{{ viewDate | calendarDate:(view + 'ViewTitle'):'en' }}

Metadata

Methods

transform
transform(date: Date, method: string, locale: string, weekStartsOn: number, excludeDays: number[], daysInWeek?: number)
Parameters :
Name Type Optional Default value
date Date No
method string No
locale string No this.locale
weekStartsOn number No 0
excludeDays number[] No []
daysInWeek number Yes
Returns : string
import { Pipe, PipeTransform, LOCALE_ID, Inject } from '@angular/core';
import { CalendarDateFormatter } from '../calendar-date-formatter/calendar-date-formatter.provider';

/**
 * This pipe is primarily for rendering the current view title. Example usage:
 * ```typescript
 * // where `viewDate` is a `Date` and view is `'month' | 'week' | 'day'`
 * {{ viewDate | calendarDate:(view + 'ViewTitle'):'en' }}
 * ```
 */
@Pipe({
  name: 'calendarDate',
})
export class CalendarDatePipe implements PipeTransform {
  constructor(
    private dateFormatter: CalendarDateFormatter,
    @Inject(LOCALE_ID) private locale: string
  ) {}

  transform(
    date: Date,
    method: string,
    locale: string = this.locale,
    weekStartsOn: number = 0,
    excludeDays: number[] = [],
    daysInWeek?: number
  ): string {
    if (typeof this.dateFormatter[method] === 'undefined') {
      const allowedMethods = Object.getOwnPropertyNames(
        Object.getPrototypeOf(CalendarDateFormatter.prototype)
      ).filter((iMethod) => iMethod !== 'constructor');
      throw new Error(
        `${method} is not a valid date formatter. Can only be one of ${allowedMethods.join(
          ', '
        )}`
      );
    }
    return this.dateFormatter[method]({
      date,
      locale,
      weekStartsOn,
      excludeDays,
      daysInWeek,
    });
  }
}

results matching ""

    No results matching ""