EvanHahn / HumanizeDuration.js

361000 becomes "6 minutes, 1 second"

Home Page:https://evanhahn.github.io/HumanizeDuration.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: add option for keeping zeros in the output

nfvs opened this issue · comments

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

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

What do you think the API should look like?

I guess just an option like {leading_zeros: True}

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?

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.

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?

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 :)

commented

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

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.

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