Matcher2.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. /**
  3. * Matcher2 is a simple wrapper around a JSONObj and the MatchExpression created from it.
  4. * @class Matcher2
  5. * @namespace mungedb-aggregate.pipeline.matcher2
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var Matcher2 = module.exports = function Matcher2(pattern, nested){
  10. this._pattern = pattern;
  11. this.parser = new MatchExpressionParser();
  12. var result = this.parser.parse(pattern);
  13. if (result.code != ErrorCodes.OK)
  14. return {code:16810, description:"bad query: " + result};
  15. this._expression = result.result;
  16. }, klass = Matcher2, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  17. // DEPENDENCIES
  18. var errors = require("../../Errors.js"),
  19. ErrorCodes = errors.ErrorCodes,
  20. MatchExpressionParser = require("./MatchExpressionParser.js");
  21. //PROTOTYPE MEMBERS
  22. proto._expression = undefined;
  23. proto._pattern = undefined;
  24. /**
  25. *
  26. * matches checks the input doc against the internal element path to see if it is a match
  27. * @method matches
  28. * @param doc
  29. * @param details
  30. *
  31. */
  32. proto.matches = function matches(doc, details){
  33. if (!this._expression)
  34. return true;
  35. return this._expression.matchesJSON(doc, details);
  36. };
  37. /**
  38. *
  39. * Return the _pattern property
  40. * @method getQuery
  41. *
  42. */
  43. proto.getQuery = function getQuery(){
  44. return this._pattern;
  45. };
  46. /**
  47. *
  48. * Convert _pattern into a string
  49. * @method toString
  50. *
  51. */
  52. proto.toString = function toString(){
  53. return this._pattern.toString();
  54. };