EvanHahn / HumanizeDuration.js

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

Optionally omit small units #19

Closed sarim closed 10 years ago

sarim commented 10 years ago

When calculating a big duration, i want to omit seconds and milliseconds.

Instead of XX hours, XX minutes, XX seconds, XX milliseconds , XX hours, XX minutes looks more human readable.

EvanHahn commented 10 years ago

What about this?

humanizeDuration(1234, { units: "hm" }) // => "XX hours, XX minutes"
sarim commented 10 years ago

yes that would work. But i think something like this,

var hD = humanizeDuration({
    units: "hm",
    delimiter: ","
});

hD(1234);

This way it'll be possible to add more user defined configurations.

EvanHahn commented 10 years ago

So you'd prefer humanizeDuration to be more of a constructor object? I think I'd prefer for it to be a function, but I do like the idea of passing in many options.

Thoughts?

sarim commented 10 years ago

Umm,

humanizeDuration.setOptions({
    units: "hm",
    delimiter: ","
});

humanizeDuration(1234);
humanizeDuration.prototype.options = {
    units: "hm",
    delimiter: ","
};

humanizeDuration(1234);

Something like these?

EvanHahn commented 10 years ago

What about this, as a subtle difference?

humanizeDuration(10000000) // 2 hours, 46 minutes, 40 seconds

humanizeDuration.setDefaults({
  units: "hm",
  delimiter: ", "
})

humanizeDuration(10000000)                         // 2 hours, 46 minutes
humanizeDuration(10000000, { delimiter: " and " }) // 2 hours and 46 minutes
humanizeDuration(10000000, { units: "s" })         // 10000 seconds
sarim commented 10 years ago

Ah, it like this. Options passed when calling the function would override the default options, nice :)

EvanHahn commented 10 years ago

Working on it now...

EvanHahn commented 10 years ago

@sarim I'm realizing an issue with changing the defaults; if two modules depend on HumanizeDuration and either of them change the defaults, that's bad. I'd really like to have an easy-to-use function that everyone can use, but I'd also like to make it customizable.

What do you think of this?

humanizeDuration(10000000)                        // 2 hours, 46 minutes, 40 seconds
humanizeDuration(10000000, { units: ["second"] }) // 10000 seconds

var h = humanizeDuration.humanizer({
  units: ["hour", "minute"],
  delimiter: " and "
})
h(10000000) // 2 hours, 46 minutes
sarim commented 10 years ago

Yeah, that looks much cleaner. :+1:

EvanHahn commented 10 years ago

Finished this. This will go in the 2.0.0 release.