path-parser
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -86,2 +86,7 @@ define(['exports', 'module'], function (exports, module) { | ||
var optTrailingSlash = function optTrailingSlash(source, trailingSlash) { | ||
if (!trailingSlash) return source; | ||
return source.replace(/\/$/, '') + '(?:/)?'; | ||
}; | ||
var Path = (function () { | ||
@@ -155,4 +160,8 @@ function Path(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
// Check if exact match | ||
var match = this._urlMatch(path, new RegExp('^' + this.source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
// If no match, or no query params, no need to go further | ||
@@ -184,4 +193,8 @@ if (!match || !this.hasQueryParams) return match; | ||
value: function partialMatch(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// Check if partial match (start of given path matches regex) | ||
return this._urlMatch(path, new RegExp('^' + this.source)); | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
return this._urlMatch(path, new RegExp('^' + source)); | ||
} | ||
@@ -188,0 +201,0 @@ }, { |
@@ -89,2 +89,7 @@ 'use strict'; | ||
var optTrailingSlash = function optTrailingSlash(source, trailingSlash) { | ||
if (!trailingSlash) return source; | ||
return source.replace(/\/$/, '') + '(?:/)?'; | ||
}; | ||
var Path = (function () { | ||
@@ -158,4 +163,8 @@ function Path(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
// Check if exact match | ||
var match = this._urlMatch(path, new RegExp('^' + this.source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
// If no match, or no query params, no need to go further | ||
@@ -187,4 +196,8 @@ if (!match || !this.hasQueryParams) return match; | ||
value: function partialMatch(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// Check if partial match (start of given path matches regex) | ||
return this._urlMatch(path, new RegExp('^' + this.source)); | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
return this._urlMatch(path, new RegExp('^' + source)); | ||
} | ||
@@ -191,0 +204,0 @@ }, { |
@@ -98,2 +98,7 @@ (function (global, factory) { | ||
var optTrailingSlash = function optTrailingSlash(source, trailingSlash) { | ||
if (!trailingSlash) return source; | ||
return source.replace(/\/$/, '') + '(?:/)?'; | ||
}; | ||
var Path = (function () { | ||
@@ -167,4 +172,8 @@ function Path(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
// Check if exact match | ||
var match = this._urlMatch(path, new RegExp('^' + this.source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '?.*$' : '$'))); | ||
// If no match, or no query params, no need to go further | ||
@@ -196,4 +205,8 @@ if (!match || !this.hasQueryParams) return match; | ||
value: function partialMatch(path) { | ||
var trailingSlash = arguments[1] === undefined ? 0 : arguments[1]; | ||
// Check if partial match (start of given path matches regex) | ||
return this._urlMatch(path, new RegExp('^' + this.source)); | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
var source = optTrailingSlash(this.source, trailingSlash); | ||
return this._urlMatch(path, new RegExp('^' + source)); | ||
} | ||
@@ -200,0 +213,0 @@ }, { |
@@ -77,2 +77,7 @@ let defaultOrConstrained = (match) => { | ||
let optTrailingSlash = (source, trailingSlash) => { | ||
if (!trailingSlash) return source | ||
return source.replace(/\/$/, '') + '(?:\/)?' | ||
} | ||
export default class Path { | ||
@@ -121,5 +126,7 @@ constructor(path) { | ||
match(path) { | ||
match(path, trailingSlash = 0) { | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
let source = optTrailingSlash(this.source, trailingSlash) | ||
// Check if exact match | ||
let match = this._urlMatch(path, new RegExp('^' + this.source + (this.hasQueryParams ? '\?.*$' : '$'))) | ||
let match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\?.*$' : '$'))) | ||
// If no match, or no query params, no need to go further | ||
@@ -147,5 +154,7 @@ if (!match || !this.hasQueryParams) return match | ||
partialMatch(path) { | ||
partialMatch(path, trailingSlash = 0) { | ||
// Check if partial match (start of given path matches regex) | ||
return this._urlMatch(path, new RegExp('^' + this.source)) | ||
// trailingSlash: falsy => non optional, truthy => optional | ||
let source = optTrailingSlash(this.source, trailingSlash) | ||
return this._urlMatch(path, new RegExp('^' + source)) | ||
} | ||
@@ -152,0 +161,0 @@ |
{ | ||
"name": "path-parser", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A small utility to parse, match and generate paths", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/path-parser.js", |
@@ -24,3 +24,3 @@ [![npm version](https://badge.fury.io/js/path-parser.svg)](http://badge.fury.io/js/path-parser) | ||
p.partialMatch('/users/profile/00123/orders') // => {id: "00123"} | ||
p.partialMatch('/profile/00123/orders') // => false | ||
p.partialMatch('/profile/00123/orders') // => null | ||
// Building | ||
@@ -55,2 +55,13 @@ p.build({id: '00123'}) // => "users/profile/00123" | ||
## Optional trailing slashes | ||
When using `.match()` or `.partialMatch()`, you can path a second argument. If truthy, it will make trailing slashes optional. | ||
```javascript | ||
var path = new Path('/my-path'); | ||
path.match('/my-path/') // => null | ||
path.match('/my-path/', true) // => {} | ||
``` | ||
## Related modules | ||
@@ -57,0 +68,0 @@ |
@@ -128,2 +128,14 @@ 'use strict'; | ||
}); | ||
it('should match paths with optional trailing slashes', function () { | ||
var path = new Path('/my-path'); | ||
should.not.exist(path.match('/my-path/')); | ||
path.match('/my-path/', true).should.eql({}); | ||
path.match('/my-path/', 1).should.eql({}); | ||
path = new Path('/my-path/'); | ||
should.not.exist(path.match('/my-path')); | ||
path.match('/my-path', true).should.eql({}); | ||
path.match('/my-path', 1).should.eql({}); | ||
}); | ||
}); |
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
47742
967
69