11const prototypeFilters = require ( '@x-govuk/govuk-prototype-filters' ) ;
22
3- /**
4- * @param {Environment } env
5- */
6- module . exports = function ( env ) {
7- const filters = prototypeFilters ;
8-
9- /* ------------------------------------------------------------------
10- add your methods to the filters obj below this comment block:
11- @example :
12-
13- filters.sayHi = function(name) {
14- return 'Hi ' + name + '!'
15- }
16-
17- Which in your templates would be used as:
18-
19- {{ 'Paul' | sayHi }} => 'Hi Paul'
20-
21- Notice the first argument of your filters method is whatever
22- gets 'piped' via '|' to the filter.
23-
24- Filters can take additional arguments, for example:
25-
26- filters.sayHi = function(name,tone) {
27- return (tone == 'formal' ? 'Greetings' : 'Hi') + ' ' + name + '!'
28- }
29-
30- Which would be used like this:
31-
32- {{ 'Joel' | sayHi('formal') }} => 'Greetings Joel!'
33- {{ 'Gemma' | sayHi }} => 'Hi Gemma!'
343
35- For more on filters and how to write them see the Nunjucks
36- documentation.
37-
38- ------------------------------------------------------------------ */
4+ module . exports = function ( ) {
5+ const filters = prototypeFilters ;
396
407 /**
418 * Find an object by ID in an array
9+ *
4210 * @param {Array } array - Array to search
4311 * @param {string } id - ID to find
44- * @returns {Object } Found object or undefined
45- */
12+ * @returns {object } Found object or undefined
13+ */
4614 const findById = ( array , id ) => {
4715 if ( ! array || ! Array . isArray ( array ) ) return undefined
4816 return array . find ( item => item . id === id )
4917 }
50-
51- filters . findById = findById
5218
19+ filters . findById = findById
5320
5421 filters . dayName = function ( isoDate ) {
5522 const date = new Date ( Date . parse ( isoDate ) )
5623 const dateFormatter = new Intl . DateTimeFormat ( 'en-GB' , { weekday : 'short' } ) ;
57-
24+
5825 return dateFormatter . format ( date )
5926 }
60-
27+
6128 filters . pluck = function ( array , attribute ) {
6229 return array . map ( ( item ) => item [ attribute ] )
6330 }
64-
31+
6532 filters . capitaliseFirstLetter = function ( string ) {
6633 if ( string ) {
6734 return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 )
@@ -78,23 +45,23 @@ module.exports = function (env) {
7845 * January, rather than 0 (which is the default for
7946 * JavaScript date objects).
8047 *
81- * @param {Integer, string } monthNumber - number of the month
82- * @returns {String } Full name of the month in English
83- */
48+ * @param {number| string } monthNumber - number of the month
49+ * @returns {string } Full name of the month in English
50+ */
8451 filters . monthName = function ( monthNumber ) {
85-
52+
8653 try {
8754 monthNumber = parseInt ( monthNumber )
88-
55+
8956 if ( ! monthNumber || ( monthNumber < 1 ) || ( monthNumber > 12 ) ) {
9057 throw new Error ( 'Invalid monthNumber - must be between 1 and 12' )
9158 }
92-
59+
9360 const date = new Date ( Date . UTC ( 2000 , ( monthNumber - 1 ) , 1 , 0 , 0 , 0 ) ) ;
9461 const dateFormatter = new Intl . DateTimeFormat ( 'en-GB' , { month : 'long' } ) ;
95-
62+
9663 return dateFormatter . format ( date )
97-
64+
9865 } catch ( error ) {
9966 return error . message . split ( ':' ) [ 0 ]
10067 }
0 commit comments