diff --git a/lib/filters/core-filters.js b/lib/filters/core-filters.js index f96df7720c..673121a311 100644 --- a/lib/filters/core-filters.js +++ b/lib/filters/core-filters.js @@ -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('')) + + /** + * 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 })) : []) })