-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters