Skip to content

Commit

Permalink
fix: fix bug when multiple query params
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jun 26, 2015
1 parent b7a5a96 commit 880bae0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage/
node_modules/
npm-debug.log
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ node_js:
- '0.10'
- '0.12'
script:
- npm test
- npm run test-cover
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var rules = [{
}, {
// Sub delimiters
name: 'sub-delimiter',
pattern: /^(\!|\&|,|;|\-|_)/,
pattern: /^(\!|\&|\-|_)/,
regex: function regex(match) {
return new RegExp(match[0]);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ var Path = (function () {
}, {});

if (Object.keys(queryParams).every(function (p) {
return _this2.queryParams.indexOf(p) !== 1;
return Object.keys(_this2.queryParams).indexOf(p) !== 1;
}) && Object.keys(queryParams).length === this.queryParams.length) {
// Extend url match
Object.keys(queryParams).forEach(function (p) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const rules = [
{
// Sub delimiters
name: 'sub-delimiter',
pattern: /^(\!|\&|,|;|\-|_)/,
pattern: /^(\!|\&|\-|_)/,
regex: match => new RegExp(match[0])
},
{
Expand Down Expand Up @@ -119,7 +119,7 @@ export default class Path {
return obj
}, {})

if (Object.keys(queryParams).every(p => this.queryParams.indexOf(p) !== 1)
if (Object.keys(queryParams).every(p => Object.keys(this.queryParams).indexOf(p) !== 1)
&& Object.keys(queryParams).length === this.queryParams.length) {
// Extend url match
Object.keys(queryParams)
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A small utility to parse paths",
"main": "index.js",
"scripts": {
"test": "_mocha",
"test": "node ./node_modules/mocha/bin/_mocha",
"test-cover": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha",
"build": "./node_modules/.bin/babel -m common lib/Path.js > index.js"
},
"repository": {
Expand All @@ -29,6 +30,8 @@
"devDependencies": {
"should": "^6.0.3",
"mocha": "^2.2.5",
"babel": "^5.5.8"
"babel": "^5.5.8",
"coveralls": "^2.11.2",
"istanbul": "^0.3.16"
}
}
25 changes: 23 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ describe('Path', function () {
}).should.throw();
});

// it('should throw an error if a path cannot be tokenised', function () {
// (function () {
// new Path('/!#')
// }).should.throw();
// });

it('should match and build paths with url parameters', function () {
var path = new Path('/users/profile/:id-:id2.html');
// Successful match & partial match
Expand All @@ -22,10 +28,25 @@ describe('Path', function () {
path.match('/users/details/123-abc.html').should.be.false;
path.match('/users/profile/123-abc.html?what').should.be.false;

path.build({ id: '123', id2: 'abc' }).should.equal('/users/profile/123-abc.html')
path.build({ id: '123', id2: 'abc' }).should.equal('/users/profile/123-abc.html');
(function () {
path.build({ id: '123'});
}).should.throw('Missing parameters');
});

it('should match and build paths with query parameters', function () {
var path = new Path('/users?offset&limit');
// Successful match & partial match
path.match('/users?offset=31&limit=15').should.eql({ offset: '31', limit: '15' });
// path.partialMatch('/users').should.eql({});
// Unsuccessful match
path.match('/users?offset=31').should.be.false;
path.match('/users?limit=15').should.be.false;

path.build({ offset: 31, limit: 15 }).should.equal('/users?offset=31&limit=15')
});

it('should match build paths with url and query parameters', function () {
it('should match and build paths with url and query parameters', function () {
var path = new Path('/users/profile/:id-:id2?:id3');
path.hasQueryParams.should.be.true;
// Successful match & partial match
Expand Down

0 comments on commit 880bae0

Please sign in to comment.