All files / libs/kernel/api/src/lib/pipes date.pipe.ts

100% Statements 34/34
100% Branches 4/4
100% Functions 2/2
100% Lines 34/34

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 31 32 33 34 351x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 9x 9x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 9x 9x 9x 1x  
import { Injectable } from '@nestjs/common';
 
import { dayjs } from '@amalia/ext/dayjs';
 
import { BaseValidationPipe } from './base-validation.pipe';
 
@Injectable()
export class DatePipe extends BaseValidationPipe<Date> {
  public override validateOne(value: Date): boolean {
    return dayjs(value).isValid();
  }
 
  public override transformOne(value: unknown): Date {
    return typeof value === 'string'
      ? // Pass format as undefined so the date is parsed as ISO8601. Pass true to force the date to be valid (no overflow and other weird stuff).
        dayjs(
          value,
          [
            'YYYY-MM-DDTHH:mm:ss.SSS[Z]',
            'YYYY-MM-DDTHH:mm:ss.SSSZZ',
            'YYYY-MM-DDTHH:mm:ss.SSSZ',
            'YYYY-MM-DDTHH:mm:ss.SSS',
            'YYYY-MM-DDTHH:mm:ss[Z]',
            'YYYY-MM-DDTHH:mm:ssZZ',
            'YYYY-MM-DDTHH:mm:ssZ',
            'YYYY-MM-DDTHH:mm:ss',
            'YYYY-MM-DD',
          ],
          true,
        ).toDate()
      : // Should not happen in real life but the tests test that.
        dayjs(value as Date).toDate();
  }
}