EvanHahn / HumanizeDuration.js

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

Using a timeformat specifier for humanizing #9

Closed irfn closed 10 years ago

irfn commented 10 years ago

Here is the changes required to implement Issue #8

EvanHahn commented 10 years ago

I like this feature, but I don't like the API. I think it should look like this:

humanizeDuration(123, { lang: 'en', format: 'Y,Mo,D' })

And what happens when you do something like this? Is it nothing? One month?

humanizeDuration((1).week(), { format: 'Mo' })

I think we should also decide whether you round up or not. This doesn't really matter at the millisecond perspective (who cares about nanoseconds?) but it matters if you're humanizing months. Should 14 days be "0 months" or "1 month"? Or what?

humanizeDuration(123.4, { round: 'down' }) // as if you humanized 123
humanizeDuration(123.4)                    // as if you humanized 123
humanizeDuration(123.4, { round: 'up' })   // as if you humanized 124

Let's figure out what this looks like before I merge. I like the feature, I'm just a stickler for the details.

irfn commented 10 years ago

Agreed about the API. I was wondering about backward compatibility for the lang param. Thats why added a new param. One other options is that we keep the humanizeDuration as is and introduce a new top level api.

I like the round option, though beyond days it can become tricky as you pointed out. Whether 14 days ~ 0 Months or 16 days ~ 1 month. Both sound weird. I dont have any solutions, but i was just going by a requirement in my recent work. I think the best option is to keep this feature non default and provide a lot of options to customise output as required.

EvanHahn commented 10 years ago

Because we're using semantic versioning, we can change the API as long as we're under version 1.

What happens here?

humanize((14).days(), { format: 'Mo', round: 'down' }) // "0 months"
humanize((14).days(), { format: 'Mo', round: 'up' })   // "1 month"
humanize((14).days(), { format: 'Mo,D', round: 'up' }) // ?

What about something like this?

var components = humanize.components((36).days())
components.months == '1 month'
components.total.months == '1 month'
components.days == '6 days'
components.total.days == '36 days'
components.hours == '0 hours'
components.total.hours == '864 hours'

The example you gave #8 could be done like this:

var components = humanize.components((240).hours() + (20).minutes())
[components.total.hours, components.minutes].join(', ') + ' used of 1000 hours'

I want to be careful not to re-implement Moment's duration stuff. If this gets too complex, a potential user will have no reason to choose Humanize Duration over Moment.