-
Notifications
You must be signed in to change notification settings - Fork 24
units to consider when selecting the unit #7
Comments
We have bigger problems as described in #6, which might prevent us from using bestfit. |
@rxaviers yes, we can resume this. I have been thinking about this one in the context of #7, and I think we can get away with removing |
I'm afraid that removing bestFit would cause the user to have to create a new RTF object for each unit he'd like to format against. Alternatively, we could have a base algorithm and let users provide a custom one, sth like: new RelativeTimeFormat('en', {
unit: 'bestFit',
bestFit: function(x) {
...
return 'second';
}
}); |
That should not be a problem. In fact most apps that I have been will provide abstraction layers for Intl stuff since they want to control the locale, and other settings. Look at formatjs, momentjs, etc.
Even though this is not in ECMAScript (e.g.: |
With respect to bestFit... On Globalize, we chose to go with a very simple API initially that doesn't include bestFit. Therefore, in order for developers to do bestFit on their own, they actually need to instantiate several formatters (one for each unit). Not that this is a problem, but it's something to keep in mind, because it would be the case in here as well. For example, devs would have to do something like this: let secondFmt = new Intl.RelativeTimeFormat(locales, {unit: 'second'});
let minuteFmt = new Intl.RelativeTimeFormat(locales, {unit: 'minute'});
...
let yearFmt = new Intl.RelativeTimeFormat(locales, {unit: 'year'});
function bestFitFmt(date) {
switch(bestFitCalcs(date)) {
case 'second': return secondFmt(date);
case 'minute': return minuteFmt(date);
...
default: return yearFmt(date);
};
} |
I believe that this conversation is relevant to both, RelativeTimeFormat and UnitFormat, which faces similar challenges. I would say that having to create all formatters, every time you want to use it is a bad API design. I understand that we aim to focus on building low level blocks and then letting libraries use them, but I don't think that we should take it to the point where the API itself is unusable without a wrapper. So, so far we discussed either going for a sensible "bestFit" and allow people to override it, either by specifying units (so that you can achieve what @rxaviers suggests - just say Here's a third way. We create a stateful object not for a unit, but for a let rtf = new Intl.RelatveTimeFormat('en', {
category: 'duration'
});
rtf.format(2, 'second');
rtf.format(-10, 'minute'); This would do three things:
|
@zbraniecki I like where this is going. It's only not immediately clear to me what |
Oh, yes, sorry! :) For relativetimeformat, we don't need it, but we will for UnitFormat. So, the updated proposal: let rtf = new Intl.RelatveTimeFormat('en');
rtf.format(2, 'second');
rtf.format(-10, 'minute'); |
@zbraniecki this API is closer to ICU http://icu-project.org/apiref/icu4j/com/ibm/icu/text/RelativeDateTimeFormatter.html. Note that on ICU, they call |
since we don't support best-fit anymore, this issue is not longer needed. |
Currently, the spec is considering "all" units possible when trying to determine the correct one - seconds, minutes, hours, days, weeks, months, quarters, years.
We still have more units to consider like "last Tuesday", and I can see users wanting to skip "quarters" or calculate only up to days, so - seconds, minutes, hours, normally, but nothing above days (so "4500 days ago").
It seems like a simple solution would be to allow people to pass the list of units to consider:
Then, if you want to add weekdays or quarters, you'd just list it as one of the units.
Does it sound realistic?
The text was updated successfully, but these errors were encountered: