FixedArityExpressionT.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. var FixedArityExpressionT = module.exports = function FixedArityExpressionT(SubClass, nArgs) {
  10. var FixedArityExpression = function FixedArityExpression() {
  11. if (arguments.length !== 0) throw new Error(klass.name + "<" + SubClass.name + ">: zero args expected");
  12. base.call(this);
  13. }, klass = FixedArityExpression, base = require("./NaryBaseExpressionT")(SubClass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
  14. //NOTE: attach statics to emulate the C++ behavior
  15. for (var propName in base)
  16. klass[propName] = base[propName];
  17. /**
  18. * Check that the number of args is what we expected
  19. * @method validateArguments
  20. * @param args Array The array of arguments to the expression
  21. * @throws
  22. */
  23. proto.validateArguments = function validateArguments(args) {
  24. if(args.length !== nArgs) {
  25. throw new Error("Expression " + this.getOpName() + " takes exactly " +
  26. nArgs + " arguments. " + args.length + " were passed in.");
  27. }
  28. };
  29. return FixedArityExpression;
  30. };