EvanHahn / HumanizeDuration.js

361000 becomes "6 minutes, 1 second"
https://evanhahn.github.io/HumanizeDuration.js/
The Unlicense
1.65k stars 175 forks source link

Return zero if value rounds away to zero #62

Closed mkavanagh closed 9 years ago

mkavanagh commented 9 years ago

Currently, if you pass round: true and the value to be formatted is less than half of the smallest allowed unit, you get an empty string as result:

> humanize(500, {round: true});
'0.5 seconds'
> humanize(499, {round: true});
''

IMO this makes more sense:

> humanize(500, {round: true});
'0.5 seconds'
> humanize(499, {round: true});
'0 seconds'

(i.e. if we're on the last unit and we haven't added anything to the result yet, do it regardless of whether the unit count is zero)

EvanHahn commented 9 years ago

Good catch—this is a bug.

Related question: right now, there's a bit of code that returns "0" if you pass exactly zero.

humanizeDuration(0)  // => "0"

What should happen there?

mkavanagh commented 9 years ago

my preference would be to return 0 in the smallest allowed unit in either case - then it still reads as a duration, e.g.

humanizeDuration(0, {units: ['day']}); // => "0 days"
EvanHahn commented 9 years ago

Agreed.

TheMcMurder commented 9 years ago

+1