From 603fcdb7e16ede820d070a37a0a34eafeeeb491d Mon Sep 17 00:00:00 2001 From: Benjamin Lannon Date: Wed, 16 Feb 2022 11:34:36 -0500 Subject: [PATCH] Modifying one-line-ifs to also check for boolean values and presence properly Renabled failing test, and reworked wording within readme to match expected logic. Co-authored-by: Alexander J. Lallier --- README.md | 10 +++++----- conditionals.js | 5 ++++- test/conditionals.js | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c3e0e924..c2f3d8a9 100644 --- a/README.md +++ b/README.md @@ -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 -

One line if.

+

One line if.

``` -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 -

One line if.

+

One line if.

``` -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 -

One line if.

+ ``` Like the `` 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: diff --git a/conditionals.js b/conditionals.js index ecbda54d..98580c1c 100644 --- a/conditionals.js +++ b/conditionals.js @@ -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) diff --git a/test/conditionals.js b/test/conditionals.js index b3787d99..5dcd115c 100644 --- a/test/conditionals.js +++ b/test/conditionals.js @@ -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), '

') done() })