cloudfour / core-hbs-helpers

Handlebars helpers that we usually need
MIT License
11 stars 2 forks source link

Consider replacing "and" and "or" helpers with "all" and "any" #53

Closed tylersticka closed 5 years ago

tylersticka commented 5 years ago

We currently have and and or helpers, which support two arguments.

It would be nice if instead we had helpers that resolved to true if all or any arguments resolve to true. This would work for two or more arguments.

Example helpers from a personal project:

var R = require('ramda');

function all () {
  var options = R.last(arguments);
  var result = true;
  var values;
  var i;

  if (R.isNil(options.fn)) {
    options = undefined;
    values = arguments;
  } else {
    values = R.dropLast(1, arguments);
  }

  for (i = 0; i < values.length; i++) {
    if (!values[i]) {
      result = false;
      break;
    }
  }

  if (options) {
    return result ? options.fn(this) : options.inverse(this);
  }

  return result;
};

function any () {
  var options = R.last(arguments);
  var values, result;

  if (R.isNil(options.fn)) {
    options = undefined;
    values = arguments;
  } else {
    values = R.dropLast(1, arguments);
  }

  result = R.any(R.__, values);

  if (options) {
    return result ? options.fn(this) : options.inverse(this);
  }

  return result;
};