Matcher2.js 1.3 KB

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