Skip to content

ifNth arguments seem to be switched #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
evenfrost opened this issue Jan 31, 2018 · 2 comments
Open

ifNth arguments seem to be switched #306

evenfrost opened this issue Jan 31, 2018 · 2 comments

Comments

@evenfrost
Copy link

var isNth = utils.isNumber(a) && utils.isNumber(b) && b % a === 0;

Is there any idea behind switching arguments (b % a) here? Documentation says that

Conditionally renders a block if the remainder is zero when a operand is divided by b.

but logic seems to be inverse. I spent plenty of time trying to figure out why my ifNth is not working as supposed, or I just don't get it.

@evenfrost evenfrost changed the title ifNth confusion ifNth arguments seem to be switched Jan 31, 2018
@doowb
Copy link
Member

doowb commented Jan 31, 2018

The following test shows an example of the usage:

it('should render a custom class on even rows', function() {
var source = '{{#each items}}<div{{#ifNth 2 @index}}{{else}} class="row-alternate"{{/ifNth}}>{{name}}</div>{{/each}}';
var fn = hbs.compile(source);
var context = {
items: [
{ name: 'Philip J. Fry' },
{ name: 'Turanga Leela' },
{ name: 'Bender Bending Rodriguez' },
{ name: 'Amy Wong' },
{ name: 'Hermes Conrad' }
]
};
assert(fn(context), [
'<div>Philip J. Fry</div>',
'<div class="row-alternate">Turanga Leela</div>',
'<div>Bender Bending Rodriguez</div>',
'<div class="row-alternate">Amy Wong</div>',
'<div>Hermes Conrad</div>'
].join(''));
});
});

I agree that the documentation could be updated to show an example and clear up which is argument is dividing the other. Would you like to do a PR to update the code comments? I think the logic is correct because the usage in the test "feels" more natural to me.

@evenfrost
Copy link
Author

Sure, landed here: #307.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants