StrcasecmpExpression.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var StrcasecmpExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /**
  4. * A $strcasecmp pipeline expression. @see evaluate
  5. **/
  6. var klass = function StrcasecmpExpression(){
  7. if(arguments.length !== 0) throw new Error("zero args expected");
  8. base.call(this);
  9. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  10. var Value = require("../Value"),
  11. NaryExpression = require("./NaryExpression");
  12. // PROTOTYPE MEMBERS
  13. proto.getOpName = function getOpName(){
  14. return "$strcasecmp";
  15. };
  16. proto.addOperand = function addOperand(expr) {
  17. this.checkArgLimit(2);
  18. base.prototype.addOperand.call(this, expr);
  19. };
  20. /**
  21. * Takes in two strings. Returns a number. $strcasecmp is positive if the first string is “greater than” the second and negative if the first string is “less than” the second. $strcasecmp returns 0 if the strings are identical.
  22. **/
  23. proto.evaluate = function evaluate(doc){
  24. this.checkArgCount(2);
  25. var val1 = this.operands[0].evaluate(doc),
  26. val2 = this.operands[1].evaluate(doc),
  27. str1 = Value.coerceToString(val1).toUpperCase(),
  28. str2 = Value.coerceToString(val2).toUpperCase(),
  29. cmp = Value.compare(str1, str2);
  30. return cmp;
  31. };
  32. return klass;
  33. })();