StrcasecmpExpression.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. var StrcasecmpExpression = module.exports = (function(){
  2. // CONSTRUCTOR
  3. /** A $strcasecmp pipeline expression. @see evaluate **/
  4. var klass = function StrcasecmpExpression(){
  5. if(arguments.length !== 0) throw new Error("zero args expected");
  6. base.call(this);
  7. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. var Value = require("../Value"),
  9. NaryExpression = require("./NaryExpression");
  10. // PROTOTYPE MEMBERS
  11. proto.getOpName = function getOpName(){
  12. return "$strcasecmp";
  13. };
  14. proto.addOperand = function addOperand(expr) {
  15. this.checkArgLimit(2);
  16. base.addOperand(expr);
  17. };
  18. /** 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. **/
  19. proto.evaluate = function evaluate(doc){
  20. this.checkArgCount(2);
  21. var val1 = this.operands[0].evaluate(doc),
  22. val2 = this.operands[1].evaluate(doc),
  23. str1 = Value.coerceToString(val1).toUpperCase(),
  24. str2 = Value.coerceToString(val2).toUpperCase(),
  25. cmp = Value.compare(str1, str2);
  26. return cmp;
  27. };
  28. return klass;
  29. })();