joanllenas / ngx-date-fns

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

Allow "0" date values #277

Closed toverux closed 4 years ago

toverux commented 4 years ago

Hello, I wanted to use the dfnsDistanceInWords pipe to convert a number in a duration string (an absolute number, ie. a time interval but not a date per se). For that, I usually cheat with distanceInWords function by passing a "zero" date for comparison, so I have two "absolute" referentials.

Ex: dateFns.distanceInWords(0, myDuration) would return something like "less than a minute".

Refactoring some code to use the pipes, I noticed that ngx-date-fns does not allow to pass "0" dates:

ERROR Error: dfnsDistanceInWords: missing required arguments
    at DistanceInWordsPipe.transform (ngx-date-fns.js:76) [angular]

The reason is that ngx-date-fns checks the input and does not allow any flasey value :

https://github.com/joanllenas/ngx-date-fns/blob/master/projects/ngx-date-fns/src/lib/distance-in-words.pipe.ts#L43

Instead of:

if (!dateToCompare || !date) {
  throw new Error(DistanceInWordsPipe.NO_ARGS_ERROR);
}

We should have :

if (dateToCompare == undefined || date == undefined) {
  throw new Error(DistanceInWordsPipe.NO_ARGS_ERROR);
}

Loosy comparison with undefined works here, 0 would be allowed but not undefined or null, which would be expected.

Obviously, there are other pipes concerned. For now, I'll use 1 instead of 0 :)

joanllenas commented 4 years ago

Hi @toverux , thanks for reporting this bug. I'll fix it ASAP.

Cheers!

joanllenas commented 4 years ago

Hey @toverux , this should be fixed now. Check v4.0.3 Cheers!

toverux commented 4 years ago

Yay! Thanks!