Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Intl.RelativeTimeFormat #104

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
4 participants
@josh
Copy link
Member

commented Apr 12, 2019

馃毀 WIP 馃毀

Aims to implement #98.

To-do

  • Feature detect Intl.RelativeTimeFormat.
  • Decide how to handle "micro" formatter.
  • Fix tests.

@josh josh requested a review from github/web-systems Apr 12, 2019

@@ -62,31 +64,29 @@ export default class RelativeTime {
const month = Math.round(day / 30)
const year = Math.round(month / 12)
if (ms < 0) {
return 'just now'
return rtf.format(0, 'second')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(0, 'second')
//=> "now"

This comment has been minimized.

Copy link
@koddsson

koddsson Apr 15, 2019

Contributor

Nice! I looked at the list here and didn't find a substitute to "Just now" so I wasn't sure how we were going to solve that 馃槃

tc39/proposal-intl-relative-time#107

} else if (sec < 45) {
return `${sec} seconds ago`
return rtf.format(-sec, 'second')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(-30, 'second')
//=> "30 seconds ago"
} else if (min < 45) {
return `${min} minutes ago`
return rtf.format(-min, 'minute')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(-30, 'minute')
//=> "30 minutes ago"
} else if (hr < 24) {
return `${hr} hours ago`
return rtf.format(-hr, 'hour')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(-30, 'hour')
//=> "30 hours ago"
} else if (sec < 90) {
return 'a minute ago'
return rtf.format(-min, 'minute')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(-1, 'minute')
//=> "1 minute ago"

Loss of "an".

@@ -2,6 +2,8 @@

import {strftime, makeFormatter, isDayFirst, isThisYear, isYearSeparator} from './utils'

const rtf = new Intl.RelativeTimeFormat('en', {numeric: 'auto'})

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member

Needs feature detection and fallback.

@@ -124,29 +124,29 @@ export default class RelativeTime {
const month = Math.round(day / 30)
const year = Math.round(month / 12)
if (month >= 18) {
return `${year} years from now`
return rtf.format(year, 'year')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(2, 'year')
//=> "in 2 years"
} else if (hr >= 24) {
return 'a day from now'
return rtf.format(day, 'day')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(1, 'day')
//=> "tomorrow"
} else if (day >= 30) {
return 'a month from now'
return rtf.format(month, 'month')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(1, 'month')
//=> "next month"
} else if (sec >= 45) {
return 'a minute from now'
return rtf.format(min, 'minute')

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member
new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(1, 'minute')
//=> "in 1 minute"
@@ -2,6 +2,8 @@

import {strftime, makeFormatter, isDayFirst, isThisYear, isYearSeparator} from './utils'

const rtf = new Intl.RelativeTimeFormat('en', {numeric: 'auto'})

export default class RelativeTime {

This comment has been minimized.

Copy link
@josh

josh Apr 12, 2019

Author Member

This RelativeTime class helper probably needs to say around to wrap are rules about which unit precision we prefer to display.

@josh

This comment has been minimized.

Copy link
Member Author

commented Apr 12, 2019

doh, @koddsson did you already start working on this? (#103)

@koddsson

This comment has been minimized.

Copy link
Contributor

commented Apr 15, 2019

@josh Nah I just started exploring and noticed that some tests for special cases that would break when we'd start using RelativeTimeFormat so I added those 馃樃

@mislav

This comment has been minimized.

Copy link
Member

commented Apr 15, 2019

  1. Is there going to be a locale attribute so that users may pass a locale specifier other than "en" down to Intl?
  2. If there isn't a locale attribute, should we attempt to read the default locale from the document, e.g. from <html lang="...">? https://www.w3.org/International/questions/qa-html-language-declarations
@keithamus

This comment has been minimized.

Copy link
Member

commented Apr 15, 2019

should we attempt to read the default locale from the document

navigator.language can be used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can鈥檛 perform that action at this time.