url-mapper
Advanced tools
Comparing version 0.3.3 to 0.4.0
30
index.js
@@ -25,28 +25,17 @@ 'use strict'; | ||
// TODO: Normalize URL without using location.origin | ||
if (~url.indexOf('//')) { | ||
url = url.replace('//', '~'); // To split correctly on next line we replace protocol | ||
var splitUrl = url.split('/'); | ||
splitUrl.shift(); // Remove http://www.example.com | ||
url = '/' + splitUrl.join('/'); // Bring it back together | ||
} | ||
var path = url; | ||
// This logic should probably be better, has to Handle | ||
// /#/foo, #/foo, /foo, /foo/, /#/foo/, #/foo/ | ||
var path = url.replace('#', '').replace('#', '').split(''); | ||
if (path.length > 1 && path[path.length - 1] === '/') { | ||
path.pop(); | ||
} | ||
if (path[0] === '/' && path[1] === '/') { | ||
path.shift(); | ||
} | ||
path = path.join(''); | ||
var params = {}; | ||
var route = {}; | ||
var hash = null; | ||
var queryString = null; | ||
var matchedRoute; | ||
if (~path.indexOf('#')) { | ||
hash = path.split(/#(.+)/)[1]; | ||
path = path.split('#')[0]; | ||
} | ||
if (~path.indexOf('?')) { | ||
queryString = path.split('?')[1]; | ||
queryString = path.split(/\?(.+)/)[1]; | ||
path = path.split('?')[0]; | ||
@@ -58,3 +47,3 @@ } | ||
var keys = []; | ||
var re = pathtoRegexp(route === '*' ? '(.*)' : route, keys) | ||
var re = pathtoRegexp(route, keys) | ||
cache[route] = { | ||
@@ -72,2 +61,3 @@ keys: keys, | ||
path: path, | ||
hash: hash || '', | ||
params: params, | ||
@@ -74,0 +64,0 @@ query: query |
{ | ||
"name": "url-mapper", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Take a URL and map to functions, parsing params", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,3 @@ var Router = require('../index'); | ||
test.equals(this.callbackInput.path, '/'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
@@ -42,2 +43,3 @@ test.deepEqual(this.callbackInput.query, {}); | ||
test.equals(this.callbackInput.path, '/foo/99'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, { id: '99' }); | ||
@@ -52,2 +54,3 @@ test.deepEqual(this.callbackInput.query, {}); | ||
test.equals(this.callbackInput.path, '/foo/99/baz/ad8b0483-9642-479a-a234-fb0bf21cb294'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, { id: '99', guid: 'ad8b0483-9642-479a-a234-fb0bf21cb294' }); | ||
@@ -62,2 +65,3 @@ test.deepEqual(this.callbackInput.query, {}); | ||
test.equals(this.callbackInput.path, '/encode/test%40test.com/766b8ba4ccfdaf60d0925b'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, { email: 'test@test.com', hash: '766b8ba4ccfdaf60d0925b' }); | ||
@@ -72,2 +76,3 @@ test.deepEqual(this.callbackInput.query, {}); | ||
test.equals(this.callbackInput.path, '/query'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
@@ -82,2 +87,3 @@ test.deepEqual(this.callbackInput.query, {foo: 'bar'}); | ||
test.equals(this.callbackInput.path, '/query/'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
@@ -88,6 +94,7 @@ test.deepEqual(this.callbackInput.query, {foo: 'bar'}); | ||
routeFullUrl: function (test) { | ||
test.equals(Router('http://www.example.com/foo', this.routes), '/foo'); | ||
test.equals(this.callbackInput.url, '/foo'); | ||
routeHash: function(test) { | ||
test.equals(Router('/foo#bar', this.routes), '/foo'); | ||
test.equals(this.callbackInput.url, '/foo#bar'); | ||
test.equals(this.callbackInput.path, '/foo'); | ||
test.equals(this.callbackInput.hash, 'bar'); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
@@ -98,29 +105,12 @@ test.deepEqual(this.callbackInput.query, {}); | ||
routeHashUrl: function (test) { | ||
test.equals(Router('http://www.example.com/#/foo', this.routes), '/foo'); | ||
test.equals(this.callbackInput.url, '/#/foo'); | ||
test.equals(this.callbackInput.path, '/foo'); | ||
routeQueryWithHash: function (test) { | ||
test.equals(Router('/query?foo=bar#baz', this.routes), '/query'); | ||
test.equals(this.callbackInput.url, '/query?foo=bar#baz'); | ||
test.equals(this.callbackInput.path, '/query'); | ||
test.equals(this.callbackInput.hash, 'baz'); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
test.deepEqual(this.callbackInput.query, {}); | ||
test.deepEqual(this.callbackInput.query, {foo: 'bar'}); | ||
test.done(); | ||
}, | ||
routeEndingSlash: function (test) { | ||
test.equals(Router('http://www.example.com/#/foo', this.routes), '/foo'); | ||
test.equals(this.callbackInput.url, '/#/foo'); | ||
test.equals(this.callbackInput.path, '/foo'); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
test.deepEqual(this.callbackInput.query, {}); | ||
test.done(); | ||
}, | ||
routeEndingSlashHash: function (test) { | ||
test.equals(Router('http://www.example.com/#/foo/', this.routes), '/foo'); | ||
test.equals(this.callbackInput.url, '/#/foo/'); | ||
test.equals(this.callbackInput.path, '/foo'); | ||
test.deepEqual(this.callbackInput.params, {}); | ||
test.deepEqual(this.callbackInput.query, {}); | ||
test.done(); | ||
}, | ||
catchAll: function (test) { | ||
@@ -130,2 +120,3 @@ test.equals(Router('/missing', this.routes), '*'); | ||
test.equals(this.callbackInput.path, '/missing'); | ||
test.equals(this.callbackInput.hash, ''); | ||
test.deepEqual(this.callbackInput.params, { '0': '/missing' }); | ||
@@ -168,4 +159,5 @@ test.deepEqual(this.callbackInput.query, {}); | ||
test.equals(Router('/missing', routes), undefined); | ||
test.deepEqual(this.callbackInput, {}); | ||
test.done(); | ||
} | ||
}; |
13281
347