ElemMatchObjectMatchExpression.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var ElemMatchObjectMatchExpression = module.exports = function ElemMatchObjectMatchExpression(){
  3. base.call(this);
  4. }, klass = ElemMatchObjectMatchExpression, base = require("./ArrayMatchingMatchExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  5. proto._sub = undefined;
  6. /**
  7. *
  8. * Writes a debug string for this object
  9. * @method debugString
  10. * @param level
  11. *
  12. */
  13. proto.debugString = function debugString(level){
  14. return this._debugAddSpace( level ) +
  15. this.path() + " $elemMatch (obj) " +
  16. (this.getTag() ? this.getTag().debugString() : "") +
  17. this._exp._debugString( level + 1 );
  18. };
  19. /**
  20. *
  21. * Return 1 since we have a single "sub"
  22. * @method numChildren
  23. *
  24. */
  25. proto.numChildren = function numChildren(){
  26. return 1;
  27. };
  28. /**
  29. *
  30. * Return the _sub property
  31. * @method getChild
  32. * @param i
  33. *
  34. */
  35. proto.getChild = function getChild(i){
  36. return this._sub.get();
  37. };
  38. /**
  39. *
  40. * Initialize the necessary items
  41. * @method init
  42. * @param path
  43. * @param type
  44. *
  45. */
  46. proto.init = function init(path, sub){
  47. this._sub = sub;
  48. return this.initPath(path);
  49. };
  50. /**
  51. *
  52. * Check if one of the items in the input array matches _sub
  53. * @method matchesArray
  54. * @param anArray
  55. * @param details
  56. *
  57. */
  58. proto.matchesArray = function matchesArray(anArray, details){
  59. for (var i in anArray) { //jshint ignore:line
  60. var inner = anArray[i];
  61. if (!(inner instanceof Object))
  62. continue;
  63. if (this._sub.matchesJSON(inner, null)) {
  64. if (details && details.needRecord()) {
  65. details.setElemMatchKey(i);
  66. }
  67. return true;
  68. }
  69. }
  70. return false;
  71. };
  72. /**
  73. *
  74. * clone this instance to a new one
  75. * @method shallowClone
  76. *
  77. */
  78. proto.shallowClone = function shallowClone(){
  79. var element = new ElemMatchObjectMatchExpression();
  80. element.init(this.path(), this._sub.shallowClone());
  81. if ( this.getTag() ){
  82. element.setTag(this.getTag().clone());
  83. }
  84. return element;
  85. };