Skip to content

Commit 9acce1c

Browse files
committed
remove deprecated calls to compile & deferring compilation. fixes VerbalExpressions#7
1 parent 484e9b4 commit 9acce1c

File tree

8 files changed

+1370
-983
lines changed

8 files changed

+1370
-983
lines changed

VerbalExpressions.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,19 @@
1919
* I am the constructor function.
2020
* @constructor
2121
* @alias VerEx
22-
* @return {RegExp} A new instance of RegExp with injected methods
22+
* @return {VerbalExpression} A new instance of VerbalExpression
2323
*/
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() {}
3325

3426
/**
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
3728
*/
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;
4830

49-
return verbalExpression;
50-
};
31+
/**
32+
* Cache of compiled regex
33+
*/
34+
VerbalExpression.prototype._regexp = undefined;
5135

5236
/**
5337
* Regex prefixes
@@ -100,7 +84,6 @@
10084
*/
10185
VerbalExpression.prototype.add = function add(value) {
10286
this._source += value || '';
103-
this.compile(this._prefixes + this._source + this._suffixes, this._modifiers);
10487

10588
return this;
10689
};
@@ -214,8 +197,11 @@
214197
* @return {VerbalExpression} Same instance of VerbalExpression to allow method chaining
215198
*/
216199
VerbalExpression.prototype.replace = function replace(source, value) {
200+
if (this._dirty) {
201+
this.toRegExp();
202+
}
217203
source = source.toString();
218-
return source.replace(this, value);
204+
return source.replace(this._regexp, value);
219205
};
220206

221207
/// Add regular expression special ///
@@ -501,8 +487,34 @@
501487
* @return {RegExp} Converted RegExp instance
502488
*/
503489
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);
506518
};
507519

508520
/**

0 commit comments

Comments
 (0)