Silently ignore non-iterables in {#each}

This commit is contained in:
David Bruant 2025-04-17 19:36:45 +02:00
parent ecfedf9317
commit ad8e60d474
2 changed files with 35 additions and 8 deletions

View File

@ -30,7 +30,7 @@ const ODTMimetype = 'application/vnd.oasis.opendocument.text'
* @param {any} context - data / global object
* @return {any}
*/
function evaludateTemplateExpression(expression, context){
function evaluateTemplateExpression(expression, context){
const parts = expression.trim().split('.')
let value = context;
@ -76,7 +76,7 @@ function findPlacesToFillInString(str) {
parts.push(fixedPart)
parts.push(data => evaludateTemplateExpression(expression, data))
parts.push(data => evaluateTemplateExpression(expression, data))
remaining = newRemaining
}
@ -189,12 +189,10 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, d
// Find the iterable in the data
// PPP eventually, evaluate the expression as a JS expression
const iterable = evaludateTemplateExpression(iterableExpression, data)
if(!iterable){
throw new TypeError(`Missing iterable (${iterableExpression})`)
}
if(typeof iterable[Symbol.iterator] !== 'function'){
throw new TypeError(`'${iterableExpression}' is not iterable`)
let iterable = evaluateTemplateExpression(iterableExpression, data)
if(!iterable || typeof iterable[Symbol.iterator] !== 'function'){
// when there is no iterable, silently replace with empty array
iterable = []
}
// create each loop result

View File

@ -71,6 +71,35 @@ Pâtes à lasagne (fraîches !)
`)
});
test('Filling with {#each} and non-iterable value results in no error and empty result', async t => {
const templatePath = join(import.meta.dirname, './data/enum-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺
{#each listeCourses as élément}
{élément}
{/each}
`
const data = {
listeCourses : undefined
}
const odtTemplate = await getOdtTemplate(templatePath)
const templateTextContent = await getOdtTextContent(odtTemplate)
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
const odtResult = await fillOdtTemplate(odtTemplate, data)
const odtResultTextContent = await getOdtTextContent(odtResult)
t.deepEqual(odtResultTextContent, `🧺 La liste de courses incroyable 🧺
`)
});