Meteor-handlebar-helpers icon indicating copy to clipboard operation
Meteor-handlebar-helpers copied to clipboard

Make $and and $or take infinite amount of parameters?

Open rclai opened this issue 10 years ago • 8 comments

UI.registerHelper('$and', function () {
    var args = _.toArray(arguments);
    if (!args.length) {
        return false;
    }
    var test = args[0];
    if (args.length > 1) {
        _.each(_.rest(args), function (arg) {
            test = test && arg;
        });
    }
    return test;
});
UI.registerHelper('$or', function () {
    var args = _.toArray(arguments);
    if (!args.length) {
        return false;
    }
    var test = args[0];
    if (args.length > 1) {
        _.each(_.rest(args), function (arg) {
            test = test || arg;
        });
    }
    return test;
});

Is this a good idea? Is it done right/efficiently?

rclai avatar Dec 12 '14 14:12 rclai

I went for two in binary helpers - but we could have this in the $in/$nin (they currently only support 4)

I normally either convert into array var args = _.toArray(arguments); or use a for loop since its possible to break from it. (I'm a bit old fashion when it comes to array like objects)

The reason for the limited args of two in binary operators is to reason about stuff like:

{{# if $and a $or b c }}

 At some point I think spacebars will support something like:
{{#if a && (b || c) }}

raix avatar Dec 12 '14 14:12 raix

Thinking about this some more - not sure how this actually work

raix avatar Dec 12 '14 14:12 raix

Spacebars now (Meteor 1.2) supports nested helpers. Not as succinct as @raix hoped it would be (no native javascript expressions/operators) but we can nest handlebars-helpers

{{#if $and a ($or b ($and c d) ) }}

@rclai do you think we should still enhance these helpers to accept and process arbitrary number of arguments?

This would still be confusing if used in a regular block helper cascade where helpers are evaluated from right to left.

To me, the current way it works with two arguments and the new nesting syntax is just fine for a blanket solution helper like this and anyone in need for something far mre precise could create a dedicated one.

serkandurusoy avatar Sep 23 '15 12:09 serkandurusoy

I'm so glad that we have nested expressions now.

I personally wouldn't go overboard in trying to list so many conditionals, but I'm sure someone has tried to but couldn't.

So I think it's still a good idea, especially since we now have nested expressions.

Just try it out, I already wrote the code in the first post.

rclai avatar Sep 23 '15 14:09 rclai

Thanks. Would you care to do a PR?

@raix what do you think?

serkandurusoy avatar Sep 23 '15 14:09 serkandurusoy

:+1: makes perfect sense (please use es6, api.versionsFrom('1.2'); and api.use('ecmascript');)

raix avatar Sep 24 '15 09:09 raix

Hi guys,

it's been over a year, but oh-yeah I am still on blaze and love this package here. Just stumbled upon the useCase where I need to check something like $and condition1 condition2 ($or condition3 condition4).

What is the state on this? Any change of getting this to work and release a new version?

thebarty avatar Feb 14 '17 08:02 thebarty

I'll take a pr and do a release if you are up to it :)

raix avatar Feb 16 '17 18:02 raix