joanllenas / ngx-date-fns

⏳ date-fns pipes for Angular
163 stars 14 forks source link

Allow empty value (Prevent throwing "Invalid time value") #344

Closed waveiron closed 3 years ago

waveiron commented 3 years ago

Description of the Issue and Steps to Reproduce:

Did you search for duplicate issue? [Yes]

Please describe the issue and steps to reproduce, preferably with a code sample / plunker:

@Component({
  selector: 'app-a',
  template: `
    <div>{{empty|dfnsFormatDistanceToNow}}</div>
  `,
})
export class ExamBComponent implements OnInit {
  empty: undefined = undefined;
}

This will throw:

ERROR RangeError: Invalid time value
    at formatDistance (index.js:134)
    at formatDistanceToNow (index.js:111)
    at FormatDistanceToNowPipe.transform (ngx-date-fns.js:971)
    ...
joanllenas commented 3 years ago

Hi @waveiron , Not accepting invalid input values was a decision I made when I started this project, but at this point, I agree that this is not what you expect from a pipe.

I will probably follow a pattern similar to what Angular uses for its pipes: https://github.com/angular/angular/blob/11.0.3/packages/common/src/pipes/number_pipe.ts#L239

Does that work for you?

For consistency sake, I will apply the same fix to other pipes where it makes sense to do it.

waveiron commented 3 years ago

@joanllenas That will work, since pipes accept "loose" data in view level. Thanks for your great work.

joanllenas commented 3 years ago

Hi @waveiron Just released v7 with your suggestion implemented: https://github.com/joanllenas/ngx-date-fns/releases/tag/v7.0.0 Cheers!

waveiron commented 3 years ago

@joanllenas Wow, thanks for your great work!