Skip to content

Commit

Permalink
Fix for nested array that needs context (rooseveltframework#423)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
dishad and Donald Disha authored Oct 6, 2020
1 parent 2631a33 commit 4ecbed0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
58 changes: 37 additions & 21 deletions looping.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren

// <loop through='list' key='index' val='value'>
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 })
Expand Down Expand Up @@ -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 }
5 changes: 5 additions & 0 deletions test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), '<p>The variable \'something\' and \'somethingElse\' are both present</p>')
done()
})

it('should render nothing if condition isn\'t met (conditionals/ifNotPresent.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/ifNotPresent.html', model), '<div></div>')
done()
Expand Down
3 changes: 1 addition & 2 deletions test/looping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), '<p>0</p><p>a</p><p>b</p><p>c</p><p>1</p><p>d</p><p>e</p><p>f</p><p>2</p><p>g</p><p>h</p><p>i</p>')
done()
})
Expand Down
1 change: 0 additions & 1 deletion test/templates/looping/loopArrayOfArrays.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

<loop through='arrays' key='i' val='item'>
<p>{i}</p>
<p>{item}</p>
<loop through='item' val='member'>
<p>{member}</p>
</loop>
Expand Down

0 comments on commit 4ecbed0

Please sign in to comment.