Skip to content

Commit

Permalink
feat: support matrix parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 1, 2015
1 parent 496a560 commit 0451290
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
14 changes: 12 additions & 2 deletions dist/commonjs/path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var rules = [{
name: 'url-parameter-splat',
pattern: /^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: /([^\?]*)/
}, {
name: 'url-parameter-matrix',
pattern: /^\;([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: function regex(match) {
return new RegExp(';' + match[1] + '=([a-zA-Z0-9-_.~]+)');
}
}, {
// Query parameter: ?param1&param2
// ?:param1&:param2
Expand All @@ -34,7 +40,7 @@ var rules = [{
}, {
// Sub delimiters
name: 'sub-delimiter',
pattern: /^(\!|\&|\-|_|\.)/,
pattern: /^(\!|\&|\-|_|\.|;)/,
regex: function regex(match) {
return new RegExp(match[0]);
}
Expand Down Expand Up @@ -85,7 +91,10 @@ var Path = (function () {
return /^url-parameter/.test(t.type);
}).length > 0;
this.hasSpatParam = this.tokens.filter(function (t) {
return /splat/.test(t.type);
return /splat$/.test(t.type);
}).length > 0;
this.hasMatrixParams = this.tokens.filter(function (t) {
return /matrix$/.test(t.type);
}).length > 0;
this.hasQueryParams = this.tokens.filter(function (t) {
return t.type === 'query-parameter';
Expand Down Expand Up @@ -182,6 +191,7 @@ var Path = (function () {
var base = this.tokens.filter(function (t) {
return t.type !== 'query-parameter';
}).map(function (t) {
if (t.type === 'url-parameter-matrix') return ';' + t.val[0] + '=' + params[t.val[0]];
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match;
}).join('');

Expand Down
14 changes: 12 additions & 2 deletions dist/umd/path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
name: 'url-parameter-splat',
pattern: /^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: /([^\?]*)/
}, {
name: 'url-parameter-matrix',
pattern: /^\;([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: function regex(match) {
return new RegExp(';' + match[1] + '=([a-zA-Z0-9-_.~]+)');
}
}, {
// Query parameter: ?param1&param2
// ?:param1&:param2
Expand All @@ -43,7 +49,7 @@
}, {
// Sub delimiters
name: 'sub-delimiter',
pattern: /^(\!|\&|\-|_|\.)/,
pattern: /^(\!|\&|\-|_|\.|;)/,
regex: function regex(match) {
return new RegExp(match[0]);
}
Expand Down Expand Up @@ -94,7 +100,10 @@
return /^url-parameter/.test(t.type);
}).length > 0;
this.hasSpatParam = this.tokens.filter(function (t) {
return /splat/.test(t.type);
return /splat$/.test(t.type);
}).length > 0;
this.hasMatrixParams = this.tokens.filter(function (t) {
return /matrix$/.test(t.type);
}).length > 0;
this.hasQueryParams = this.tokens.filter(function (t) {
return t.type === 'query-parameter';
Expand Down Expand Up @@ -191,6 +200,7 @@
var base = this.tokens.filter(function (t) {
return t.type !== 'query-parameter';
}).map(function (t) {
if (t.type === 'url-parameter-matrix') return ';' + t.val[0] + '=' + params[t.val[0]];
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match;
}).join('');

Expand Down
15 changes: 12 additions & 3 deletions modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const rules = [
pattern: /^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: /([^\?]*)/
},
{
name: 'url-parameter-matrix',
pattern: /^\;([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,
regex: match => new RegExp(';' + match[1] + '=([a-zA-Z0-9-_.~]+)')
},
{
// Query parameter: ?param1&param2
// ?:param1&:param2
Expand All @@ -28,7 +33,7 @@ const rules = [
{
// Sub delimiters
name: 'sub-delimiter',
pattern: /^(\!|\&|\-|_|\.)/,
pattern: /^(\!|\&|\-|_|\.|;)/,
regex: match => new RegExp(match[0])
},
{
Expand Down Expand Up @@ -71,7 +76,8 @@ export default class Path {
this.tokens = tokenise(path)

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.hasSpatParam = this.tokens.filter(t => /splat$/.test(t.type)).length > 0
this.hasMatrixParams = this.tokens.filter(t => /matrix$/.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
Expand Down Expand Up @@ -142,7 +148,10 @@ export default class Path {

let base = this.tokens
.filter(t => t.type !== 'query-parameter')
.map(t => /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match)
.map(t => {
if (t.type === 'url-parameter-matrix') return `;${t.val[0]}=${params[t.val[0]]}`
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match
})
.join('')

let searchPart = this.queryParams
Expand Down
15 changes: 12 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Path', function () {
path.build({ id: '123', id2: '456', id3: '789' }).should.equal('/users/profile/123-456?id3=789')
});

it('should match build paths with splat parameters', function () {
it('should match and build paths with splat parameters', function () {
var path = new Path('/users/*splat');
path.hasSpatParam.should.be.true;
// Successful match
Expand All @@ -77,19 +77,28 @@ describe('Path', function () {
path.build({ splat: 'profile/123'}).should.equal('/users/profile/123');
});

it('should match build paths with splat and url parameters', function () {
it('should match and build paths with splat and url parameters', function () {
var path = new Path('/users/*splat/view/:id');
path.hasSpatParam.should.be.true;
// Successful match
path.match('/users/profile/view/123').should.eql({ splat: 'profile', id: '123' });
path.match('/users/admin/manage/view/123').should.eql({ splat: 'admin/manage', id: '123' });
});

it('should match build paths with url, splat and query parameters', function () {
it('should match and build paths with url, splat and query parameters', function () {
var path = new Path('/:section/*splat?id');
path.hasSpatParam.should.be.true;
// Successful match
path.match('/users/profile/view?id=123').should.eql({ section: 'users', splat: 'profile/view', id: '123' });
path.build({section: 'users', splat: 'profile/view', id: '123'}).should.equal('/users/profile/view?id=123');
})

it('should match and build paths with matrix parameters', function () {
var path = new Path('/users/;section');
path.hasMatrixParams.should.be.true;
// Build path
path.build({ section: 'profile'}).should.equal('/users/;section=profile');
// Successful match
path.match('/users/;section=profile').should.eql({ section: 'profile' });
});
});

0 comments on commit 0451290

Please sign in to comment.