EvanHahn / HumanizeDuration.js

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

An API to return the parts of the computed humanized duration #201

Closed solimant closed 3 years ago

solimant commented 3 years ago

🙋 Feature Request

It would be nice if we can have a humanizeDurationParts that can return the parts of the computed humanized duration.

🤔 Expected Behavior

humanizeDurationParts would return a shaped representation of the humanized duration. For example, something like:

humanizeDurationParts(12000); // {largest: 'second', parts: [{unit: 'second', value: 12}]}
humanizeDurationParts(97320000); // {largest: 'day', parts: [{unit: 'day', value: 1}, {unit: 'hour', value: 3}, {unit: 'minute', value: 2}]}

Alternatively, we can discuss what's the best shape to return; for example:

😯 Current Behavior

There is no such API.

💁 Possible Solution

Parse the output string of humanizeDuration? But what do you do if the output string is in a different language?

🔦 Context

Be able to use this with the Intl.RelativeTimeFormat API so that one can be able to pass the correct unit to the API to render something like 3 months ago.

💻 Examples

const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
const {value, unit} = humanizeDurationParts(12000)[0]; // assuming ordered array shape
rtf.format(value, unit); // "in 12 seconds"

OR

const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
const {value, unit} = humanizeDurationParts(-70000)[0]; // assuming ordered array shape
rtf.format(value, unit) // "1 minute ago"
EvanHahn commented 3 years ago

Would the parse-ms module work for you?

This library is actively maintained but no new features will be added, so I'm going to close this issue, but I'm happy to discuss a fork or help out with another module.

solimant commented 3 years ago

Would the parse-ms module work for you?

This library is actively maintained but no new features will be added, so I'm going to close this issue, but I'm happy to discuss a fork or help out with another module.

Yes, that works, thank you very much!