From 4ecbed05f51ec323d2649372133360ff47dd44ac Mon Sep 17 00:00:00 2001 From: Donald Disha Date: Tue, 6 Oct 2020 08:18:31 -0400 Subject: [PATCH] Fix for nested array that needs context (#423) * test for nested if inside of else failing * Looping an array within an array * Looping an array within an array * remove comment Co-authored-by: Donald Disha --- looping.js | 58 ++++++++++++------- test/conditionals.js | 5 ++ test/looping.js | 3 +- test/templates/looping/loopArrayOfArrays.html | 1 - 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/looping.js b/looping.js index a054bef9..1c56da2a 100644 --- a/looping.js +++ b/looping.js @@ -94,7 +94,15 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren // if (periodIndex < 0) { // Loop through value is not an object - through = model[params.through] + if (model[params.through]) { // Loop through non-object value requires context + through = model[params.through] + } else { // Loop through non-object value does not require context + const contextResult = findContext(params.through, { contextModels, currentContext }) + context = contextResult.context + contextModels = contextResult.contextModels + currentContext = contextResult.currentContext + through = getContext(model, context, params.through) + } } else { // Loop through value is an object if (model[params.through.slice(0, periodIndex)] === undefined) { // Loop through object value requires context const contextResult = findContext(params.through, { contextModels, currentContext }) @@ -236,31 +244,39 @@ function getContext (model, str, thru) { let tempIndex = '' let getIndex = false - for (let i = 0; i < str.length; i++) { // Found index - if (getIndex) { // Get numerical index to select from - if (str[i] === ']') { - currentValue = currentValue[tempIndex] - getIndex = false - tempIndex = '' - } else { - tempIndex += str[i] - } - } else if (str[i] === '[') { // Found index - if (currentValue) { - currentValue = currentValue[tempStr] // Fetch value from current spot in nested object - } else { - currentValue = model[tempStr] // Fetch value from model + if (str) { // Looking for context + for (let i = 0; i < str.length; i++) { // Found index + if (getIndex) { // Get numerical index to select from + if (str[i] === ']') { + currentValue = currentValue[tempIndex] + getIndex = false + tempIndex = '' + } else { + tempIndex += str[i] + } + } else if (str[i] === '[') { // Found index + if (currentValue) { + currentValue = currentValue[tempStr] // Fetch value from current spot in nested object + } else { + currentValue = model[tempStr] // Fetch value from model + } + getIndex = true + tempStr = '' + } else if (str[i] === '.') { // Do nothing + } else { // Get attribute name from model + tempStr += str[i] } - getIndex = true - tempStr = '' - } else if (str[i] === '.') { // Do nothing - } else { // Get attribute name from model - tempStr += str[i] } + } else { // Return undefined in case there is no context (should ignore loop with invalid through attribute) + return undefined } // Contains contextual value to loop through - return currentValue[thru] + if (currentValue[thru]) { + return currentValue[thru] + } else { // Contextual value came from an array of arrays + return currentValue + } } module.exports = { parseLoop } diff --git a/test/conditionals.js b/test/conditionals.js index ea279a03..0dd7469b 100644 --- a/test/conditionals.js +++ b/test/conditionals.js @@ -250,6 +250,11 @@ describe('Conditionals', function () { done() }) + it('should parse nested conditionals correctly (conditionals/nestedConditionalInElse.html)', function (done) { + assert.equalIgnoreSpaces(teddy.render('conditionals/nestedConditionalInElse.html', model), '

The variable \'something\' and \'somethingElse\' are both present

') + done() + }) + it('should render nothing if condition isn\'t met (conditionals/ifNotPresent.html)', function (done) { assert.equalIgnoreSpaces(teddy.render('conditionals/ifNotPresent.html', model), '
') done() diff --git a/test/looping.js b/test/looping.js index 7bb780e4..1da9ffba 100644 --- a/test/looping.js +++ b/test/looping.js @@ -36,8 +36,7 @@ describe('Looping', function () { done() }) - // TODO: https://github.com/rooseveltframework/teddy/issues/404 - it.skip('should loop through {arrays} correctly (looping/loopArrayOfArrays.html)', function (done) { + it('should loop through {arrays} correctly (looping/loopArrayOfArrays.html)', function (done) { assert.equalIgnoreSpaces(teddy.render('looping/loopArrayOfArrays.html', model), '

0

a

b

c

1

d

e

f

2

g

h

i

') done() }) diff --git a/test/templates/looping/loopArrayOfArrays.html b/test/templates/looping/loopArrayOfArrays.html index 79e43309..57735b33 100644 --- a/test/templates/looping/loopArrayOfArrays.html +++ b/test/templates/looping/loopArrayOfArrays.html @@ -4,7 +4,6 @@

{i}

-

{item}

{member}