StrcasecmpExpression.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. /**
  3. * A $strcasecmp pipeline expression.
  4. * @see evaluate
  5. * @class StrcasecmpExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var StrcasecmpExpression = module.exports = function StrcasecmpExpression(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = StrcasecmpExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var Value = require("../Value"),
  16. NaryExpression = require("./NaryExpression");
  17. // PROTOTYPE MEMBERS
  18. proto.getOpName = function getOpName(){
  19. return "$strcasecmp";
  20. };
  21. proto.addOperand = function addOperand(expr) {
  22. this.checkArgLimit(2);
  23. base.prototype.addOperand.call(this, expr);
  24. };
  25. /**
  26. * 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.
  27. * @method evaluate
  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. };