StrcasecmpExpression.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. base.call(this);
  12. }, klass = StrcasecmpExpression,
  13. FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
  14. base = FixedArityExpression,
  15. proto = klass.prototype = Object.create(base.prototype, {
  16. constructor: {
  17. value: klass
  18. }
  19. });
  20. // DEPENDENCIES
  21. var Value = require("../Value"),
  22. NaryExpression = require("./NaryExpression"),
  23. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$strcasecmp";
  27. };
  28. /**
  29. * 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.
  30. * @method evaluate
  31. **/
  32. proto.evaluateInternal = function evaluateInternal(vars) {
  33. var val1 = this.operands[0].evaluateInternal(vars),
  34. val2 = this.operands[1].evaluateInternal(vars),
  35. str1 = Value.coerceToString(val1).toUpperCase(),
  36. str2 = Value.coerceToString(val2).toUpperCase(),
  37. cmp = Value.compare(str1, str2);
  38. return cmp;
  39. };
  40. /** Register Expression */
  41. Expression.registerExpression("$strcasecmp", base.parse);