|
| 1 | + |
| 2 | +const prototypeFilters = require('@x-govuk/govuk-prototype-filters'); |
| 3 | + |
| 4 | +module.exports = function (env) { /* eslint-disable-line no-unused-vars */ |
| 5 | + /** |
| 6 | + * Instantiate object used to store the methods registered as a |
| 7 | + * 'filter' (of the same name) within nunjucks. You can override |
| 8 | + * gov.uk core filters by creating filter methods of the same name. |
| 9 | + * @type {Object} |
| 10 | + */ |
| 11 | + const filters = prototypeFilters; |
| 12 | + |
| 13 | + |
| 14 | + filters.dayName = function(isoDate) { |
| 15 | + |
| 16 | + const date = new Date(Date.parse(isoDate)) |
| 17 | + const dateFormatter = new Intl.DateTimeFormat('en-GB', {weekday: 'short'}); |
| 18 | + |
| 19 | + return dateFormatter.format(date) |
| 20 | + } |
| 21 | + |
| 22 | + /* ------------------------------------------------------------------ |
| 23 | + add your methods to the filters obj below this comment block: |
| 24 | + @example: |
| 25 | +
|
| 26 | + filters.sayHi = function(name) { |
| 27 | + return 'Hi ' + name + '!' |
| 28 | + } |
| 29 | +
|
| 30 | + Which in your templates would be used as: |
| 31 | +
|
| 32 | + {{ 'Paul' | sayHi }} => 'Hi Paul' |
| 33 | +
|
| 34 | + Notice the first argument of your filters method is whatever |
| 35 | + gets 'piped' via '|' to the filter. |
| 36 | +
|
| 37 | + Filters can take additional arguments, for example: |
| 38 | +
|
| 39 | + filters.sayHi = function(name,tone) { |
| 40 | + return (tone == 'formal' ? 'Greetings' : 'Hi') + ' ' + name + '!' |
| 41 | + } |
| 42 | +
|
| 43 | + Which would be used like this: |
| 44 | +
|
| 45 | + {{ 'Joel' | sayHi('formal') }} => 'Greetings Joel!' |
| 46 | + {{ 'Gemma' | sayHi }} => 'Hi Gemma!' |
| 47 | +
|
| 48 | + For more on filters and how to write them see the Nunjucks |
| 49 | + documentation. |
| 50 | +
|
| 51 | + ------------------------------------------------------------------ */ |
| 52 | + |
| 53 | + /* ------------------------------------------------------------------ |
| 54 | + keep the following line to return your filters to the app |
| 55 | + ------------------------------------------------------------------ */ |
| 56 | + return filters; |
| 57 | +}; |
0 commit comments