Matcher.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. /**
  3. * Matcher is a simple wrapper around a JSONObj and the MatchExpression created from it.
  4. * @class Matcher
  5. * @namespace mungedb-aggregate.pipeline.matcher
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var Matcher = module.exports = function Matcher(pattern, whereCallback){
  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 = Matcher, 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. if ((doc != {}) && (Object.keys(doc)[0]))
  36. return this._expression.matchesBSON(doc, details);
  37. return this._expression.matches(mydoc, details);
  38. };
  39. /**
  40. *
  41. * Return the _pattern property
  42. * @method getQuery
  43. *
  44. */
  45. proto.getQuery = function getQuery(){
  46. return this._pattern;
  47. };
  48. /**
  49. *
  50. * Convert _pattern into a string
  51. * @method toString
  52. *
  53. */
  54. proto.toString = function toString(){
  55. return this._pattern.toString();
  56. };