1
+ /*!
2
+ * VerbalExpressions JavaScript Library v0.1
3
+ * https://github.com/jehna/VerbalExpressions
4
+ *
5
+ *
6
+ * Released under the MIT license
7
+ * http://jquery.org/license
8
+ *
9
+ * Date: 2013-07-19
10
+ *
11
+ */
12
+
13
+ // Define the collection class.
14
+ window . VerbalExpression = ( function ( ) {
15
+
16
+ // I am the constructor function.
17
+ function VerbalExpression ( ) {
18
+ var verbalExpression = Object . create ( RegExp . prototype ) ;
19
+
20
+ // Initialize
21
+ verbalExpression = ( RegExp . apply ( verbalExpression , arguments ) || verbalExpression ) ;
22
+
23
+ // Add all the class methods
24
+ VerbalExpression . injectClassMethods ( verbalExpression ) ;
25
+
26
+ // Return the new object.
27
+ return ( verbalExpression ) ;
28
+ }
29
+
30
+
31
+ // Define the static methods.
32
+ VerbalExpression . injectClassMethods = function ( verbalExpression ) {
33
+
34
+ // Loop over all the prototype methods
35
+ for ( var method in VerbalExpression . prototype ) {
36
+
37
+ // Make sure this is a local method.
38
+ if ( VerbalExpression . prototype . hasOwnProperty ( method ) ) {
39
+
40
+ // Add the method
41
+ verbalExpression [ method ] = VerbalExpression . prototype [ method ] ;
42
+
43
+ }
44
+
45
+ }
46
+
47
+ return ( verbalExpression ) ;
48
+
49
+ } ;
50
+
51
+
52
+ // Define the class methods.
53
+ VerbalExpression . prototype = {
54
+
55
+ _source : "" ,
56
+
57
+ lol : function ( value ) {
58
+ console . log ( "loL" ) ;
59
+ this . compile ( value ) ;
60
+ return ( this ) ;
61
+ } ,
62
+
63
+ sanitize : function ( value ) {
64
+ return value . replace ( / [ ^ \w ] / g, function ( character ) { return "\\" + character ; } ) ;
65
+ } ,
66
+
67
+ startOfLine : function ( ) {
68
+ if ( this . source . indexOf ( "^" ) !== 0 ) this . _source = "^" + this . _source ;
69
+ this . add ( "" ) ;
70
+ return ( this ) ;
71
+ } ,
72
+
73
+ add : function ( value ) {
74
+ this . _source += value || "" ;
75
+ this . compile ( this . _source ) ;
76
+ return ( this ) ;
77
+ } ,
78
+
79
+ then : function ( value ) {
80
+ value = this . sanitize ( value ) ;
81
+ this . add ( "(" + value + ")" ) ;
82
+ return ( this ) ;
83
+ } ,
84
+
85
+ find : function ( value ) {
86
+ return ( this . then ( value ) ) ;
87
+ } ,
88
+
89
+ maybe : function ( value ) {
90
+ value = this . sanitize ( value ) ;
91
+ this . add ( "(" + value + ")?" ) ;
92
+ return ( this ) ;
93
+ } ,
94
+
95
+ anything : function ( ) {
96
+ this . add ( "(.*)" ) ;
97
+ return ( this ) ;
98
+ } ,
99
+
100
+ endOfLine : function ( ) {
101
+ this . add ( "$" ) ;
102
+ return ( this ) ;
103
+ } ,
104
+
105
+ anythingBut : function ( value ) {
106
+ value = this . sanitize ( value ) ;
107
+ this . add ( "([^" + value + "]*)" ) ;
108
+ return ( this ) ;
109
+ } ,
110
+
111
+ replace : function ( source , value ) {
112
+ source = source . toString ( ) ;
113
+ return source . replace ( this , value ) ;
114
+ }
115
+
116
+ } ;
117
+
118
+
119
+ // Return the constructor.
120
+ return ( VerbalExpression ) ;
121
+
122
+
123
+ } ) . call ( { } ) ;
124
+
125
+ // Create shorthand
126
+ ( function ( w ) {
127
+ w . VerEx = function ( ) {
128
+ return new VerbalExpression ( ) ;
129
+ } ;
130
+ } ) ( window ) ;
0 commit comments