FixedArityExpressionT.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. /**
  3. * A factory and base class for expressions that take a fixed number of arguments
  4. * @class FixedArityExpressionT
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. module.exports = function FixedArityExpressionT(SubClass, nArgs) {
  10. if (arguments.length !== 2) throw new Error(klass.name + "<" + SubClass.name + ">: expected args: SubClass, nArgs");
  11. var FixedArityExpression = function FixedArityExpression() {
  12. if (arguments.length !== 0) throw new Error(klass.name + "<" + SubClass.name + ">: zero args expected");
  13. base.call(this);
  14. }, klass = FixedArityExpression, base = require("./NaryBaseExpressionT")(SubClass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}}); //jshint ignore:line
  15. //NOTE: attach statics to emulate the C++ behavior
  16. for (var propName in base) //jshint ignore:line
  17. klass[propName] = base[propName];
  18. /**
  19. * Check that the number of args is what we expected
  20. * @method validateArguments
  21. * @param args Array The array of arguments to the expression
  22. * @throws
  23. */
  24. proto.validateArguments = function validateArguments(args) {
  25. if(args.length !== nArgs) {
  26. throw new Error("Expression " + this.getOpName() + " takes exactly " +
  27. nArgs + " arguments. " + args.length + " were passed in.");
  28. }
  29. };
  30. return FixedArityExpression;
  31. };