ModMatchExpression.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var LeafMatchExpression = require("./LeafMatchExpression"),
  3. ErrorCodes = require("../errors").ErrorCodes;
  4. var ModMatchExpression = module.exports = function ModMatchExpression(){
  5. base.call(this);
  6. this._matchType = "MOD";
  7. }, klass = ModMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  8. proto._divisor = undefined;
  9. proto._remainder = undefined;
  10. /**
  11. *
  12. * Writes a debug string for this object
  13. * @method debugString
  14. * @param level
  15. *
  16. */
  17. proto.debugString = function debugString(level) {
  18. return this._debugAddSpace(level) +
  19. this.path() + " mod " + this._divisor + " % x == " + this._remainder +
  20. (this.getTag() ? " " + this.getTag().debugString() : "") +
  21. "\n";
  22. };
  23. /**
  24. *
  25. * checks if this expression is == to the other
  26. * @method equivalent
  27. * @param other
  28. *
  29. */
  30. proto.equivalent = function equivalent(other) {
  31. if(other._matchType !== this._matchType)
  32. return false;
  33. return this.path() === other.path() && this._divisor === other._divisor && this._remainder === other._remainder;
  34. };
  35. /**
  36. *
  37. * Return the _divisor property
  38. * @method getDivisor
  39. *
  40. */
  41. proto.getDivisor = function getDivisor(){
  42. return this._divisor;
  43. };
  44. /**
  45. *
  46. * Return the _remainder property
  47. * @method getRemainder
  48. *
  49. */
  50. proto.getRemainder = function getRemainder( /* */ ){
  51. return this._remainder;
  52. };
  53. /**
  54. *
  55. * Initialize the necessary items
  56. * @method init
  57. * @param path
  58. * @param type
  59. *
  60. */
  61. proto.init = function init(path,divisor,remainder) {
  62. if (divisor === 0 ){
  63. return {"code":ErrorCodes.BAD_VALUE, "desc":"Divisor cannot be 0"};
  64. }
  65. this._divisor = divisor;
  66. this._remainder = remainder;
  67. return this.initPath( path );
  68. };
  69. /**
  70. *
  71. * Check if the input element matches
  72. * @method matchesSingleElement
  73. * @param e
  74. *
  75. */
  76. proto.matchesSingleElement = function matchesSingleElement(e) {
  77. if(typeof(e) !== "number") {
  78. return false;
  79. }
  80. return (e % this._divisor) === this._remainder;
  81. };
  82. /**
  83. *
  84. * clone this instance to a new one
  85. * @method shallowClone
  86. *
  87. */
  88. proto.shallowClone = function shallowClone(){
  89. var e = new ModMatchExpression();
  90. e.init(this.path(),this._divisor, this._remainder);
  91. if (this.getTag())
  92. e.setTag(this.getTag().clone());
  93. return e;
  94. };