Skip to content

Commit

Permalink
[Date] Relative date badge (#2244)
Browse files Browse the repository at this point in the history
Close #749
  • Loading branch information
Ang-YC authored and paulmelnikow committed Jan 4, 2019
1 parent 576f30f commit f6357da
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/text-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ function formatDate(d) {
return dateString.replace(` ${moment().year()}`, '').toLowerCase()
}

function formatRelativeDate(timestamp) {
return moment()
.to(moment.unix(parseInt(timestamp, 10)))
.toLowerCase()
}

function renderTestResultMessage({
passed,
failed,
Expand Down Expand Up @@ -193,6 +199,7 @@ module.exports = {
addv,
maybePluralize,
formatDate,
formatRelativeDate,
renderTestResultMessage,
renderTestResultBadge,
}
23 changes: 23 additions & 0 deletions lib/text-formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
addv,
maybePluralize,
formatDate,
formatRelativeDate,
renderTestResultMessage,
renderTestResultBadge,
} = require('./text-formatters')
Expand Down Expand Up @@ -100,6 +101,28 @@ describe('Text formatters', function() {
})
})

context('in october', function() {
let clock
beforeEach(function() {
clock = sinon.useFakeTimers(new Date(2018, 9, 29).getTime())
})
afterEach(function() {
clock.restore()
})

test(formatRelativeDate, () => {
given(new Date(2018, 9, 31).getTime() / 1000)
.describe('when given the end of october')
.expect('in 2 days')
})

test(formatRelativeDate, () => {
given(new Date(2018, 9, 1).getTime() / 1000)
.describe('when given the beginning of october')
.expect('a month ago')
})
})

function renderBothStyles(props) {
const { message: standardMessage, color } = renderTestResultBadge(props)
const compactMessage = renderTestResultMessage({
Expand Down
55 changes: 55 additions & 0 deletions services/date/date.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const BaseService = require('../base')
const { formatRelativeDate } = require('../../lib/text-formatters')

const documentation = `
<p>
Supply a unix timestamp in seconds to display the relative time from/to now
</p>
`

module.exports = class Date extends BaseService {
static render({ relativeDateString }) {
return {
message: relativeDateString,
color: relativeDateString === 'invalid date' ? 'grey' : 'blue',
}
}

async handle({ timestamp }) {
return this.constructor.render({
relativeDateString: formatRelativeDate(timestamp),
})
}

// Metadata
static get defaultBadgeData() {
return { label: 'date' }
}

static get category() {
return 'other'
}

static get route() {
return {
base: 'date',
format: '([0-9]+)',
capture: ['timestamp'],
}
}

static get examples() {
return [
{
title: 'Relative date',
pattern: ':timestamp',
namedParams: { timestamp: '1540814400' },
staticExample: this.render({ relativeDateString: '2 days ago' }),
keywords: ['date', 'time', 'countdown', 'countup', 'moment'],
documentation,
},
]
}
}
18 changes: 18 additions & 0 deletions services/date/date.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const Joi = require('joi')
const ServiceTester = require('../service-tester')
const { isRelativeFormattedDate } = require('../test-validators')

const t = new ServiceTester({ id: 'date', title: 'Relative Date Tests' })
module.exports = t

t.create('Relative date')
.get('/1540814400.json')
.expectJSONTypes(
Joi.object().keys({ name: 'date', value: isRelativeFormattedDate })
)

t.create('Relative date - Invalid')
.get('/9999999999999.json')
.expectJSONTypes(Joi.object().keys({ name: 'date', value: 'invalid date' }))
7 changes: 7 additions & 0 deletions services/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ const isFormattedDate = Joi.alternatives().try(
)
)

const isRelativeFormattedDate = Joi.alternatives().try(
Joi.string().regex(
/^(in |)([0-9]+|a few|a|an|)(| )(second|minute|hour|day|month|year)(s|)( ago|)$/
)
)

const isDependencyState = withRegex(
/^(\d+ out of date|\d+ deprecated|up to date)$/
)
Expand Down Expand Up @@ -127,6 +133,7 @@ module.exports = {
isDecimalPercentage,
isFileSize,
isFormattedDate,
isRelativeFormattedDate,
isDependencyState,
isBuildStatus,
withRegex,
Expand Down

0 comments on commit f6357da

Please sign in to comment.