SizeMatchExpression.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var ArrayMatchingMatchExpression = require('./ArrayMatchingMatchExpression');
  3. var SizeMatchExpression = module.exports = function SizeMatchExpression(){
  4. base.call(this, 'SIZE');
  5. }, klass = SizeMatchExpression, base = ArrayMatchingMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  6. proto._size = undefined;
  7. /**
  8. *
  9. * Writes a debug string for this object
  10. * @method debugString
  11. * @param level
  12. *
  13. */
  14. proto.debugString = function debugString(level) {
  15. var debug = this._debugAddSpace( level ) + this.path() + " $size : " + this._size.toString() + "\n";
  16. var td = this.tagData();
  17. if (td !== null){
  18. debug += " " + td.debugString();
  19. }
  20. return debug;
  21. };
  22. /**
  23. *
  24. * checks if this expression is == to the other
  25. * @method equivalent
  26. * @param other
  27. *
  28. */
  29. proto.equivalent = function equivalent(other) {
  30. if(other.matchType() !== this.matchType()) {
  31. return false;
  32. }
  33. return this._size === other._size && this.path() === other.path();
  34. };
  35. /**
  36. *
  37. * Return the _size property
  38. * @method getData
  39. *
  40. */
  41. proto.getData = function getData(){
  42. return this._size;
  43. };
  44. /**
  45. *
  46. * Initialize the necessary items
  47. * @method init
  48. * @param path
  49. * @param size
  50. *
  51. */
  52. proto.init = function init(path,size) {
  53. this._size = size;
  54. return this.initPath(path);
  55. };
  56. /**
  57. *
  58. * Check if the input array matches
  59. * @method matchesArray
  60. * @param anArray
  61. * @param details
  62. *
  63. */
  64. proto.matchesArray = function matchesArray(anArray, details) {
  65. if(this._size < 0) {
  66. return false;
  67. }
  68. return anArray.length === this._size;
  69. };
  70. /**
  71. *
  72. * clone this instance to a new one
  73. * @method shallowClone
  74. *
  75. */
  76. proto.shallowClone = function shallowClone(){
  77. // File: expression_array.h lines: 116-119
  78. var e = new SizeMatchExpression();
  79. e.init(this.path(),this._size);
  80. if ( this.getTag() ) {
  81. e.setTag(this.getTag().clone());
  82. }
  83. return e;
  84. };