SetDifferenceExpression.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. /**
  3. * A $setdifference pipeline expression.
  4. * @see evaluateInternal
  5. * @class SubstrExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetDifferenceExpression = module.exports = function SetDifferenceExpression() {
  11. this.fixedAirty(2);
  12. if (arguments.length !== 2) throw new Error("two args expected");
  13. base.call(this);
  14. }, klass = SetDifferenceExpression,
  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. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$setdifference";
  27. };
  28. /**
  29. * Takes 2 arrays. Assigns the second array to the first array.
  30. * @method evaluateInternal
  31. **/
  32. proto.evaluateInternal = function evaluateInternal(doc) {
  33. var array1 = this.operands[0].evaluateInternal(doc),
  34. array2 = this.operands[1].evaluateInternal(doc);
  35. if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
  36. if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
  37. var returnVec = [];
  38. array1.forEach(function(key) {
  39. if (-1 === array2.indexOf(key)) {
  40. returnVec.push(key);
  41. }
  42. }, this);
  43. return returnVec;
  44. };
  45. /** Register Expression */
  46. Expression.registerExpression("$setdifference", SetDifferenceExpression.parse);