StrcasecmpExpression.js 1.4 KB

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