Permalink
Browse files

[minor] Adding some comments, updating tests

  • Loading branch information...
1 parent 2d5b2b1 commit f58ece8d345ef38299333dc83fee7b0ca18de5f9 @3rd-Eden 3rd-Eden committed Apr 10, 2013
Showing with 36 additions and 4 deletions.
  1. +20 −2 index.js
  2. +16 −2 tests/routes.test.js
View
@@ -4,6 +4,7 @@ var url = require('url')
, xRegExp = require('xregexp').XRegExp;
/**
+ * HTTP Route
*
* @constructor
* @param {String|RegExp} url url to match against
@@ -23,13 +24,27 @@ var Route = module.exports = function Route(url) {
Object.defineProperty(Route.prototype, 'url', {
enumerable: false
+
+ /**
+ * Returns the compiled version of a url.
+ *
+ * @returns {String}
+ */
, get: function get() {
return this._url.toString();
}
+ /**
+ * Parse the url.
+ *
+ * @param {Mixed} uri The uri that needs to be parsed.
+ */
, set: function set(uri) {
var self = this;
+ //
+ // We're already a regular expression, no need to parse it further.
+ //
if (uri instanceof RegExp) {
this._url = uri;
this.pattern = uri.source;
@@ -42,14 +57,17 @@ Object.defineProperty(Route.prototype, 'url', {
return;
}
+ //
+ // Only strings and regular expressions are allowed.
+ //
if (typeof (uri) !== 'string') throw new TypeError('url must be a String');
this._url = url.parse(uri).pathname;
this.pattern = '^';
this.flags = 'x';
this.params = [];
- this._url.split('/').forEach(function (fragment) {
+ this._url.split('/').forEach(function forEach(fragment) {
if (!fragment.length) return;
self.pattern += '\\/+';
@@ -101,7 +119,7 @@ Route.prototype.exec = function exec(uri) {
var re = xRegExp(this.pattern, this.flags)
, result = re.exec(uri.pathname);
- if (!result) return false;
+ if (!result) return {};
var params = {}
, i = 0;
View
@@ -43,15 +43,29 @@ describe('Route', function () {
expect(r.url).to.equal('/foo');
});
- it('should accept regexps as urls');
+ it('should accept regexps as urls', function () {
+ var r = new Route(/\/foo\/bar/);
+
+ expect(r.url).to.be.a('string');
+ });
it('should transform re/adfasdfa/adfasf/gm to a XRegExp');
it('should accept named parameters');
});
describe('#test', function () {
- it('should test against urls for matches');
+ it('should test against urls for matches', function () {
+ var r = new Route('/foo');
+
+ expect(r.test('/foo')).to.equal(true);
+ expect(r.test('/foo/bar')).to.equal(false);
+
+ r = new Route(/\/foo\/bar/);
+
+ expect(r.test('/foo')).to.equal(false);
+ expect(r.test('/foo/bar')).to.equal(true);
+ });
it('should accept optional parameters');
});

0 comments on commit f58ece8

Please sign in to comment.