@ministryofjustice/fb-nunjucks-helpers
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -40,2 +40,3 @@ const Markdown = require('markdown-it')() | ||
const setItemsLabel = data => { | ||
data = JSON.parse(JSON.stringify(data)) | ||
try { | ||
@@ -67,5 +68,18 @@ data = setLabel(data) | ||
data.items = data.items || [] | ||
data.items = data.items.map(item => { | ||
data.items = data.items.map((item, index) => { | ||
let hint = item.hint | ||
if (hint && !hint.html) { | ||
hint = {html: hint} | ||
} | ||
let itemId = item.id || item.name | ||
if (!itemId && data.name) { | ||
itemId = `${data.name}-${index}` | ||
} | ||
let html = item.text ? item.text : item.html | ||
html = html || item.label | ||
const id = itemId ? itemId.replace(/\./g, '--') : undefined | ||
const updatedItem = Object.assign({}, item, { | ||
text: item.text ? item.text : item.label | ||
id, | ||
html, | ||
hint | ||
}) | ||
@@ -72,0 +86,0 @@ if (data._type !== 'date') { |
@@ -129,2 +129,25 @@ const test = require('tape') | ||
const itemsData = setItemsLabel({ | ||
name: 'name', | ||
items: [{ | ||
text: 'item1text', | ||
id: 'item1id' | ||
}, { | ||
html: 'item2html', | ||
hint: 'hint2html' | ||
}, { | ||
label: 'item3label', | ||
name: 'foo.bar' | ||
}] | ||
}) | ||
t.equals(itemsData.items[0].html, 'item1text', 'it should set the item.html property with the value of any text property passed') | ||
t.equals(itemsData.items[1].html, 'item2html', 'it should set the item.html property with the value of any html property passed') | ||
t.equals(itemsData.items[2].html, 'item3label', 'it should set the item.html property with the value of any html property passed') | ||
t.equals(itemsData.items[1].hint.html, 'hint2html', 'it should set the item.html property with the value of any html property passed') | ||
t.equals(itemsData.items[0].id, 'item1id', 'it should use the item.id property if passed explicitly') | ||
t.equals(itemsData.items[1].id, 'name-1', 'it should set the item.id property with the value of any name property passed on the parent data') | ||
t.equals(itemsData.items[2].id, 'foo--bar', 'it should set the item.id property with the value of its name property, substituting dots with double dashes') | ||
t.end() | ||
@@ -131,0 +154,0 @@ }) |
{ | ||
"name": "@ministryofjustice/fb-nunjucks-helpers", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Form Builder Nunjucks helpers", | ||
@@ -5,0 +5,0 @@ "main": "lib/fb-nunjucks-helpers.js", |
27716
637