|
19 | 19 | * I am the constructor function.
|
20 | 20 | * @constructor
|
21 | 21 | * @alias VerEx
|
22 |
| - * @return {RegExp} A new instance of RegExp with injected methods |
| 22 | + * @return {VerbalExpression} A new instance of VerbalExpression |
23 | 23 | */
|
24 |
| - function VerbalExpression() { |
25 |
| - var verbalExpression = new RegExp(); |
26 |
| - |
27 |
| - // Add all the class methods |
28 |
| - VerbalExpression.injectClassMethods(verbalExpression); |
29 |
| - |
30 |
| - // Return the new object. |
31 |
| - return verbalExpression; |
32 |
| - } |
| 24 | + function VerbalExpression() {} |
33 | 25 |
|
34 | 26 | /**
|
35 |
| - * @param {RegExp} verbalExpression An instance of RegExp on which to add VerbalExpressions methods |
36 |
| - * @return {RegExp} A new instance of RegExp with injected methods |
| 27 | + * Track changes requiring recompile |
37 | 28 | */
|
38 |
| - VerbalExpression.injectClassMethods = function injectClassMethods(verbalExpression) { |
39 |
| - var method; |
40 |
| - // Loop over all the prototype methods |
41 |
| - for (method in VerbalExpression.prototype) { |
42 |
| - // Make sure this is a local method. |
43 |
| - if (VerbalExpression.prototype.hasOwnProperty(method)) { |
44 |
| - // Add the method |
45 |
| - verbalExpression[method] = VerbalExpression.prototype[method]; |
46 |
| - } |
47 |
| - } |
| 29 | + VerbalExpression.prototype._dirty = true; |
48 | 30 |
|
49 |
| - return verbalExpression; |
50 |
| - }; |
| 31 | + /** |
| 32 | + * Cache of compiled regex |
| 33 | + */ |
| 34 | + VerbalExpression.prototype._regexp = undefined; |
51 | 35 |
|
52 | 36 | /**
|
53 | 37 | * Regex prefixes
|
|
100 | 84 | */
|
101 | 85 | VerbalExpression.prototype.add = function add(value) {
|
102 | 86 | this._source += value || '';
|
103 |
| - this.compile(this._prefixes + this._source + this._suffixes, this._modifiers); |
104 | 87 |
|
105 | 88 | return this;
|
106 | 89 | };
|
|
214 | 197 | * @return {VerbalExpression} Same instance of VerbalExpression to allow method chaining
|
215 | 198 | */
|
216 | 199 | VerbalExpression.prototype.replace = function replace(source, value) {
|
| 200 | + if (this._dirty) { |
| 201 | + this.toRegExp(); |
| 202 | + } |
217 | 203 | source = source.toString();
|
218 |
| - return source.replace(this, value); |
| 204 | + return source.replace(this._regexp, value); |
219 | 205 | };
|
220 | 206 |
|
221 | 207 | /// Add regular expression special ///
|
|
501 | 487 | * @return {RegExp} Converted RegExp instance
|
502 | 488 | */
|
503 | 489 | VerbalExpression.prototype.toRegExp = function toRegExp() {
|
504 |
| - var array = this.toString().match(/\/(.*)\/([gimuy]+)?/); |
505 |
| - return new RegExp(array[1], array[2]); |
| 490 | + if (this._dirty) { |
| 491 | + this._regexp = new RegExp(this._prefixes + this._source + this._suffixes, this._modifiers); |
| 492 | + } |
| 493 | + |
| 494 | + return new RegExp(this._regexp.source, this._modifiers); |
| 495 | + }; |
| 496 | + |
| 497 | + /** |
| 498 | + * Convert regex to string |
| 499 | + * @return {String} string representation of verbal expression |
| 500 | + */ |
| 501 | + VerbalExpression.prototype.toString = function toString() { |
| 502 | + if (this._dirty) { |
| 503 | + return (this.toRegExp() + ''); |
| 504 | + } |
| 505 | + return (this._regex + ''); |
| 506 | + }; |
| 507 | + |
| 508 | + /** |
| 509 | + * Test string against regex |
| 510 | + * @param {String} str string to test |
| 511 | + * @return {Boolean} true if string passes test, false otherwise |
| 512 | + */ |
| 513 | + VerbalExpression.prototype.test = function test(str) { |
| 514 | + if (this._dirty) { |
| 515 | + this.toRegExp(); |
| 516 | + } |
| 517 | + return this._regexp.test(str); |
506 | 518 | };
|
507 | 519 |
|
508 | 520 | /**
|
|
0 commit comments