diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..23d67fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +yarn.lock diff --git a/index.js b/index.js index 438d6e6..d4704b4 100644 --- a/index.js +++ b/index.js @@ -35,5 +35,34 @@ module.exports = function parseuri(str) { uri.ipv6uri = true; } + uri.pathNames = pathNames(uri, uri['path']); + uri.queryKey = queryKey(uri, uri['query']); + return uri; }; + +function pathNames(obj, path) { + var regx = /\/{2,9}/g, + names = path.replace(regx, "/").split("/"); + + if (path.substr(0, 1) == '/' || path.length === 0) { + names.splice(0, 1); + } + if (path.substr(path.length - 1, 1) == '/') { + names.splice(names.length - 1, 1); + } + + return names; +} + +function queryKey(uri, query) { + var data = {}; + + query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) { + if ($1) { + data[$1] = $2; + } + }); + + return data; +} diff --git a/package.json b/package.json index 8c70ee4..67e9ef1 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,9 @@ "test": "make test" }, "devDependencies": { + "better-assert": "~1.0.0", "mocha": "1.17.1", - "better-assert": "~1.0.0" + "expect.js": "^0.3.1" }, "author": "Gal Koren", "license": "MIT" diff --git a/test.js b/test.js index 50a361d..be447df 100644 --- a/test.js +++ b/test.js @@ -25,6 +25,9 @@ describe('my suite', function(){ expect(query.query).to.be('foo=bar'); expect(query.path).to.be('/foo/bar'); expect(query.relative).to.be('/foo/bar?foo=bar'); + expect(query.queryKey.foo).to.be('bar'); + expect(query.pathNames[0]).to.be('foo'); + expect(query.pathNames[1]).to.be('bar'); expect(localhost.protocol).to.be(''); expect(localhost.host).to.be('localhost'); expect(localhost.port).to.be('8080');