EvanHahn / HumanizeDuration.js

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

Feature: add option for keeping zeros in the output #110

Closed nfvs closed 6 years ago

nfvs commented 7 years ago

Sometimes it may make sense having the output with the same length by keeping zeros.

Eg. 21h 06m 00s instead of 21h 6m

EvanHahn commented 7 years ago

What do you think the API should look like?

nfvs commented 7 years ago

I guess just an option like {leading_zeros: True}

EvanHahn commented 7 years ago

It seems like the option might need to be numeric, like { leadingZeros: 2 } so that the library knows how many leading zeros to insert. Do you agree?

nfvs commented 7 years ago

I'm afraid I don't :-)

There are only 60 seconds in a minute, 60 minutes in an hour and 24hours in a day. I don't see a case where you would ever want more than two digits. Maybe calling it leading_zeros is misleading?

My idea was for eg. 1h becoming 01h 00m 00s.

EvanHahn commented 7 years ago

It might make a little more sense for things like years or milliseconds, where the number of leading zeros isn't necessarily constant. There are some other examples, too, like if you want to humanize the whole string in hours only.

What do you think?

nfvs commented 7 years ago

I think that can increase complexity quite a bit, for little gains. You could also argue that you might want a different number of digits for different units (2 digits for seconds, 3 for milliseconds), so now you'd need to declare more than one number.

I think that keeping it simple and restricting it to a reasonable scenario of 2 digits for hours/minutes/seconds and 3 for milliseconds would cover most cases. Years are not divisible so I don't think it makes sense to worry about them.

Just my personal opinion of course :)

EvanHahn commented 7 years ago

What about something more flexible? Perhaps something that'd return each unit with its number and you could build this functionality yourself?

robokozo commented 7 years ago

I'm listing a bunch of humanized times and would love a feature like this in order to keep the columns nice and tidy.

Maybe a new config option that works similarly to the language/word configuration for creating a language

For instance, I'd create a custom humanizer that could be configured like this:

humanizeDuration.humanizer({
  numberRendering: {
    y: x => x,
    mo: x => pad(x, 2, '0'),
    w: x => pad(x, 2, '0'),
    d: x => pad(x, 2, '0'),
    h: x => pad(x, 2, '0'),
    m: x => pad(x, 2, '0'),
    s: x => pad(x, 2, '0'),
    ms: x => pad(x, 3, '0'),
  },

The defaults would just return the original

EvanHahn commented 6 years ago

This library is in maintenance mode, so I won't be adding new features, sadly. I'm going to close this issue. See #120 for more.

nfvs commented 6 years ago

@EvanHahn sorry to hear that, but thanks for your work 🍻