Skip to content

Commit

Permalink
feat: add spat param flag
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jun 25, 2015
1 parent e346bbf commit b77174a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var rules = [{
regex: /([a-zA-Z0-9-_.~]+)/
}, {
// Url parameter (splat)
name: 'url-parameter',
name: 'url-parameter-splat',
pattern: /^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: /(.*?)/
}, {
Expand Down Expand Up @@ -82,14 +82,17 @@ var Path = (function () {
this.tokens = tokenise(path);

this.hasUrlParams = this.tokens.filter(function (t) {
return t.type === 'url-parameter';
return /url-parameter/.test(t.type);
}).length > 0;
this.hasSpatParam = this.tokens.filter(function (t) {
return /splat/.test(t.type);
}).length > 0;
this.hasQueryParams = this.tokens.filter(function (t) {
return t.type === 'query-parameter';
}).length > 0;
// Extract named parameters from tokens
this.urlParams = !this.hasUrlParams ? [] : this.tokens.filter(function (t) {
return t.type === 'url-parameter';
return /url-parameter/.test(t.type);
}).map(function (t) {
return t.val;
})
Expand Down
7 changes: 4 additions & 3 deletions lib/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const rules = [
},
{
// Url parameter (splat)
name: 'url-parameter',
name: 'url-parameter-splat',
pattern: /^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: /(.*?)/
},
Expand Down Expand Up @@ -70,11 +70,12 @@ export default class Path {
this.path = path
this.tokens = tokenise(path)

this.hasUrlParams = this.tokens.filter(t => t.type === 'url-parameter').length > 0
this.hasUrlParams = this.tokens.filter(t => /url-parameter/.test(t.type)).length > 0
this.hasSpatParam = this.tokens.filter(t => /splat/.test(t.type)).length > 0
this.hasQueryParams = this.tokens.filter(t => t.type === 'query-parameter').length > 0
// Extract named parameters from tokens
this.urlParams = !this.hasUrlParams ? [] : this.tokens
.filter(t => t.type === 'url-parameter')
.filter(t => /url-parameter/.test(t.type))
.map(t => t.val)
// Flatten
.reduce((r, v) => r.concat(v))
Expand Down
4 changes: 3 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Path', function () {

it('should match 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
path.match('/users/profile/123-456?id3=789').should.eql({ id: '123', id2: '456', id3: '789' });
path.partialMatch('/users/profile/123-456').should.eql({ id: '123', id2: '456' });
Expand All @@ -39,7 +40,8 @@ describe('Path', function () {

it('should match build paths with splat parameters', function () {
var path = new Path('/users/*splat/:id');

path.hasSpatParam.should.be.true;
// Successful match
path.match('/users/profile/view/123').should.eql({ splat: 'profile/view', id: '123' });
});
});

0 comments on commit b77174a

Please sign in to comment.