StrcasecmpExpression.js 1.7 KB

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