Skip to content

Commit

Permalink
#38. Added "twitter-minute-now" style.
Browse files Browse the repository at this point in the history
  • Loading branch information
purecatamphetamine committed Oct 12, 2020
1 parent 648ffe1 commit c06adb7
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 195 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<!--
TO DO: Remove the legacy compatibility of getting "now" label from "long.second.current" in runtime code (not in locale generation code — that one's as intended).
TO DO: Obtaining "now" label in runtime when not using the polyfill:
```js
new Intl.RelativeTimeFormat("en", { numeric: "auto" }).format(0, 'second')
// Outputs "now".
```
TO DO: Remove deprecated `tiny` locale labels.
TO DO: Remove "defaultLocale = 'en'": a developer will have to specify their own default locale.
Expand All @@ -20,6 +29,13 @@ TO DO: "time" style should use "round" scale instead of "approximate".
TO DO: if `style` is passed as an object then maybe it should be passed as part of `options` (and document that in the "Custom" section of the readme).
-->

2.2.8 / 12.10.2020
==================

* Added `"twitter-minute-now"` style.

* The threshold for `"now"` -> `"1m"`/`"1 minute ago"` is now 30 seconds rather than 40 seconds.

2.2.6 / 12.10.2020
==================

Expand Down
161 changes: 43 additions & 118 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ timeAgo.format(Date.now() - 1.5 * 60 * 1000, 'round')

### Round (minute)

Same as `"round"` but without seconds.
Same as `"round"` style but without seconds.

```js
timeAgo.format(Date.now(), 'round-minute')
// 0 seconds ago → "just now"

timeAgo.format(Date.now() - 39 * 1000, 'round-minute')
// 39 seconds ago → "just now"
timeAgo.format(Date.now() - 29 * 1000, 'round-minute')
// 29 seconds ago → "just now"

timeAgo.format(Date.now() - 40 * 1000, 'round-minute')
// 40 seconds ago → "1 minute ago"
timeAgo.format(Date.now() - 30 * 1000, 'round-minute')
// 30 seconds ago → "1 minute ago"

timeAgo.format(Date.now() - 1.5 * 60 * 1000, 'round-minute')
// 1.5 minutes ago → "2 minutes ago"
Expand All @@ -231,115 +231,6 @@ timeAgo.format(Date.now() - 1.5 * 60 * 1000, 'round-minute')
* 2 minutes ago
*

<!--
### Approximate
`"approximate"` style is a legacy one that has been introduced in the early versions of this library. It's basically the same as `"round-minute"` with the difference that it rounds time in some cases:
* `just now` → `just now`
* `40 seconds ago` → `just now`
* `45 seconds ago` → `1 minute ago`
* `5 minutes ago` → `5 minutes ago`
* `6 minutes ago` → `5 minutes ago`
* `7 minutes ago` → `5 minutes ago`
* `8 minutes ago` → `10 minutes ago`
* `9 minutes ago` → `10 minutes ago`
* `10 minutes ago` → `10 minutes ago`
* …
```js
timeAgo.format(Date.now(), 'approximate')
// 0 seconds ago → "just now"
timeAgo.format(Date.now() - 15 * 1000, 'approximate')
// 15 seconds ago → "just now"
timeAgo.format(Date.now() - 1.5 * 60 * 1000, 'approximate')
// 1.5 minutes ago → "2 minutes ago"
timeAgo.format(Date.now() - 3 * 60 * 1000, 'approximate')
// 3 minutes ago → "5 minutes ago"
```
* just now
* 1 minute ago
* 2 minutes ago
* 5 minutes ago
* 10 minutes ago
* 15 minutes ago
* 20 minutes ago
* …
* 50 minutes ago
* 1 hour ago
* 2 hours ago
* …
* 20 hours ago
* 1 day ago
* 2 days ago
* …
* 5 days ago
* 1 week ago
* 2 weeks ago
* 3 weeks ago
* 1 month ago
* 2 months ago
* …
* 10 months ago
* 1 year ago
* 2 years ago
* …
For historical reasons, `"approximate"` style is the one that's used when no `style` argument is passed (this will be changed in the next major version: `round` or `round-minute` will be the default one).
### Approximate (time)
`"approximate-time"` style is a legacy one that has been introduced in the early versions of this library. It's the same as the `"approximate"` style but without the "ago" part.
```js
timeAgo.format(Date.now(), 'approximate-time')
// 0 seconds ago → "just now"
timeAgo.format(Date.now() - 15 * 1000, 'approximate-time')
// 15 seconds ago → "just now"
timeAgo.format(Date.now() - 1.5 * 60 * 1000, 'approximate-time')
// 1.5 minutes ago → "2 minutes"
timeAgo.format(Date.now() - 3 * 60 * 1000, 'approximate-time')
// 3 minutes ago → "5 minutes"
```
* just now
* 1 minute
* 2 minutes
* 5 minutes
* 10 minutes
* 15 minutes
* 20 minutes
* …
* 50 minutes
* 1 hour
* 2 hours
* …
* 20 hours
* 1 day
* 2 days
* …
* 5 days
* 1 week
* 2 weeks
* 3 weeks
* 1 month
* 2 months
* …
* 10 months
* 1 year
* 2 years
* …
Not all locales support this style: only [those](https://github.com/catamphetamine/javascript-time-ago/tree/master/locale-more-styles) having `long-time.json`.
-->

### Twitter

Mimics [Twitter](https://twitter.com) style of "time ago" labels (`"1s"`, `"2m"`, `"3h"`, `"Mar 4"`, `"Apr 5, 2012"`)
Expand Down Expand Up @@ -382,6 +273,40 @@ timeAgo.format(new Date() - 1 * 1000, 'twitter-now')
// The rest is same as "twitter" style.
```

### Twitter (minute)

Same as `"twitter"` style but without seconds (starts with minutes).

```js
timeAgo.format(new Date(), 'twitter-minute')
// 0 seconds ago → "0m"

timeAgo.format(new Date() - 29 * 1000, 'twitter-minute')
// 29 seconds ago → "0m"

timeAgo.format(new Date() - 30 * 1000, 'twitter-minute')
// 30 seconds ago → "1m"

// The rest is same as "twitter" style.
```

### Twitter (minute-now)

Same as `"twitter-minute"` style but outputs `"now"` instead of `"0m"`.

```js
timeAgo.format(new Date(), 'twitter-minute-now')
// 0 seconds ago → "now"

timeAgo.format(new Date() - 29 * 1000, 'twitter-minute-now')
// 29 seconds ago → "now"

timeAgo.format(new Date() - 30 * 1000, 'twitter-minute-now')
// 30 seconds ago → "1m"

// The rest is same as "twitter-minute" style.
```

### Twitter (first minute)

Same as `"twitter"` style but doesn't output anything before the first minute.
Expand All @@ -390,11 +315,11 @@ Same as `"twitter"` style but doesn't output anything before the first minute.
timeAgo.format(new Date(), 'twitter-first-minute')
// 0 seconds ago → ""

timeAgo.format(new Date() - 39 * 1000, 'twitter-first-minute')
// 39 seconds ago → ""
timeAgo.format(new Date() - 29 * 1000, 'twitter-first-minute')
// 29 seconds ago → ""

timeAgo.format(new Date() - 40 * 1000, 'twitter-first-minute')
// 40 seconds ago → "1m"
timeAgo.format(new Date() - 30 * 1000, 'twitter-first-minute')
// 30 seconds ago → "1m"

// The rest is same as "twitter" style.
```
Expand Down
5 changes: 5 additions & 0 deletions bin/generate-locale-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ for (const locale of getAllLocales()) {
}
}

// Locales are guaranteed to have a "now" label.
if (!getNowLabel(localeData)) {
throw new Error(`"now" label not found for locale "${locale}"`)
}

// Create the locale *.json file.
fs.outputFileSync(
path.join(localesDirectory, `${locale}.json`),
Expand Down
2 changes: 1 addition & 1 deletion source/TimeAgo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe(`javascript-time-ago`, () => {
timeAgo.format(Date.now() - 45 * 1000, 'round').should.equal('45 seconds ago')
// "default" style was renamed to "round".
timeAgo.format(Date.now() - 45 * 1000, 'default').should.equal('45 seconds ago')
timeAgo.format(Date.now() - 39 * 1000, 'round-minute').should.equal('just now')
timeAgo.format(Date.now() - 29 * 1000, 'round-minute').should.equal('just now')
// "time" style was renamed to "approximate-time".
timeAgo.format(Date.now() - 2 * 60 * 1000, 'time').should.equal('2 minutes')
timeAgo.format(Date.now() - 2 * 60 * 1000, 'approximate-time').should.equal('2 minutes')
Expand Down
4 changes: 2 additions & 2 deletions source/steps/roundMinute.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default [
formatAs: 'now'
},
{
// As soon as 40 seconds have passed,
// As soon as 30 seconds have passed,
// output "minute"s.
minTime: 40,
minTime: 30,
formatAs: 'minute'
},
{
Expand Down
4 changes: 2 additions & 2 deletions source/steps/roundMinute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe('steps/roundMinute', () => {
// expect(getStepFor(0)).to.be.undefined
expect(getStepFor(0).formatAs).to.equal('now')
expect(getStepFor(0.5).formatAs).to.equal('now')
expect(getStepFor(39.9).formatAs).to.equal('now')
expect(getStepFor(40).formatAs).to.equal('minute')
expect(getStepFor(29.9).formatAs).to.equal('now')
expect(getStepFor(30).formatAs).to.equal('minute')
expect(getStepFor(59.5 * 60 - 1).formatAs).to.equal('minute')
expect(getStepFor(59.5 * 60).formatAs).to.equal('hour')
expect(getStepFor(24 * 60 * 60).formatAs).to.equal('day')
Expand Down
4 changes: 2 additions & 2 deletions source/style/roundMinute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('style/round-minute', () => {
const formatInterval = (secondsPassed) => timeAgo.format(now - secondsPassed * 1000, { now, ...roundMinute })

formatInterval(0).should.equal('just now')
formatInterval(39.9).should.equal('just now')
formatInterval(40).should.equal('1 minute ago')
formatInterval(29.9).should.equal('just now')
formatInterval(30).should.equal('1 minute ago')
formatInterval(1.49 * 60).should.equal('1 minute ago')
formatInterval(1.51 * 60).should.equal('2 minutes ago')
formatInterval(2.49 * 60).should.equal('2 minutes ago')
Expand Down
50 changes: 25 additions & 25 deletions source/style/twitter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import twitter from './twitter'
import TimeAgo from '../TimeAgo'
import { day, month, year } from '../steps'
import { hour, minute, day, month, year } from '../steps'

describe('style/twitter', () => {
it('should fallback from "mini-time" to "narrow"', () => {
const timeAgo = new TimeAgo('it')
timeAgo.format(Date.now() - 3 * 60 * 60 * 1000, 'twitter').should.equal('3 h fa')
timeAgo.format(Date.now() - 3 * hour * 1000, 'twitter').should.equal('3 h fa')
})

it('should format Twitter style relative time (English)', () => {
Expand All @@ -19,20 +19,20 @@ describe('style/twitter', () => {
formatInterval(0.5).should.equal('1s')
formatInterval(59.4).should.equal('59s')
formatInterval(59.6).should.equal('1m')
formatInterval(1.49 * 60).should.equal('1m')
formatInterval(1.51 * 60).should.equal('2m')
formatInterval(2.49 * 60).should.equal('2m')
formatInterval(2.51 * 60).should.equal('3m')
formatInterval(1.49 * minute).should.equal('1m')
formatInterval(1.51 * minute).should.equal('2m')
formatInterval(2.49 * minute).should.equal('2m')
formatInterval(2.51 * minute).should.equal('3m')
// …
formatInterval(59.49 * 60).should.equal('59m')
formatInterval(59.51 * 60).should.equal('1h')
formatInterval(1.49 * 60 * 60).should.equal('1h')
formatInterval(1.51 * 60 * 60).should.equal('2h')
formatInterval(2.49 * 60 * 60).should.equal('2h')
formatInterval(2.51 * 60 * 60).should.equal('3h')
formatInterval(59.49 * minute).should.equal('59m')
formatInterval(59.51 * minute).should.equal('1h')
formatInterval(1.49 * hour).should.equal('1h')
formatInterval(1.51 * hour).should.equal('2h')
formatInterval(2.49 * hour).should.equal('2h')
formatInterval(2.51 * hour).should.equal('3h')
// …
formatInterval(23.49 * 60 * 60).should.equal('23h')
formatInterval(day + 2 * 60 + 60 * 60).should.equal('Apr 9')
formatInterval(23.49 * hour).should.equal('23h')
formatInterval(day + 2 * minute + hour).should.equal('Apr 9')
// …
// `month` is about 30.5 days.
formatInterval(month * 3).should.equal('Jan 10')
Expand All @@ -54,8 +54,8 @@ describe('style/twitter', () => {
formatInterval(0).should.equal('0 с')
formatInterval(0.5).should.equal('1 с')
formatInterval(59.51).should.equal('1 мин')
formatInterval(59.51 * 60).should.equal('1 ч')
formatInterval(day + 62 * 60).should.equal('9 апр.')
formatInterval(59.51 * minute).should.equal('1 ч')
formatInterval(day + 62 * minute).should.equal('9 апр.')
formatInterval(year).should.equal('11 апр. 2015 г.')
})

Expand All @@ -66,8 +66,8 @@ describe('style/twitter', () => {
const formatInterval = (secondsPassed) => timeAgo.format(now - secondsPassed * 1000, { now, ...twitter })

formatInterval(59.51).should.equal('1분')
formatInterval(59.51 * 60).should.equal('1시간')
formatInterval(day + 62 * 60).should.equal('4월 9일')
formatInterval(59.51 * minute).should.equal('1시간')
formatInterval(day + 62 * minute).should.equal('4월 9일')
formatInterval(year).should.equal('2015년 4월 11일')
})

Expand All @@ -78,8 +78,8 @@ describe('style/twitter', () => {
const formatInterval = (secondsPassed) => timeAgo.format(now - secondsPassed * 1000, { now, ...twitter })

formatInterval(59.51).should.equal('1 Min.')
formatInterval(59.51 * 60).should.equal('1 Std.')
formatInterval(day + 62 * 60).should.equal('9. Apr.')
formatInterval(59.51 * minute).should.equal('1 Std.')
formatInterval(day + 62 * minute).should.equal('9. Apr.')
formatInterval(year).should.equal('11. Apr. 2015')
})

Expand All @@ -90,8 +90,8 @@ describe('style/twitter', () => {
const formatInterval = (secondsPassed) => timeAgo.format(now - secondsPassed * 1000, { now, ...twitter })

formatInterval(59.51).should.equal('1 min')
formatInterval(59.51 * 60).should.equal('1 h')
formatInterval(day + 62 * 60).should.equal('9 avr.')
formatInterval(59.51 * minute).should.equal('1 h')
formatInterval(day + 62 * minute).should.equal('9 avr.')
formatInterval(year).should.equal('11 avr. 2015')
})

Expand All @@ -102,8 +102,8 @@ describe('style/twitter', () => {
const formatInterval = (secondsPassed) => timeAgo.format(now - secondsPassed * 1000, { now, ...twitter })

formatInterval(59.51).should.equal('1分钟')
formatInterval(59.51 * 60).should.equal('1小时')
formatInterval(day + 62 * 60).should.equal('4月9日')
formatInterval(59.51 * minute).should.equal('1小时')
formatInterval(day + 62 * minute).should.equal('4月9日')
formatInterval(year).should.equal('2015年4月11日')
})

Expand All @@ -114,7 +114,7 @@ describe('style/twitter', () => {
// Intl.DateTimeFormat = undefined
//
// const timeAgo = new TimeAgo('en')
// timeAgo.format(Date.now() - 365 * 24 * 60 * 60 * 1000, 'twitter').should.equal('1yr')
// timeAgo.format(Date.now() - 365 * 24 * hour * 1000, 'twitter').should.equal('1yr')
//
// Intl.DateTimeFormat = DateTimeFormat
// })
Expand Down
Loading

0 comments on commit c06adb7

Please sign in to comment.