-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intl format relative #2173
Intl format relative #2173
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, code and tests look good.
Besides review comments I have few requests:
-
Please rewrite it to TypeScript. If you never worked with it or just need advice - ping me, I'll help you.
-
Let's change the behavior. You can see that our
formatDistance
picks the most appropriate unit depending on the distance. The less the distance the smaller the unit. Let emulate this algo but allow users passunit
as well. So that we can moveunit
to options and make it optional.Here's the algo I suggest (tell me if you have other ideas)
- Less than minites:
second
- Less than hour:
minute
- Less than day:
hour
- Less than week:
day
- Less than quarter:
month
- Less than year:
quarter
- More or equal to year:
year
- Less than minites:
-
Let's rename the function to
intlFormatDistance
and make API similar to https://date-fns.org/v2.16.1/docs/formatDistance. Initially, I wanted to keep it similar to Intl API, but realized that it will confuse our users. -
Add a second date argument like in
formatDistance
and use it in calculations instead ofnew Date()
. -
Add another function variation
intlFormatDistanceToNow
similar to https://date-fns.org/v2.16.1/docs/formatDistanceToNow that uses Date.now() as the second argument.
WIP:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good progress. Please format the code using Prettier and address my suggestions.
src/intlFormatDistance/index.ts
Outdated
* For reference see: | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of forcing users to read MDN, we can add either example for each option variant or a table to the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this ok ?
e.g. "hi": Hindi (language) or "de-AT": German (language) as used in Austria (region)
Awesome work! While working on my own implementation I found a gotcha with When I compare two dates, today (Feb 24, 2020) and almost 2 years ago (March 30, 2019). new Intl.RelativeTimeFormat('en', {
numeric: "auto"
}).format(-1, 'year')
// 'last year' The majority of Intl implementations will return https://codesandbox.io/s/musing-river-90m6t?file=/src/index.ts Since your proposed implementation always takes two dates, you could prevent this error from occurring by having additional checks in the unit resolver that only applies 'auto' when the two compared years differ exactly 1 year, and force 'numeric' in all other cases. @kossnocorp It occurs to me that maybe this more opinionated/smart logic should be in |
Or... split this implementation to:
My 2cts would be to avoid providing the exact arguments of An other thing to review would be wether outputting 'quarters' by default makes sense to most implementations. Date-fns is slightly opinionated in its other implementations (i.e. when to include time in 'relative' and never returning '4 quarters ago' in formatDistance), providing that as an option to control would make this more useful, otherwise I would find myself going back to first comparing the dates, then calling intlFormatDistance with unit months when I know we're talking about 3-12 months distance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some more new comments
@robertvanhoesel, this is an excellent point. Adding special logic for Using I think instead we can use We also should make new Intl.RelativeTimeFormat('en').format(0, 'year')
//=> "in 0 years" ..it is not a good default option. However, I'm not sure what are side effects of this change. @tan75 could you please explore that? |
@kossnocorp
however it looks like explicitly adding
The same happens to 1 month and 1 year |
hi @robertvanhoesel Any ideas why? |
@tan75 the typescript compiler needs to know that you are using es2020.intl definitions. In .tsconfig you should add es2020 to the target. Also, this is a good case to catch, as older browser will also not know about this constructor and fail. |
I use the below logic to check wether the API is available in the browser. const IntlRelativeSupported = typeof Intl.RelativeTimeFormat === 'function'; |
Thank a lot for your answer! Do you mean tsconfig.json? I added it here |
1f8a942
to
c8e4782
Compare
4ae5190
to
f2beb0f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐️
551d7ba
to
9154c43
Compare
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
Co-authored-by: Sasha Koss <koss@nocorp.me>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
Hi, I can't pass this error. I am using react native version is 0.68.2, react version is 17.0.2 and using date-fns version is 2.29.3. Code reference doc here. I am using also intl version is 1.2.5. Maybe this is to cause the error? I check with this code
Codes:
|
* @throws {RangeError} `options.style` must not be invalid style | ||
* | ||
* @example | ||
* // What is the distance between the dates when the fist date is after the second? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - first
Hi, I also encountered this problem. The same result as here. Any workaround? I need something exactly like intlFormatDistance with its "... ago" etc. Making if statement is not good way for me, because I need it to work on every devices.
|
New Feature
Implement intlFormatDistance for date-fns