-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/ministryofjustice/mojdt-f…
- Loading branch information
Showing
5 changed files
with
135 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,8 @@ | |
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"moment": "^2.22.2" | ||
} | ||
} |
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,69 @@ | ||
const moment = require('moment'); | ||
|
||
module.exports = function () { | ||
/** | ||
* Instantiate object used to store the methods registered as a | ||
* 'filter' (of the same name) within nunjucks. You can override | ||
* gov.uk core filters by creating filter methods of the same name. | ||
* @type {Object} | ||
*/ | ||
let filters = {} | ||
|
||
/* ------------------------------------------------------------------ | ||
date filter for use in Nunjucks | ||
example: {{ params.date | date("DD/MM/YYYY") }} | ||
outputs: 01/01/1970 | ||
------------------------------------------------------------------ */ | ||
filters.date = function(timestamp, format) { | ||
return moment(timestamp).format(format); | ||
} | ||
|
||
/* ------------------------------------------------------------------ | ||
utility functions for use in mojDate function/filter | ||
------------------------------------------------------------------ */ | ||
function govDate(timestamp) { | ||
return moment(timestamp).format('D MMMM YYYY'); | ||
} | ||
|
||
function govShortDate(timestamp) { | ||
return moment(timestamp).format('D MMM YYYY'); | ||
} | ||
|
||
function govTime(timestamp) { | ||
let t = moment(timestamp); | ||
if(t.minutes() > 0) { | ||
return t.format('h:mma'); | ||
} else { | ||
return t.format('ha'); | ||
} | ||
} | ||
|
||
/* ------------------------------------------------------------------ | ||
standard dates for use in Nunjucks, | ||
example: {{ params.date | mojDate("datetime") }} | ||
outputs: 1 Jan 1970 at 1:32pm | ||
------------------------------------------------------------------ */ | ||
filters.mojDate = function(timestamp, type) { | ||
|
||
switch(type) { | ||
case "datetime": | ||
return govDate(timestamp) + " at " + govTime(timestamp); | ||
case "shortdatetime": | ||
return govShortDate(timestamp) + " at " + govTime(timestamp); | ||
case "date": | ||
return govDate(timestamp); | ||
case "shortdate": | ||
return govShortDate(timestamp); | ||
case "time": | ||
return govTime(timestamp); | ||
default: | ||
return timestamp; | ||
} | ||
|
||
} | ||
|
||
/* ------------------------------------------------------------------ | ||
keep the following line to return your filters to the app | ||
------------------------------------------------------------------ */ | ||
return filters; | ||
} |