Skip to content

Commit

Permalink
Allow users to simplify entering items when the text and value are th…
Browse files Browse the repository at this point in the history
…e same.
  • Loading branch information
BenSurgisonGDS committed Nov 29, 2023
1 parent 667abeb commit b667f7d
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/filters/core-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@
const { runWhenEnvIsAvailable, external } = require('./api')
const { addFilter, getFilter } = external

/**
* Logs an object in the template to the console in the browser.
* @param {Any} a any type
* @return {String} a script tag with a console.log call.
* @example {{ "hello world" | log }}
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
*/
runWhenEnvIsAvailable(() => {
const nunjucksSafe = getFilter('safe')

/**
* Logs an object in the template to the console in the browser.
* @param {Any} a any type
* @return {String} a script tag with a console.log call.
* @example {{ "hello world" | log }}
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
*/
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))

/**
* Returns an array of objects for use in a macro that requires a list of items
* @param {String[]} an array of strings.
* @return {Object[]} an array of objects with each object containing text and value properties.
* @example {{ govukCheckboxes({
* name: "waste",
* fieldset: {
* legend: {
* text: "Which types of waste do you transport?",
* isPageHeading: true,
* classes: "govuk-fieldset__legend--l"
* }
* },
* hint: {
* text: "Select all that apply."
* },
* items: ['Rubble', 'Oil', 'Card'] | formatItems
* }) }}
*/
addFilter('formatItems', (items = []) => Array.isArray(items) ? items.map(item => ({ text: item, value: item })) : [])
})

0 comments on commit b667f7d

Please sign in to comment.