RegexMatchExpression.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var XRegExp = require('xregexp').XRegExp,
  3. LeafMatchExpression = require('./LeafMatchExpression'),
  4. ErrorCodes = require('../../Errors').ErrorCodes;
  5. var RegexMatchExpression = module.exports = function RegexMatchExpression(){
  6. base.call(this, 'REGEX');
  7. }, klass = RegexMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. klass.MaxPatternSize = 32764;
  9. proto._flags = undefined;
  10. proto._re = undefined;
  11. proto._regex = undefined;
  12. /**
  13. *
  14. * Writes a debug string for this object
  15. * @method debugString
  16. * @param level
  17. *
  18. */
  19. proto.debugString = function debugString(level) {
  20. var debug = this._debugAddSpace( level );
  21. debug += this.path() + " regex /" + this._regex + "/" + this._flags;
  22. var td = this.getTag();
  23. if (td === null) {
  24. debug += " " + td.debugString();
  25. }
  26. debug += "\n";
  27. return debug;
  28. };
  29. proto.shortDebugString = function shortDebugString() {
  30. return "/" + this._regex + "/" + this._flags;
  31. };
  32. /**
  33. *
  34. * checks if this expression is == to the other
  35. * @method equivalent
  36. * @param other
  37. *
  38. */
  39. proto.equivalent = function equivalent(other) {
  40. if (this.matchType() !== other.matchType()) return false;
  41. return this.path() === other.path() && this._regex === other._regex && this._flags === other._flags;
  42. };
  43. /**
  44. *
  45. * Return the _flags property
  46. * @method getFlags
  47. *
  48. */
  49. proto.getFlags = function getFlags(){
  50. return this._flags;
  51. };
  52. /**
  53. *
  54. * Return the _regex property
  55. * @method getString
  56. * @param
  57. *
  58. */
  59. proto.getString = function getString(){
  60. return this._regex;
  61. };
  62. /**
  63. *
  64. * Initialize the necessary items
  65. * @method init
  66. * @param path
  67. * @param type
  68. *
  69. */
  70. proto.init = function init(path,regex,flags) {
  71. if(regex.toString().length > klass.MaxPatternSize){
  72. return {'code':ErrorCodes.BAD_VALUE, 'desc':'Regular Expression too long.'};
  73. }
  74. if (regex instanceof RegExp){
  75. this._regex = regex.source;
  76. this._re = regex;
  77. this._flags = (this._re.ignoreCase ? 'i' : '') + (this._re.multiline ? 'm' : '');
  78. } else if (typeof regex === 'string' && (!flags || typeof flags === 'string' )) {
  79. this._regex = regex;
  80. //remove invalid flags, sort and uniquify them
  81. this._flags = (flags || '').replace( /[^imxs]/g, '').split('').sort().filter(function(el,i,a){return i===a.indexOf(el);}).join('');
  82. this._re = new XRegExp(regex,this._flags);
  83. } else {
  84. return {'code':ErrorCodes.BAD_VALUE, 'desc':'regex not a regex'};
  85. }
  86. return this.initPath( path );
  87. };
  88. /**
  89. *
  90. * Check if the input element matches
  91. * @method matchesSingleElement
  92. * @param e
  93. *
  94. */
  95. proto.matchesSingleElement = function matchesSingleElement(e) {
  96. if(e instanceof RegExp){
  97. return e.toString() === this._re.toString();
  98. }
  99. if(typeof e === 'string'){
  100. return this._re.test(e);
  101. }
  102. // No support for SYMBOLS currently
  103. };
  104. /**
  105. *
  106. * clone this instance to a new one
  107. * @method shallowClone
  108. *
  109. */
  110. proto.shallowClone = function shallowClone(){
  111. var e = new RegexMatchExpression();
  112. e.init( this.path(), this._regex, this._flags );
  113. if ( this.getTag() ) {
  114. e.setTag(this.getTag().clone());
  115. }
  116. return e;
  117. };