ModMatchExpression.js 2.2 KB

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