EvanHahn / HumanizeDuration.js

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

Decimals for units other than seconds #150

Closed graemian closed 4 years ago

graemian commented 5 years ago

I'd like to have output like "12.4 days" or "6.5 weeks", but I can't figure out how to get this.

I tried { largest: 1, round: false } but it doesn't work. Is this possible?

EvanHahn commented 5 years ago

Hmm, that seems like it should work.

I don't have lots of time to maintain this module, unfortunately—any chance you could attempt a pull request?

guanzo commented 5 years ago

Running into the same issue. Happy to do a PR, but first I want to define the problem.

const ms = 1000 * 60 * 60 * 1.5 // 1.5 hours

humanizeDuration(ms, {
    units: ['h', 'm'],
    largest: 1,
})
// Current output: "1 hour"
// Expected output?: "1.5 hours"

humanizeDuration(ms, {
    units: ['h'],
    largest: 1,
})
// Current output: "1.5 hours"
// Expected output: "1.5 hours"

Does the first call output a buggy result in your opinion? Should these 2 calls return the same output?

EDIT: May have found an easy solution. Change this line

https://github.com/EvanHahn/HumanizeDuration.js/blob/c713bde4b7405bf2a1d1c61b637356034f543656/humanize-duration.js#L577

to this:

if (i + 1 === len || (has(options, 'largest') && options.largest + 1 === len)) {
const ms = 1000 * 60 * 60 * 1.5 // 1.5 hours
humanizeDuration(ms, {
    units: ['h', 'm'],
    largest: 1,
})
// Output: "1.5 hours"

Tests still pass after this change.

EvanHahn commented 5 years ago

That change seems reasonable to me.

This feels like a bugfix and not a breaking change, but are there any cases where it could arguably be a breaking change? If so, I'm more hesitant to make this change.

guanzo commented 5 years ago

Understandable. The fact that the existing tests stilled passed after my modifications is worrisome. I don't have the time to discover breaking changes, so it may be better to leave this alone.

EvanHahn commented 5 years ago

I admit that this is the kind of thing that makes me not want to maintain this library any more—there is a large matrix of possible options that are hard to test exhaustively. (I'm sure it could be done by a more competent maintainer.) For example, what happens if you throw round into the mix here?

I'm inclined to keep things as is. However, if someone wants to fork this library to fix this bug and make some additional changes (like the one in #141), I'd link to that fork.

EvanHahn commented 4 years ago

There hasn't been movement on this issue in several months so I'm going to close.

Feel free to let me know if you'd like me to reopen—I don't want to stop important discussion, I just want to clean up old issues.