diff --git a/README.md b/README.md index e6e39e3..7acd221 100644 --- a/README.md +++ b/README.md @@ -365,7 +365,7 @@ overwrite default handlebars 'if' helper
this adds support for an inline he ### ifAll ( [code](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifAll.js) | [tests](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifAll.test.js) ) -block helper for checking if ALL arguments passed in are truthy +helper for checking if ALL arguments passed in are truthy
_note:_ this can be used as a block _or_ inline helper @@ -384,7 +384,7 @@ block helper for checking if ALL arguments passed in are truthy ### ifAny ( [code](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifAny.js) | [tests](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifAny.test.js) ) -block helper for checking if ANY arguments passed in are truthy +helper for checking if ANY arguments passed in are truthy
_note:_ this can be used as a block _or_ inline helper @@ -403,7 +403,7 @@ block helper for checking if ANY arguments passed in are truthy ### ifNone ( [code](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifNone.js) | [tests](https://github.com/clay/handlebars/blob/master/helpers/conditionals/ifNone.test.js) ) -block helper for checking if NO arguments passed in are truthy +helper for checking if NO arguments passed in are truthy
_note:_ this can be used as a block _or_ inline helper @@ -441,7 +441,7 @@ compare the modulo of two values to a third value ### unlessAll ( [code](https://github.com/clay/handlebars/blob/master/helpers/conditionals/unlessAll.js) | [tests](https://github.com/clay/handlebars/blob/master/helpers/conditionals/unlessAll.test.js) ) -block helper for checking that NOT ALL arguments passed in are truthy
_note:_ this is the inverse of the ifAll helper +helper for checking that NOT ALL arguments passed in are truthy
_note:_ this can be used as a block _or_ inline helper
_note:_ this is the inverse of the ifAll helper diff --git a/helpers/conditionals/ifAll.js b/helpers/conditionals/ifAll.js index 9b9a83f..7bd9c09 100644 --- a/helpers/conditionals/ifAll.js +++ b/helpers/conditionals/ifAll.js @@ -4,7 +4,8 @@ const _initial = require('lodash/initial'), _takeWhile = require('lodash/takeWhile'); /** - * block helper for checking if ALL arguments passed in are truthy + * helper for checking if ALL arguments passed in are truthy + * _note:_ this can be used as a block _or_ inline helper * @return {string} calls block functions */ module.exports = function () { @@ -12,13 +13,12 @@ module.exports = function () { options = _last(arguments), taken = _takeWhile(conditionals, c => !!c === true); - // see if any of the conditionals are falsy without needing to - // iterate through all of them - + // see if any of the conditionals are falsy without needing to + // iterate through all of them if (taken.length === conditionals.length) { - return options.fn(this); + return options.fn ? options.fn(this) : true; } else { - return options.inverse(this); + return options.inverse ? options.inverse(this) : false; } }; @@ -28,4 +28,4 @@ module.exports.example = { {{else}} not all are truthy {{/ifAll}}` -}; +}; \ No newline at end of file diff --git a/helpers/conditionals/ifAll.test.js b/helpers/conditionals/ifAll.test.js index 7894d75..55c05e5 100644 --- a/helpers/conditionals/ifAll.test.js +++ b/helpers/conditionals/ifAll.test.js @@ -1,6 +1,7 @@ 'use strict'; const name = getName(__filename), - tpl = hbs.compile('{{#ifAll a b}}yes{{else}}no{{/ifAll}}'); + tpl = hbs.compile('{{#ifAll a b}}yes{{else}}no{{/ifAll}}'), + inlineTpl = hbs.compile('{{ifAll a b}}'); describe(name, function () { it('is true if all conditionals are truthy', function () { @@ -14,4 +15,16 @@ describe(name, function () { expect(tpl({a: 1, b: 0})).to.equal('no'); expect(tpl({a: undefined, b: true})).to.equal('no'); }); -}); + + it('is true if all inline conditionals are truthy', function () { + expect(inlineTpl({a: true, b: true})).to.equal('true'); + expect(inlineTpl({a: 'abc', b: 123})).to.equal('true'); + }); + + it('is false if any inline conditionals are falsy', function () { + expect(inlineTpl({a: true, b: false})).to.equal('false'); + expect(inlineTpl({a: 'true', b: ''})).to.equal('false'); + expect(inlineTpl({a: 1, b: 0})).to.equal('false'); + expect(inlineTpl({a: undefined, b: true})).to.equal('false'); + }); +}); \ No newline at end of file diff --git a/helpers/conditionals/ifAny.js b/helpers/conditionals/ifAny.js index c2f3cc7..9e379f1 100644 --- a/helpers/conditionals/ifAny.js +++ b/helpers/conditionals/ifAny.js @@ -4,7 +4,8 @@ const _initial = require('lodash/initial'), _find = require('lodash/find'); /** - * block helper for checking if ANY arguments passed in are truthy + * helper for checking if ANY arguments passed in are truthy + * _note:_ this can be used as a block _or_ inline helper * @return {string} calls block functions */ module.exports = function () { @@ -18,9 +19,9 @@ module.exports = function () { if (truthyFound) { // at least one of the conditionals is truthy - return options.fn(this); + return options.fn ? options.fn(this) : true; } else { - return options.inverse(this); + return options.inverse ? options.inverse(this) : false; } }; @@ -30,4 +31,4 @@ module.exports.example = { {{else}} none are truthy {{/ifAny}}` -}; +}; \ No newline at end of file diff --git a/helpers/conditionals/ifAny.test.js b/helpers/conditionals/ifAny.test.js index 080bd38..af26eea 100644 --- a/helpers/conditionals/ifAny.test.js +++ b/helpers/conditionals/ifAny.test.js @@ -1,6 +1,7 @@ 'use strict'; const name = getName(__filename), - tpl = hbs.compile('{{#ifAny a b}}yes{{else}}no{{/ifAny}}'); + tpl = hbs.compile('{{#ifAny a b}}yes{{else}}no{{/ifAny}}'), + inlineTpl = hbs.compile('{{ifAny a b}}'); describe(name, function () { it('is true if any conditionals are truthy', function () { @@ -15,4 +16,17 @@ describe(name, function () { expect(tpl({a: 0, b: 0})).to.equal('no'); expect(tpl({a: undefined, b: null})).to.equal('no'); }); -}); + + it('is true if any inline conditionals are truthy', function () { + expect(inlineTpl({a: true, b: true})).to.equal('true'); + expect(inlineTpl({a: 'abc', b: 0})).to.equal('true'); + expect(inlineTpl({a: undefined, b: 1})).to.equal('true'); + }); + + it('is false if all inline conditionals are falsy', function () { + expect(inlineTpl({a: false, b: false})).to.equal('false'); + expect(inlineTpl({a: '', b: ''})).to.equal('false'); + expect(inlineTpl({a: 0, b: 0})).to.equal('false'); + expect(inlineTpl({a: undefined, b: null})).to.equal('false'); + }); +}); \ No newline at end of file diff --git a/helpers/conditionals/ifNone.js b/helpers/conditionals/ifNone.js index d6323ea..818b0b7 100644 --- a/helpers/conditionals/ifNone.js +++ b/helpers/conditionals/ifNone.js @@ -4,7 +4,8 @@ const _initial = require('lodash/initial'), _find = require('lodash/find'); /** - * block helper for checking if NO arguments passed in are truthy + * helper for checking if NO arguments passed in are truthy + * _note:_ this can be used as a block _or_ inline helper * @return {string} calls block functions */ module.exports = function () { @@ -19,9 +20,9 @@ module.exports = function () { if (truthyFound !== undefined) { // at least one of the conditionals is truthy // so _find returns quickly without iterating over all of them - return options.inverse(this); + return options.inverse ? options.inverse(this) : false; } else { - return options.fn(this); + return options.fn ? options.fn(this) : true; } }; @@ -31,4 +32,4 @@ module.exports.example = { {{else}} not all are falsy {{/ifNone}}` -}; +}; \ No newline at end of file diff --git a/helpers/conditionals/ifNone.test.js b/helpers/conditionals/ifNone.test.js index e1f5687..c378355 100644 --- a/helpers/conditionals/ifNone.test.js +++ b/helpers/conditionals/ifNone.test.js @@ -1,6 +1,7 @@ 'use strict'; const name = getName(__filename), - tpl = hbs.compile('{{#ifNone a b}}yes{{else}}no{{/ifNone}}'); + tpl = hbs.compile('{{#ifNone a b}}yes{{else}}no{{/ifNone}}'), + inlineTpl = hbs.compile('{{ifNone a b}}'); describe(name, function () { it('is true if all conditionals are falsy', function () { @@ -16,4 +17,18 @@ describe(name, function () { expect(tpl({a: 1, b: 0})).to.equal('no'); expect(tpl({a: undefined, b: 1})).to.equal('no'); }); -}); + + it('is true if all inline conditionals are falsy', function () { + expect(inlineTpl({a: false, b: false})).to.equal('true'); + expect(inlineTpl({a: '', b: 0})).to.equal('true'); + expect(inlineTpl({a: undefined, b: undefined})).to.equal('true'); + expect(inlineTpl({a: undefined, b: null})).to.equal('true'); + }); + + it('is false if any inline conditionals are truthy', function () { + expect(inlineTpl({a: true, b: false})).to.equal('false'); + expect(inlineTpl({a: 'true', b: ''})).to.equal('false'); + expect(inlineTpl({a: 1, b: 0})).to.equal('false'); + expect(inlineTpl({a: undefined, b: 1})).to.equal('false'); + }); +}); \ No newline at end of file diff --git a/helpers/conditionals/unlessAll.js b/helpers/conditionals/unlessAll.js index adb6af4..475e15e 100644 --- a/helpers/conditionals/unlessAll.js +++ b/helpers/conditionals/unlessAll.js @@ -4,7 +4,8 @@ const _initial = require('lodash/initial'), _takeWhile = require('lodash/takeWhile'); /** - * block helper for checking that NOT ALL arguments passed in are truthy + * helper for checking that NOT ALL arguments passed in are truthy + * _note:_ this can be used as a block _or_ inline helper * _note:_ this is the inverse of the ifAll helper * @return {string} calls block functions */ @@ -13,13 +14,12 @@ module.exports = function () { options = _last(arguments), taken = _takeWhile(conditionals, c => !!c === true); - // see if any of the conditionals are falsy without needing to - // iterate through all of them - + // see if any of the conditionals are falsy without needing to + // iterate through all of them if (taken.length === conditionals.length) { - return options.inverse(this); + return options.inverse ? options.inverse(this) : false; } else { - return options.fn(this); + return options.fn ? options.fn(this) : true; } }; @@ -29,4 +29,4 @@ module.exports.example = { {{else}} all are truthy {{/ifAll}}` -}; +}; \ No newline at end of file diff --git a/helpers/conditionals/unlessAll.test.js b/helpers/conditionals/unlessAll.test.js index 99b4bd1..40daae3 100644 --- a/helpers/conditionals/unlessAll.test.js +++ b/helpers/conditionals/unlessAll.test.js @@ -1,6 +1,7 @@ 'use strict'; const name = getName(__filename), - tpl = hbs.compile('{{#unlessAll a b}}yes{{else}}no{{/unlessAll}}'); + tpl = hbs.compile('{{#unlessAll a b}}yes{{else}}no{{/unlessAll}}'), + inlineTpl = hbs.compile('{{unlessAll a b}}'); describe(name, function () { it('is false if all conditionals are truthy', function () { @@ -14,4 +15,16 @@ describe(name, function () { expect(tpl({a: 1, b: 0})).to.equal('yes'); expect(tpl({a: undefined, b: true})).to.equal('yes'); }); -}); + + it('is false if all inline conditionals are truthy', function () { + expect(inlineTpl({a: true, b: true})).to.equal('false'); + expect(inlineTpl({a: 'abc', b: 123})).to.equal('false'); + }); + + it('is true if any inline conditionals are falsy', function () { + expect(inlineTpl({a: true, b: false})).to.equal('true'); + expect(inlineTpl({a: 'true', b: ''})).to.equal('true'); + expect(inlineTpl({a: 1, b: 0})).to.equal('true'); + expect(inlineTpl({a: undefined, b: true})).to.equal('true'); + }); +}); \ No newline at end of file