File

src/app/pipes/payments-filter.pipe.ts

Metadata

name paymentsFilter

Methods

searchAmount
searchAmount(value: Payment, target: string)
Parameters :
Name Type Optional
value Payment no
target string no
Returns : boolean
searchDate
searchDate(date: string, target: string)
Parameters :
Name Type Optional
date string no
target string no
Returns : boolean
searchMemo
searchMemo(value: Payment, target: string)
Parameters :
Name Type Optional
value Payment no
target string no
Returns : boolean
searchName
searchName(value: Payment, target: string)
Parameters :
Name Type Optional
value Payment no
target string no
Returns : boolean
searchPayment
searchPayment(value: Payment, target: string)
Parameters :
Name Type Optional
value Payment no
target string no
Returns : boolean
searchStatus
searchStatus(value: Payment, target: string)
Parameters :
Name Type Optional
value Payment no
target string no
Returns : boolean
transform
transform(input: Payment[], targetRaw: string)
Parameters :
Name Type Optional
input Payment[] no
targetRaw string no
Returns : []
import { DatePipe } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { LOCALE } from '../app.constants';
import { Payment } from '../models';

@Pipe({
  name: 'paymentsFilter',
})
export class PaymentsFilterPipe implements PipeTransform {
  transform(input: Payment[], targetRaw: string): Payment[] {
    if (input === null || input === undefined || targetRaw === null || targetRaw === undefined) {
      return input;
    }
    const target = targetRaw.toLowerCase();
    return input.filter((value) => this.searchPayment(value, target));
  }

  searchPayment(value: Payment, target: string) {
    const datePipe = new DatePipe(LOCALE);
    const dateStr = value.paymentDateFormatted;
    const shortDate = dateStr ? datePipe.transform(new Date(dateStr), 'shortDate') : null;
    const mediumDate = dateStr ? datePipe.transform(new Date(dateStr), 'mediumDate').toLowerCase() : null;
    const longDate = dateStr ? datePipe.transform(new Date(dateStr), 'longDate').toLowerCase() : null;

    return (
      this.searchName(value, target) ||
      this.searchStatus(value, target) ||
      this.searchMemo(value, target) ||
      this.searchDate(shortDate, target) ||
      this.searchDate(mediumDate, target) ||
      this.searchDate(longDate, target) ||
      this.searchAmount(value, target)
    );
  }

  searchName(value: Payment, target: string): boolean {
    return (
      (value !== null &&
        value !== undefined &&
        value.payToName !== null &&
        value.payToName !== undefined &&
        value.payToName.toLowerCase().indexOf(target) > -1) ||
      (value !== null &&
        value !== undefined &&
        value.displayName !== null &&
        value.displayName !== undefined &&
        value.displayName.toLowerCase().indexOf(target) > -1)
    );
  }

  searchStatus(value: Payment, target: string): boolean {
    return (
      value !== null &&
      value !== undefined &&
      value.status !== null &&
      value.status !== undefined &&
      value.status.toLowerCase().indexOf(target) > -1
    );
  }

  searchMemo(value: Payment, target: string): boolean {
    return (
      value !== null &&
      value !== undefined &&
      value.memo !== null &&
      value.memo !== undefined &&
      value.memo.toLowerCase().indexOf(target) > -1
    );
  }

  searchDate(date: string, target: string): boolean {
    return date != null && date !== undefined && date.indexOf(target) > -1;
  }

  searchAmount(value: Payment, target: string): boolean {
    return (
      value !== null &&
      value !== undefined &&
      value.amountFormatted !== null &&
      value.amountFormatted !== undefined &&
      value.amountFormatted.indexOf(target) > -1
    );
  }
}

results matching ""

    No results matching ""