path-parser
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -23,2 +23,8 @@ 'use strict'; | ||
}, { | ||
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¶m2 | ||
@@ -38,3 +44,3 @@ // ?:param1&:param2 | ||
name: 'sub-delimiter', | ||
pattern: /^(\!|\&|\-|_|\.)/, | ||
pattern: /^(\!|\&|\-|_|\.|;)/, | ||
regex: function regex(match) { | ||
@@ -90,4 +96,7 @@ return new RegExp(match[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) { | ||
@@ -132,3 +141,3 @@ return t.type === 'query-parameter'; | ||
var match = path.match(regex); | ||
if (!match) return false;else if (!this.urlParams.length) return {}; | ||
if (!match) return null;else if (!this.urlParams.length) return {}; | ||
// Reduce named params to key-value pairs | ||
@@ -168,3 +177,3 @@ return match.slice(1, this.urlParams.length + 1).reduce(function (params, m, i) { | ||
return false; | ||
return null; | ||
} | ||
@@ -190,2 +199,3 @@ }, { | ||
}).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; | ||
@@ -192,0 +202,0 @@ }).join(''); |
@@ -32,2 +32,8 @@ (function (global, factory) { | ||
}, { | ||
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¶m2 | ||
@@ -47,3 +53,3 @@ // ?:param1&:param2 | ||
name: 'sub-delimiter', | ||
pattern: /^(\!|\&|\-|_|\.)/, | ||
pattern: /^(\!|\&|\-|_|\.|;)/, | ||
regex: function regex(match) { | ||
@@ -99,4 +105,7 @@ return new RegExp(match[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) { | ||
@@ -141,3 +150,3 @@ return t.type === 'query-parameter'; | ||
var match = path.match(regex); | ||
if (!match) return false;else if (!this.urlParams.length) return {}; | ||
if (!match) return null;else if (!this.urlParams.length) return {}; | ||
// Reduce named params to key-value pairs | ||
@@ -177,3 +186,3 @@ return match.slice(1, this.urlParams.length + 1).reduce(function (params, m, i) { | ||
return false; | ||
return null; | ||
} | ||
@@ -199,2 +208,3 @@ }, { | ||
}).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; | ||
@@ -201,0 +211,0 @@ }).join(''); |
@@ -16,2 +16,7 @@ const rules = [ | ||
{ | ||
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¶m2 | ||
@@ -32,3 +37,3 @@ // ?:param1&:param2 | ||
name: 'sub-delimiter', | ||
pattern: /^(\!|\&|\-|_|\.)/, | ||
pattern: /^(\!|\&|\-|_|\.|;)/, | ||
regex: match => new RegExp(match[0]) | ||
@@ -76,3 +81,4 @@ }, | ||
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 | ||
@@ -102,3 +108,3 @@ // Extract named parameters from tokens | ||
let match = path.match(regex) | ||
if (!match) return false | ||
if (!match) return null | ||
else if (!this.urlParams.length) return {} | ||
@@ -135,3 +141,3 @@ // Reduce named params to key-value pairs | ||
return false | ||
return null | ||
} | ||
@@ -150,3 +156,6 @@ | ||
.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('') | ||
@@ -153,0 +162,0 @@ |
{ | ||
"name": "path-parser", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A small utility to parse paths", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/path-parser.js", |
@@ -35,5 +35,5 @@ 'use strict'; | ||
// Unsuccessful match | ||
path.match('/users/details/123-abc').should.be.false; | ||
path.match('/users/details/123-abc.html').should.be.false; | ||
path.match('/users/profile/123-abc.html?what').should.be.false; | ||
should.not.exist(path.match('/users/details/123-abc')); | ||
should.not.exist(path.match('/users/details/123-abc.html')); | ||
should.not.exist(path.match('/users/profile/123-abc.html?what')); | ||
@@ -52,4 +52,4 @@ path.build({ id: '123', id2: 'abc' }).should.equal('/users/profile/123-abc.html'); | ||
// Unsuccessful match | ||
path.match('/users?offset=31').should.be.false; | ||
path.match('/users?limit=15').should.be.false; | ||
should.not.exist(path.match('/users?offset=31')); | ||
should.not.exist(path.match('/users?limit=15')); | ||
@@ -65,5 +65,5 @@ path.build({ offset: 31, limit: 15 }).should.equal('/users?offset=31&limit=15') | ||
path.partialMatch('/users/profile/123-456').should.eql({ id: '123', id2: '456' }); | ||
// Unsuccessful match | ||
path.match('/users/details/123-456').should.be.false; | ||
path.match('/users/profile/123-456?id3=789&id4=000').should.be.false; | ||
// Un,successful match | ||
should.not.exist(path.match('/users/details/123-456')); | ||
should.not.exist(path.match('/users/profile/123-456?id3=789&id4=000')); | ||
@@ -73,3 +73,3 @@ 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'); | ||
@@ -84,3 +84,3 @@ path.hasSpatParam.should.be.true; | ||
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'); | ||
@@ -93,3 +93,3 @@ path.hasSpatParam.should.be.true; | ||
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'); | ||
@@ -101,2 +101,11 @@ path.hasSpatParam.should.be.true; | ||
}) | ||
it('should match and build paths with matrix parameters', function () { | ||
var path = new Path('/users/;section;id'); | ||
path.hasMatrixParams.should.be.true; | ||
// Build path | ||
path.build({ section: 'profile', id: '123'}).should.equal('/users/;section=profile;id=123'); | ||
// Successful match | ||
path.match('/users/;section=profile;id=123').should.eql({ section: 'profile', id: '123' }); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31147
652
0