Skip to content

Commit d4af554

Browse files
committed
Added: Many new class methods
Removed: lol method Added: Comments on class methods Added methods: lineBreak, br, tab, word, anyOf, any, range, modifiers, multiple-loop
1 parent 9c18a10 commit d4af554

File tree

1 file changed

+173
-16
lines changed

1 file changed

+173
-16
lines changed

VerbalExpressions.js

Lines changed: 173 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,67 +52,224 @@ window.VerbalExpression = (function(){
5252
// Define the class methods.
5353
VerbalExpression.prototype = {
5454

55+
// Variables to hold the whole
56+
// expression construction in order
57+
_prefixes : "",
5558
_source : "",
59+
_suffixes : "",
60+
_modifiers : "gm", // default to global multiline matching
5661

57-
lol: function( value ){
58-
console.log( "loL" );
59-
this.compile( value );
60-
return( this );
61-
},
6262

63+
// Sanitation function for adding
64+
// anything safely to the expression
6365
sanitize : function( value ) {
6466
return value.replace(/[^\w]/g, function(character) { return "\\" + character; });
6567
},
6668

67-
startOfLine: function() {
68-
if(this.source.indexOf( "^" ) !== 0) this._source = "^" + this._source;
69+
// Function to add stuff to the
70+
// expression. Also compiles the
71+
// new expression so it's ready to
72+
// be used.
73+
add: function( value ) {
74+
this._source += value || "";
75+
this.compile(this._prefixes + this._source + this._suffixes, this._modifiers);
76+
return( this );
77+
},
78+
79+
// Start and end of line functions
80+
startOfLine: function( enable ) {
81+
enable = ( enable != false );
82+
this._prefixes = enable ? "^" : "";
6983
this.add( "" );
7084
return( this );
7185
},
7286

73-
add: function( value ) {
74-
this._source += value || "";
75-
this.compile(this._source);
87+
endOfLine : function( enable ) {
88+
enable = ( enable != false );
89+
this._suffixes = enable ? "$" : "";
90+
this.add( "" );
7691
return( this );
7792
},
7893

94+
// We try to keep the syntax ass
95+
// user-friendly as possible.
96+
// So we can use the "normal"
97+
// behaviour to split the "scentences"
98+
// naturally.
7999
then : function( value ) {
80100
value = this.sanitize(value);
81101
this.add( "(" + value + ")" );
82102
return( this );
83103
},
84104

105+
// And because we can't start with
106+
// "then" function, we create an alias
107+
// to be used as the first function
108+
// of the chain.
85109
find : function( value ) {
86110
return( this.then( value ) );
87111
},
88112

113+
// Maybe is used to add values with ?
89114
maybe : function( value ) {
90115
value = this.sanitize(value);
91116
this.add( "(" + value + ")?" );
92117
return( this );
93118
},
94119

120+
// Any character any times
95121
anything : function() {
96122
this.add( "(.*)" );
97123
return( this );
98124
},
99125

100-
endOfLine : function() {
101-
this.add( "$" );
102-
return( this );
103-
},
104-
126+
// Anything but these characters
105127
anythingBut : function( value ) {
106128
value = this.sanitize(value);
107129
this.add( "([^" + value + "]*)" );
108130
return( this );
109131
},
110132

133+
// Shorthand function for the
134+
// String.replace function to
135+
// give more logical flow if, for
136+
// example, we're doing multiple
137+
// replacements on one regexp.
111138
replace : function( source, value ) {
112139
source = source.toString();
113140
return source.replace( this, value );
141+
},
142+
143+
144+
/// Add regular expression special ///
145+
/// characters ///
146+
147+
// Line break
148+
lineBreak : function() {
149+
this.add( "(\\n|(\\r\\n))" ); // Unix + windows CLRF
150+
return( this );
151+
},
152+
// And a shorthand for html-minded
153+
br : function() {
154+
return this.lineBreak();
155+
},
156+
157+
// Tab (duh?)
158+
tab : function() {
159+
this.add( "\\t" );
160+
return( this );
161+
},
162+
163+
// Any alphanumeric
164+
word : function() {
165+
this.add( "\\w+" );
166+
return( this );
167+
},
168+
169+
// Any given character
170+
anyOf : function( value ) {
171+
value = this.sanitize(value);
172+
this.add( "["+ value +"]" );
173+
return( this );
174+
},
175+
176+
// Shorthand
177+
any : function( value ) {
178+
return( this.anyOf( value ) );
179+
},
180+
181+
// Usage: .range( from, to [, from, to ...] )
182+
range : function() {
183+
184+
var value = "[";
185+
console.log(arguments);
186+
187+
for(var _from = 0; _from < arguments.length; _from += 2) {
188+
var _to = _from+1;
189+
if(arguments.length <= to) break;
190+
191+
var from = this.sanitize( arguments[_from] );
192+
var to = this.sanitize( arguments[_to] );
193+
194+
value += from + "-" + to;
195+
}
196+
197+
value += "]";
198+
199+
this.add( value );
200+
return( this );
201+
},
202+
203+
204+
/// Modifiers ///
205+
206+
// Modifier abstraction
207+
addModifier : function( modifier ) {
208+
if( this._modifiers.indesxOf( modifier ) == -1 ) {
209+
this._modifiers += modifier;
210+
}
211+
this.add("");
212+
return( this );
213+
},
214+
removeModifier : function( modifier ) {
215+
this._modifiers = this._modifiers.replace( modifier, "" );
216+
this.add("");
217+
return( this );
218+
},
219+
220+
// Case-insensitivity modifier
221+
withAnyCase : function( enable ) {
222+
223+
if(enable != false) this.addModifier( "i" );
224+
else this.removeModifier( "i" );
225+
226+
this.add( "" );
227+
return( this );
228+
229+
},
230+
231+
// Default behaviour is with "g" modifier,
232+
// so we can turn this another way around
233+
// than other modifiers
234+
stopAtFirst : function( enable ) {
235+
236+
if(enable != false) this.removeModifier( "g" );
237+
else this.addModifier( "g" );
238+
239+
this.add( "" );
240+
return( this );
241+
242+
},
243+
244+
// Multiline, also reversed
245+
searhOneLine : function( enable ) {
246+
247+
if(enable != false) this.removeModifier( "m" );
248+
else this.addModifier( "m" );
249+
250+
this.add( "" );
251+
return( this );
252+
253+
},
254+
255+
256+
257+
/// Loops ///
258+
259+
multiple : function( value ) {
260+
// Use expression or string
261+
value = value.source ? value.source : this.sanitize(value);
262+
switch(value.substr(-1)) {
263+
case "*":
264+
case "+":
265+
break;
266+
default:
267+
value += "+";
268+
}
269+
this.add( value );
270+
return( this );
114271
}
115-
272+
116273
};
117274

118275

0 commit comments

Comments
 (0)