Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ overwrite default handlebars 'if' helper<br />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<br /> _note:_ this can be used as a block _or_ inline helper



Expand All @@ -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<br /> _note:_ this can be used as a block _or_ inline helper



Expand All @@ -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<br /> _note:_ this can be used as a block _or_ inline helper



Expand Down Expand Up @@ -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<br /> _note:_ this is the inverse of the ifAll helper
helper for checking that NOT ALL arguments passed in are truthy<br /> _note:_ this can be used as a block _or_ inline helper<br /> _note:_ this is the inverse of the ifAll helper



Expand Down
14 changes: 7 additions & 7 deletions helpers/conditionals/ifAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ 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 () {
const conditionals = _initial(arguments),
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;
}
};

Expand All @@ -28,4 +28,4 @@ module.exports.example = {
{{else}}
not all are truthy
{{/ifAll}}`
};
};
17 changes: 15 additions & 2 deletions helpers/conditionals/ifAll.test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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');
});
});
9 changes: 5 additions & 4 deletions helpers/conditionals/ifAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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;
}
};

Expand All @@ -30,4 +31,4 @@ module.exports.example = {
{{else}}
none are truthy
{{/ifAny}}`
};
};
18 changes: 16 additions & 2 deletions helpers/conditionals/ifAny.test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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');
});
});
9 changes: 5 additions & 4 deletions helpers/conditionals/ifNone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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;
}
};

Expand All @@ -31,4 +32,4 @@ module.exports.example = {
{{else}}
not all are falsy
{{/ifNone}}`
};
};
19 changes: 17 additions & 2 deletions helpers/conditionals/ifNone.test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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');
});
});
14 changes: 7 additions & 7 deletions helpers/conditionals/unlessAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
};

Expand All @@ -29,4 +29,4 @@ module.exports.example = {
{{else}}
all are truthy
{{/ifAll}}`
};
};
17 changes: 15 additions & 2 deletions helpers/conditionals/unlessAll.test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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');
});
});