Permalink
Browse files

use prototype based Class definition

  • Loading branch information...
1 parent 670f612 commit 6d3351b7c44449890ce43e64230b3372d17eaf0f @JeanSebTr committed Jul 16, 2012
Showing with 143 additions and 155 deletions.
  1. +24 −28 lib/key.js
  2. +81 −87 lib/route.js
  3. +37 −40 lib/router.js
  4. +1 −0 tests/barista.test.js
View
@@ -5,28 +5,27 @@
// key = new Key('name')
// key = new Key('name', true)
//
-var Key = function( name, optional, glob ) {
+function Key( name, optional, glob ) {
- var self = this
-
- self.name = name
- self.optional = (optional===true) ? true : false
- self.glob = (glob===true) ? true : false
- self.regex = self.glob ? /[\w\-\/]+?/ : /[\w\-]+/ // default url-friendly regex
+ this.name = name
+ this.optional = (optional===true) ? true : false
+ this.glob = (glob===true) ? true : false
+ this.regex = this.glob ? /[\w\-\/]+?/ : /[\w\-]+/ // default url-friendly regex
// special defaults for controllers & actions, which will always be function-name-safe
- if (self.name == 'controller' || self.name == 'action') {
- self.regex = /[a-zA-Z_][\w\-]*/
+ if (this.name == 'controller' || this.name == 'action') {
+ this.regex = /[a-zA-Z_][\w\-]*/
}
+}
// key.regexString()
// -----------------
// makes a regex string of the key - used by key.test()
//
// returns a string of this keys regex
- this.regexString = function() {
- var ret = String(self.regex).replace(/^\//, '').replace(/\/[gis]?$/, '')
- if (self.optional) {
+Key.prototype.regexString = function() {
+ var ret = String(this.regex).replace(/^\//, '').replace(/\/[gis]?$/, '')
+ if (this.optional) {
return '(' + ret + ')?'
}
return '(' + ret + ')'
@@ -37,20 +36,20 @@ var Key = function( name, optional, glob ) {
// validates a string using the key's regex pattern
//
// returns true/false if the string matches
- this.test = function( string ) {
- return new RegExp('^'+self.regexString()+'$').test(string)
+Key.prototype.test = function( string ) {
+ return new RegExp('^'+this.regexString()+'$').test(string)
};
// key.url( string )
// -----------------
// returns a string for buulding the url
// if it matches the key conditions
- this.url = function( string ) {
- if (self.test(string)) {
+Key.prototype.url = function( string ) {
+ if (this.test(string)) {
/*
-- no longer needed
snake_caseify the controller, if there is one
- if (self.name == 'controller') return snakeize(string)
+ if (this.name == 'controller') return snakeize(string)
*/
return string
}
@@ -62,13 +61,13 @@ var Key = function( name, optional, glob ) {
// adds conditions that the key must match
//
// returns the key... because it can?
- this.where = function( conditions ) {
+Key.prototype.where = function( conditions ) {
- var condition = conditions[self.name]
+ var condition = conditions[this.name]
- if (condition instanceof RegExp) self.regex = condition // e.g. /\d+/
+ if (condition instanceof RegExp) this.regex = condition // e.g. /\d+/
- if (condition instanceof String) self.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
+ if (condition instanceof String) this.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
// an array of allowed values, e.g. ['stop','play','pause']
if (condition instanceof Array) {
@@ -78,13 +77,10 @@ var Key = function( name, optional, glob ) {
}).map(function(cond){
return cond.replace(/^\//, '').replace(/\/[gis]?$/, '')
})
- self.regex = new RegExp(condition.join('|'))
+ this.regex = new RegExp(condition.join('|'))
}
- return self
- }
-
- return self // just in case we forgot the new operator
-};
+ return this
+ };
-exports.Key = Key
+exports.Key = Key
Oops, something went wrong.

0 comments on commit 6d3351b

Please sign in to comment.