Skip to content

Commit

Permalink
Merge pull request #540 from lannonbr/fixing-one-line-if-false-condition
Browse files Browse the repository at this point in the history
Fixing one-line-ifs expected logic
  • Loading branch information
kethinov committed Mar 1, 2022
2 parents 6cc61c7 + 603fcdb commit 9de26a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,19 @@ One line ifs
If you need a more concise conditional to control which attributes are applied to a given element, then use this syntax:

```html
<p if-something true="class='present'" false="class='not-present'">One line if.</p>
<p if-something true="class='shown'" false="class='hidden'">One line if.</p>
```

In that structure, the attribute `if-something` checks to see if the variable `something` is present. If so, the class delcared in the `true` attribute is written to the element, resulting in the following output:
In that structure, the attribute `if-something` checks to see if the variable `something` is truthy. This means it will check for either variable presence in the model or the boolean value `true`. If so, the class delcared in the `true` attribute is written to the element, resulting in the following output:

```html
<p class='present'>One line if.</p>
<p class='shown'>One line if.</p>
```

If not, the class declared in the `false` attribute is written to the element, resulting in the following output:
If not, this will check for non-presence in the model or the boolean value `false`. If so, the class declared in the `false` attribute is written to the element, resulting in the following output:

```html
<p class='not-present'>One line if.</p>
<p class='hidden'>One line if.</p>
```

Like the `<if>` tag you can check for both the presence of a variable as well as its value. To check the value of a variable, use this syntax:
Expand Down
5 changes: 4 additions & 1 deletion conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ function parseOneLineIf (charList, model) {
return insertValue(charList, condition.false.split('').reverse().join(''), startIndex, endIndex)
}
} else { // There is no value to compare against
if (varVal && varVal[0] === '{' && varVal[varVal.length - 1] === '}') {
// Cases for when there isn't a condition.varLiteral value
// case 1: `false` literal or variable not present in model (resolving to var name within brackets, ex: {notInModel}) -> insert false condition
// case 2: non-empty string present in model or `true` literal -> insert true condition
if (varVal === false || (varVal[0] === '{' && varVal[varVal.length - 1] === '}')) {
return insertValue(charList, condition.false.split('').reverse().join(''), startIndex, endIndex)
} else {
return insertValue(charList, condition.true.split('').reverse().join(''), startIndex, endIndex)
Expand Down
2 changes: 1 addition & 1 deletion test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('Conditionals', function () {
done()
})

it.skip('should evaluate one line if "if-somethingFalse" as false (conditionals/oneLineIfBooleanValue.html)', function (done) {
it('should evaluate one line if "if-somethingFalse" as false (conditionals/oneLineIfBooleanValue.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineIfBooleanValue.html', model), '<p></p>')
done()
})
Expand Down

0 comments on commit 9de26a7

Please sign in to comment.