Skip to content

Commit

Permalink
Added tiny export and test for Korean. Closes pr#7
Browse files Browse the repository at this point in the history
  • Loading branch information
purecatamphetamine committed Jan 24, 2018
1 parent 2344bd5 commit abe2b71
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 227 deletions.
1 change: 1 addition & 0 deletions locale/ko/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports =
{
locale: 'ko',
tiny: require('./tiny.json'),
long: require('./long.json'),
short: require('./short.json'),
narrow: require('./narrow.json'),
Expand Down
247 changes: 21 additions & 226 deletions source/JavascriptTimeAgo.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import javascript_time_ago from '../source/JavascriptTimeAgo'
import { twitterStyle, timeStyle } from '../source/style'
import JavascriptTimeAgo from '../source/JavascriptTimeAgo'
import { day, month, year } from '../source/gradation'

// Load locale specific relative date/time messages
Expand All @@ -9,67 +8,61 @@ describe(`time ago`, function()
{
it(`should try various flavours if some are not found`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
time_ago.format(Date.now(), { flavour: ['exotic', 'short'] }).should.equal('now')
})

it(`should fallback from "tiny" to "narrow" for Twitter style for autogenerated locales`, function()
{
const time_ago = new javascript_time_ago('de')
time_ago.format(Date.now() - 3 * 60 * 60 * 1000, 'twitter').should.equal('vor 3 Std.')
})

it(`should accept a string style argument`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
time_ago.format(Date.now(), 'twitter').should.equal('')
time_ago.format(Date.now(), 'time').should.equal('just now')
time_ago.format(Date.now(), 'exotic').should.equal('just now')
})

it(`should accept empty constructor parameters`, function()
{
const time_ago = new javascript_time_ago()
const time_ago = new JavascriptTimeAgo()
time_ago.format(new Date()).should.equal('just now')
})

it(`should accept Dates`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
time_ago.format(new Date()).should.equal('just now')
})

it(`should not accept anything but Dates and timestamps`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
const thrower = () => time_ago.format('Jan 14, 2017')
thrower.should.throw('Unsupported relative time formatter input: string, Jan 14, 2017')
})

it(`should return an empty string if the passed units are not available in locale data`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
time_ago.format(Date.now(), { units: ['femtosecond'] }).should.equal('')
})

it(`should return an empty string if no unit is suitable`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')
const now = Date.now()

// Remove 'now' unit formatting rule temporarily
const just_now_formatter = javascript_time_ago.locales.en.long.now
delete javascript_time_ago.locales.en.long.now
const just_now_formatter = JavascriptTimeAgo.locales.en.long.now
delete JavascriptTimeAgo.locales.en.long.now

time_ago.format(now, { now }).should.equal('')

// Restore 'now' unit formating rule
javascript_time_ago.locales.en.long.now = just_now_formatter
JavascriptTimeAgo.locales.en.long.now = just_now_formatter
})

it(`should format for a style with "custom" function`, function()
{
const time_ago = new javascript_time_ago('en')
const time_ago = new JavascriptTimeAgo('en')

// `custom` returns a string
time_ago.format(Date.now(),
Expand All @@ -92,196 +85,6 @@ describe(`time ago`, function()
.should.equal('just now')
})

it(`should format Twitter style relative time (English)`, function()
{
const time_ago = new javascript_time_ago('en')

const now = new Date(2016, 3, 10, 22, 59).getTime()
const elapsed = (time) => time_ago.format(now - time * 1000, { now, ...twitterStyle })

elapsed(0).should.equal('')
elapsed(44.9).should.equal('')
elapsed(45.1).should.equal('1m')
elapsed(1.49 * 60).should.equal('1m')
elapsed(1.51 * 60).should.equal('2m')
elapsed(2.49 * 60).should.equal('2m')
elapsed(2.51 * 60).should.equal('3m')
// …
elapsed(59.49 * 60).should.equal('59m')
elapsed(59.51 * 60).should.equal('1h')
elapsed(1.49 * 60 * 60).should.equal('1h')
elapsed(1.51 * 60 * 60).should.equal('2h')
elapsed(2.49 * 60 * 60).should.equal('2h')
elapsed(2.51 * 60 * 60).should.equal('3h')
// …
elapsed(23.49 * 60 * 60).should.equal('23h')
elapsed(day + 2 * 60 + 60 * 60).should.equal('Apr 9')
// …
elapsed(year).should.equal('Apr 11, 2015')
})

it(`should format Twitter style relative time (Russian)`, function()
{
const time_ago = new javascript_time_ago(['ru'])

const now = new Date(2016, 3, 10, 22, 59).getTime()
const elapsed = time => time_ago.format(now - time * 1000, { now, ...twitterStyle })

elapsed(0).should.equal('')
elapsed(44.9).should.equal('')
elapsed(45.1).should.equal('1м')
elapsed(1.49 * 60).should.equal('1м')
elapsed(1.51 * 60).should.equal('2м')
elapsed(2.49 * 60).should.equal('2м')
elapsed(2.51 * 60).should.equal('3м')
// …
elapsed(59.49 * 60).should.equal('59м')
elapsed(59.51 * 60).should.equal('1ч')
elapsed(1.49 * 60 * 60).should.equal('1ч')
elapsed(1.51 * 60 * 60).should.equal('2ч')
elapsed(2.49 * 60 * 60).should.equal('2ч')
elapsed(2.51 * 60 * 60).should.equal('3ч')
// …
elapsed(23.49 * 60 * 60).should.equal('23ч')
elapsed(day + 62 * 60).should.equal('9 апр.')
// …
elapsed(year).should.equal('11 апр. 2015 г.')
})

it(`should format "time" style relative time (English)`, function()
{
const time_ago = new javascript_time_ago('en-US')

convenient_gradation_test
([
'just now',
'1 minute',
'2 minutes',
'5 minutes',
'10 minutes',
'15 minutes',
'20 minutes',
'half an hour',
'half an hour',
'half an hour',
'half an hour',
'1 hour',
'1 hour',
'1 hour',
'2 hours',
'3 hours',
'4 hours',
'5 hours',
'6 hours',
'7 hours',
'8 hours',
'9 hours',
'10 hours',
'11 hours',
'12 hours',
'13 hours',
'14 hours',
'15 hours',
'16 hours',
'17 hours',
'18 hours',
'19 hours',
'20 hours',
'1 day',
'2 days',
'3 days',
'4 days',
'5 days',
'1 week',
'2 weeks',
'3 weeks',
'1 month',
'2 months',
'3 months',
'4 months',
'half a year',
'half a year',
'half a year',
'half a year',
'half a year',
'1 year',
'1 year',
'1 year',
'2 years',
'3 years',
'100 years'
],
time_ago,
'time')
})

it(`should format "time" style relative time (Russian)`, function()
{
const time_ago = new javascript_time_ago('ru-RU')

convenient_gradation_test
([
'только что',
'1 минута',
'2 минуты',
'5 минут',
'10 минут',
'15 минут',
'20 минут',
'полчаса',
'полчаса',
'полчаса',
'полчаса',
'1 час',
'1 час',
'1 час',
'2 часа',
'3 часа',
'4 часа',
'5 часов',
'6 часов',
'7 часов',
'8 часов',
'9 часов',
'10 часов',
'11 часов',
'12 часов',
'13 часов',
'14 часов',
'15 часов',
'16 часов',
'17 часов',
'18 часов',
'19 часов',
'20 часов',
'1 день',
'2 дня',
'3 дня',
'4 дня',
'5 дней',
'1 неделю',
'2 недели',
'3 недели',
'1 месяц',
'2 месяца',
'3 месяца',
'4 месяца',
'полгода',
'полгода',
'полгода',
'полгода',
'полгода',
'1 год',
'1 год',
'1 год',
'2 года',
'3 года',
'100 лет'
],
time_ago,
'time')
})

it(`should format time correctly for English language (short)`, function()
{
convenient_gradation_test
Expand Down Expand Up @@ -343,7 +146,7 @@ describe(`time ago`, function()
'3 yr. ago',
'100 yr. ago'
],
new javascript_time_ago('en'),
'en',
{ flavour: 'short' })
})

Expand Down Expand Up @@ -408,7 +211,7 @@ describe(`time ago`, function()
'3 years ago',
'100 years ago'
],
new javascript_time_ago('en'))
'en')
})

it(`should format time correctly for Russian language (short)`, function()
Expand Down Expand Up @@ -472,7 +275,7 @@ describe(`time ago`, function()
'3 г. назад',
'100 л. назад'
],
new javascript_time_ago('ru'),
'ru',
{ flavour: 'short' })
})

Expand Down Expand Up @@ -537,29 +340,21 @@ describe(`time ago`, function()
'3 года назад',
'100 лет назад'
],
new javascript_time_ago('ru'))
'ru')
})

it(`should format future dates`, function()
{
new javascript_time_ago('en').format(Date.now() + 60 * 60 * 1000).should.equal('in an hour')
new javascript_time_ago('ru').format(Date.now() + 45 * 1000).should.equal('через минуту')
new JavascriptTimeAgo('en').format(Date.now() + 60 * 60 * 1000).should.equal('in an hour')
new JavascriptTimeAgo('ru').format(Date.now() + 45 * 1000).should.equal('через минуту')
})
})

function convenient_gradation_test(convenient_gradation_labels, time_ago, style = {})
export function convenient_gradation_test(convenient_gradation_labels, time_ago, style = {})
{
if (typeof style === 'string')
if (typeof time_ago === 'string')
{
switch (style)
{
case 'twitter':
style = twitterStyle
break
case 'time':
style = timeStyle
break
}
time_ago = new JavascriptTimeAgo(time_ago)
}

const now = Date.now()
Expand Down

0 comments on commit abe2b71

Please sign in to comment.