StrcasecmpExpression.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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(klass.name + ": no args expected");
  12. base.call(this);
  13. }, klass = StrcasecmpExpression, base = require("./FixedArityExpressionT")(StrcasecmpExpression, 2), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. proto.evaluateInternal = function evaluateInternal(vars) {
  17. var string1 = this.operands[0].evaluateInternal(vars),
  18. string2 = this.operands[1].evaluateInternal(vars);
  19. var str1 = Value.coerceToString(string1).toUpperCase(),
  20. str2 = Value.coerceToString(string2).toUpperCase(),
  21. result = Value.compare(str1, str2);
  22. if (result === 0) {
  23. return 0;
  24. } else if (result > 0) {
  25. return 1;
  26. } else {
  27. return -1;
  28. }
  29. };
  30. Expression.registerExpression("$strcasecmp", base.parse);
  31. proto.getOpName = function getOpName() {
  32. return "$strcasecmp";
  33. };