export const pipeTransform = {
  date (val: Date | undefined, format = 'dd/MM/yyyy'): string {
    if (!val) return '';
    const day = (val.getDate()).toString().padStart(2, '0');
    const month = (val.getMonth()+1).toString().padStart(2, '0');
    const year = val.getFullYear().toString().padStart(2, '0');
    return format.replace('dd', day).replace('MM', month).replace('yyyy', year) ;
  },

  dateTime (val: Date, format = 'dd/MM/yyyy hh:mm'): string {
    const dateTime = format.split(' ');

    return this.date(val, dateTime[0]);
  },

  booleanToYesNo (val?: boolean): string {
    if (val) return 'Yes';
    return typeof val === 'undefined' ? '?' : 'No';
  },

  cpfOrCnpj(s: string): string {
    return s.length == 11 ? this.cpf(s): this.cnpj(s);
  },

  cpf(s: string): string {
    return s.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
  },

  cnpj(s: string): string {
    return s.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
  },

  cel(s: string): string {
    return s.replace(/(\d{2})(\d{4,5})(\d{4})/, '($1) $2-$3');
  },


  cep(s: string): string {
    return s.replace(/(\d{2})(\d{3})(\d{3})/, '$1.$2-$3');
  },

  telCelFormat(s: string): string {
    return s.length < 11 ? s.replace(/(\d{2})(\d{4})(\d{4})/, '($1) $2-$3') : s.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3');
  }

};