Skip to content

Commit

Permalink
Merge pull request #10 from xiCO2k/master
Browse files Browse the repository at this point in the history
Add pathNames and queryKey
  • Loading branch information
Gal Koren authored Sep 18, 2019
2 parents ef681c7 + f33ac5a commit 1db3727
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
yarn.lock
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1db3727

Please sign in to comment.