StrcasecmpExpression.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.fixedArity(2);
  12. if (arguments.length !== 0) throw new Error("zero 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(doc) {
  34. this.checkArgCount(2);
  35. var val1 = this.operands[0].evaluateInternal(doc),
  36. val2 = this.operands[1].evaluateInternal(doc),
  37. str1 = Value.coerceToString(val1).toUpperCase(),
  38. str2 = Value.coerceToString(val2).toUpperCase(),
  39. cmp = Value.compare(str1, str2);
  40. return cmp;
  41. };
  42. /** Register Expression */
  43. Expression.registerExpression("$strcasecmp", StrcasecmpExpression.parse);