Socket
Socket
Sign inDemoInstall

path-parser

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-parser - npm Package Compare versions

Comparing version 0.5.0 to 1.0.0

.babelrc

376

CHANGELOG.md

@@ -0,3 +1,9 @@

<a name="1.0.0"></a>
# [1.0.0](https://github.com/troch/path-parser/compare/v0.5.0...v1.0.0) (2016-01-05)
<a name="0.5.0"></a>
# 0.5.0 (2015-12-08)
# [0.5.0](https://github.com/troch/path-parser/compare/v0.4.4...v0.5.0) (2015-12-08)

@@ -12,3 +18,3 @@

<a name="0.4.4"></a>
## 0.4.4 (2015-11-24)
## [0.4.4](https://github.com/troch/path-parser/compare/v0.4.3...v0.4.4) (2015-11-24)

@@ -23,3 +29,3 @@

<a name="0.4.3"></a>
## 0.4.3 (2015-10-11)
## [0.4.3](https://github.com/troch/path-parser/compare/v0.4.2...v0.4.3) (2015-10-11)

@@ -30,3 +36,3 @@

<a name="0.4.2"></a>
## 0.4.2 (2015-10-07)
## [0.4.2](https://github.com/troch/path-parser/compare/v0.4.1...v0.4.2) (2015-10-07)

@@ -41,3 +47,3 @@

<a name="0.4.1"></a>
## 0.4.1 (2015-10-07)
## [0.4.1](https://github.com/troch/path-parser/compare/v0.4.0...v0.4.1) (2015-10-07)

@@ -52,3 +58,3 @@

<a name="0.4.0"></a>
# 0.4.0 (2015-10-07)
# [0.4.0](https://github.com/troch/path-parser/compare/v0.3.2...v0.4.0) (2015-10-07)

@@ -64,3 +70,3 @@

<a name="0.3.2"></a>
## 0.3.2 (2015-09-04)
## [0.3.2](https://github.com/troch/path-parser/compare/v0.3.1...v0.3.2) (2015-09-04)

@@ -74,14 +80,4 @@

<a name="0.3.2"></a>
## 0.3.2 (2015-09-04)
### Bug Fixes
* make query parameters optional when matching and match them on partial match ([303b240](https://github.com/troch/path-parser/commit/303b240))
<a name="0.3.1"></a>
## 0.3.1 (2015-09-04)
## [0.3.1](https://github.com/troch/path-parser/compare/v0.3.0...v0.3.1) (2015-09-04)

@@ -96,3 +92,3 @@

<a name="0.3.0"></a>
# 0.3.0 (2015-09-03)
# [0.3.0](https://github.com/troch/path-parser/compare/v0.2.4...v0.3.0) (2015-09-03)

@@ -112,166 +108,3 @@

* .build() now takes an options object as a second parameter
Issue router5/router5#19
<a name="0.2.4"></a>
## 0.2.4 (2015-09-01)
### Features
* allow optional trailing slash for '/' ([570a0c4](https://github.com/troch/path-parser/commit/570a0c4))
<a name="0.2.3"></a>
## 0.2.3 (2015-08-20)
### Bug Fixes
* better escape regular expression special chars so built source matches regexp so ([21d30b7](https://github.com/troch/path-parser/commit/21d30b7))
<a name="0.2.2"></a>
## 0.2.2 (2015-08-19)
### Bug Fixes
* escape special characters properly in regular expressions ([f573957](https://github.com/troch/path-parser/commit/f573957))
<a name="0.2.1"></a>
## 0.2.1 (2015-08-19)
### Bug Fixes
* don't apply optional trailing slashes on paths === / ([36e0180](https://github.com/troch/path-parser/commit/36e0180))
<a name="0.2.0"></a>
# 0.2.0 (2015-08-19)
### Features
* support optional trailing slashes ([6785886](https://github.com/troch/path-parser/commit/6785886))
<a name="0.1.1"></a>
## 0.1.1 (2015-07-22)
<a name="0.1.0"></a>
# 0.1.0 (2015-07-06)
### Features
* add matrix and url parameter constraints ([a567ba1](https://github.com/troch/path-parser/commit/a567ba1))
<a name="0.0.7"></a>
## 0.0.7 (2015-07-01)
### Features
* support matrix parameters ([0451290](https://github.com/troch/path-parser/commit/0451290))
<a name="0.0.6"></a>
## 0.0.6 (2015-06-30)
<a name="0.0.5"></a>
## 0.0.5 (2015-06-30)
<a name="0.0.4"></a>
## 0.0.4 (2015-06-28)
### Bug Fixes
* fix bug when multiple query params ([880bae0](https://github.com/troch/path-parser/commit/880bae0))
### Features
* improve tokenisation and tests ([5b9e1fe](https://github.com/troch/path-parser/commit/5b9e1fe))
<a name="0.0.3"></a>
## 0.0.3 (2015-06-26)
### Bug Fixes
* fix path building with splats ([7bd7d74](https://github.com/troch/path-parser/commit/7bd7d74))
<a name="0.0.2"></a>
## 0.0.2 (2015-06-25)
### Features
* add splat support with query params ([96bcd6d](https://github.com/troch/path-parser/commit/96bcd6d))
<a name="0.0.1"></a>
## 0.0.1 (2015-06-25)
### Features
* add spat param flag ([b77174a](https://github.com/troch/path-parser/commit/b77174a))
* add support for query parameters ([4ee86cf](https://github.com/troch/path-parser/commit/4ee86cf))
* add support for splats ([e346bbf](https://github.com/troch/path-parser/commit/e346bbf))
<a name="0.3.1"></a>
## 0.3.1 (2015-09-04)
### Bug Fixes
* better building and matching of query params ([5e8f881](https://github.com/troch/path-parser/commit/5e8f881))
<a name="0.3.0"></a>
# 0.3.0 (2015-09-03)
### Bug Fixes
* build a path without all query parameters ([208fd8a](https://github.com/troch/path-parser/commit/208fd8a))
### Features
* build paths without search part ([c9973be](https://github.com/troch/path-parser/commit/c9973be))
### BREAKING CHANGES
* .build() now takes an options object as a second parameter
Issue router5/router5#19

@@ -282,3 +115,3 @@

<a name="0.2.4"></a>
## 0.2.4 (2015-09-01)
## [0.2.4](https://github.com/troch/path-parser/compare/v0.2.3...v0.2.4) (2015-09-01)

@@ -293,3 +126,3 @@

<a name="0.2.3"></a>
## 0.2.3 (2015-08-20)
## [0.2.3](https://github.com/troch/path-parser/compare/v0.2.2...v0.2.3) (2015-08-20)

@@ -304,3 +137,3 @@

<a name="0.2.2"></a>
## 0.2.2 (2015-08-19)
## [0.2.2](https://github.com/troch/path-parser/compare/v0.2.1...v0.2.2) (2015-08-19)

@@ -315,3 +148,3 @@

<a name="0.2.1"></a>
## 0.2.1 (2015-08-19)
## [0.2.1](https://github.com/troch/path-parser/compare/v0.2.0...v0.2.1) (2015-08-19)

@@ -326,3 +159,3 @@

<a name="0.2.0"></a>
# 0.2.0 (2015-08-19)
# [0.2.0](https://github.com/troch/path-parser/compare/v0.1.1...v0.2.0) (2015-08-19)

@@ -337,3 +170,3 @@

<a name="0.1.1"></a>
## 0.1.1 (2015-07-22)
## [0.1.1](https://github.com/troch/path-parser/compare/v0.1.0...v0.1.1) (2015-07-22)

@@ -344,3 +177,3 @@

<a name="0.1.0"></a>
# 0.1.0 (2015-07-06)
# [0.1.0](https://github.com/troch/path-parser/compare/v0.0.7...v0.1.0) (2015-07-06)

@@ -355,3 +188,3 @@

<a name="0.0.7"></a>
## 0.0.7 (2015-07-01)
## [0.0.7](https://github.com/troch/path-parser/compare/v0.0.6...v0.0.7) (2015-07-01)

@@ -366,3 +199,3 @@

<a name="0.0.6"></a>
## 0.0.6 (2015-06-30)
## [0.0.6](https://github.com/troch/path-parser/compare/v0.0.5...v0.0.6) (2015-06-30)

@@ -373,3 +206,3 @@

<a name="0.0.5"></a>
## 0.0.5 (2015-06-30)
## [0.0.5](https://github.com/troch/path-parser/compare/v0.0.4...v0.0.5) (2015-06-30)

@@ -380,3 +213,3 @@

<a name="0.0.4"></a>
## 0.0.4 (2015-06-28)
## [0.0.4](https://github.com/troch/path-parser/compare/v0.0.3...v0.0.4) (2015-06-28)

@@ -395,3 +228,3 @@

<a name="0.0.3"></a>
## 0.0.3 (2015-06-26)
## [0.0.3](https://github.com/troch/path-parser/compare/v0.0.2...v0.0.3) (2015-06-26)

@@ -406,3 +239,3 @@

<a name="0.0.2"></a>
## 0.0.2 (2015-06-25)
## [0.0.2](https://github.com/troch/path-parser/compare/v0.0.1...v0.0.2) (2015-06-25)

@@ -417,3 +250,3 @@

<a name="0.0.1"></a>
## 0.0.1 (2015-06-25)
## [0.0.1](https://github.com/troch/path-parser/compare/4ee86cf...v0.0.1) (2015-06-25)

@@ -429,152 +262,1 @@

<a name="0.3.0"></a>
# 0.3.0 (2015-09-03)
### Bug Fixes
* build a path without all query parameters ([208fd8a](https://github.com/troch/path-parser/commit/208fd8a))
### Features
* build paths without search part ([c9973be](https://github.com/troch/path-parser/commit/c9973be))
### BREAKING CHANGES
* .build() now takes an options object as a second parameter
Issue router5/router5#19
<a name="0.2.4"></a>
## 0.2.4 (2015-09-01)
### Features
* allow optional trailing slash for '/' ([570a0c4](https://github.com/troch/path-parser/commit/570a0c4))
<a name="0.2.3"></a>
## 0.2.3 (2015-08-20)
### Bug Fixes
* better escape regular expression special chars so built source matches regexp so ([21d30b7](https://github.com/troch/path-parser/commit/21d30b7))
<a name="0.2.2"></a>
## 0.2.2 (2015-08-19)
### Bug Fixes
* escape special characters properly in regular expressions ([f573957](https://github.com/troch/path-parser/commit/f573957))
<a name="0.2.1"></a>
## 0.2.1 (2015-08-19)
### Bug Fixes
* don't apply optional trailing slashes on paths === / ([36e0180](https://github.com/troch/path-parser/commit/36e0180))
<a name="0.2.0"></a>
# 0.2.0 (2015-08-19)
### Features
* support optional trailing slashes ([6785886](https://github.com/troch/path-parser/commit/6785886))
<a name="0.1.1"></a>
## 0.1.1 (2015-07-22)
<a name="0.1.0"></a>
# 0.1.0 (2015-07-06)
### Features
* add matrix and url parameter constraints ([a567ba1](https://github.com/troch/path-parser/commit/a567ba1))
<a name="0.0.7"></a>
## 0.0.7 (2015-07-01)
### Features
* support matrix parameters ([0451290](https://github.com/troch/path-parser/commit/0451290))
<a name="0.0.6"></a>
## 0.0.6 (2015-06-30)
<a name="0.0.5"></a>
## 0.0.5 (2015-06-30)
<a name="0.0.4"></a>
## 0.0.4 (2015-06-28)
### Bug Fixes
* fix bug when multiple query params ([880bae0](https://github.com/troch/path-parser/commit/880bae0))
### Features
* improve tokenisation and tests ([5b9e1fe](https://github.com/troch/path-parser/commit/5b9e1fe))
<a name="0.0.3"></a>
## 0.0.3 (2015-06-26)
### Bug Fixes
* fix path building with splats ([7bd7d74](https://github.com/troch/path-parser/commit/7bd7d74))
<a name="0.0.2"></a>
## 0.0.2 (2015-06-25)
### Features
* add splat support with query params ([96bcd6d](https://github.com/troch/path-parser/commit/96bcd6d))
<a name="0.0.1"></a>
## 0.0.1 (2015-06-25)
### Features
* add spat param flag ([b77174a](https://github.com/troch/path-parser/commit/b77174a))
* add support for query parameters ([4ee86cf](https://github.com/troch/path-parser/commit/4ee86cf))
* add support for splats ([e346bbf](https://github.com/troch/path-parser/commit/e346bbf))

@@ -1,8 +0,29 @@

define(['exports', 'module'], function (exports, module) {
'use strict';
define('Path', function () { 'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var babelHelpers = {};
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function babelHelpers_classCallCheck (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var babelHelpers_createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
var defaultOrConstrained = function defaultOrConstrained(match) {

@@ -139,3 +160,3 @@ return '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')';

var Path = (function () {
_createClass(Path, null, [{
babelHelpers_createClass(Path, null, [{
key: 'createPath',

@@ -153,3 +174,3 @@ value: function createPath(path) {

function Path(path) {
_classCallCheck(this, Path);
babelHelpers_classCallCheck(this, Path);

@@ -215,3 +236,3 @@ if (!path) throw new Error('Please supply a path');

_createClass(Path, [{
babelHelpers_createClass(Path, [{
key: '_urlMatch',

@@ -239,6 +260,6 @@ value: function _urlMatch(path, regex) {

// Check if exact match
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
var matched = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
// If no match, or no query params, no need to go further
if (!match || !this.hasQueryParams) return match;
if (!matched || !this.hasQueryParams) return matched;
// Extract query params

@@ -253,6 +274,6 @@ var queryParams = parseQueryParams(path);

Object.keys(queryParams).forEach(function (p) {
return match[p] = queryParams[p];
return matched[p] = queryParams[p];
});
return match;
return matched;
}

@@ -335,7 +356,7 @@

}]);
return Path;
})();
module.exports = Path;
return Path;
});
'use strict';
Object.defineProperty(exports, '__esModule', {
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var defaultOrConstrained = function defaultOrConstrained(match) {

@@ -239,6 +239,6 @@ return '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')';

// Check if exact match
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
var matched = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
// If no match, or no query params, no need to go further
if (!match || !this.hasQueryParams) return match;
if (!matched || !this.hasQueryParams) return matched;
// Extract query params

@@ -253,6 +253,6 @@ var queryParams = parseQueryParams(path);

Object.keys(queryParams).forEach(function (p) {
return match[p] = queryParams[p];
return matched[p] = queryParams[p];
});
return match;
return matched;
}

@@ -339,3 +339,3 @@

exports['default'] = Path;
module.exports = exports['default'];
exports.default = Path;
module.exports = exports['default'];
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'module'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module);
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod);
global.Path = mod.exports;
}
})(this, function (exports, module) {
'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define('Path', factory) :
(global.Path = factory());
}(this, function () { 'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var babelHelpers = {};
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function babelHelpers_classCallCheck (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var babelHelpers_createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
var defaultOrConstrained = function defaultOrConstrained(match) {

@@ -151,3 +164,3 @@ return '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')';

var Path = (function () {
_createClass(Path, null, [{
babelHelpers_createClass(Path, null, [{
key: 'createPath',

@@ -165,3 +178,3 @@ value: function createPath(path) {

function Path(path) {
_classCallCheck(this, Path);
babelHelpers_classCallCheck(this, Path);

@@ -227,3 +240,3 @@ if (!path) throw new Error('Please supply a path');

_createClass(Path, [{
babelHelpers_createClass(Path, [{
key: '_urlMatch',

@@ -251,6 +264,6 @@ value: function _urlMatch(path, regex) {

// Check if exact match
var match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
var matched = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
// If no match, or no query params, no need to go further
if (!match || !this.hasQueryParams) return match;
if (!matched || !this.hasQueryParams) return matched;
// Extract query params

@@ -265,6 +278,6 @@ var queryParams = parseQueryParams(path);

Object.keys(queryParams).forEach(function (p) {
return match[p] = queryParams[p];
return matched[p] = queryParams[p];
});
return match;
return matched;
}

@@ -347,7 +360,7 @@

}]);
return Path;
})();
module.exports = Path;
});
return Path;
}));

@@ -1,4 +0,3 @@

let defaultOrConstrained = (match) => {
return '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')'
}
const defaultOrConstrained = (match) =>
'(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')';

@@ -56,10 +55,10 @@ const rules = [

}
]
];
const tokenise = (str, tokens = []) => {
// Look for a matching rule
let matched =
const matched =
rules.some(rule => {
let match = str.match(rule.pattern)
if (!match) return false
let match = str.match(rule.pattern);
if (!match) return false;

@@ -72,19 +71,19 @@ tokens.push({

regex: rule.regex instanceof Function ? rule.regex(match) : rule.regex
})
});
if (match[0].length < str.length) tokens = tokenise(str.substr(match[0].length), tokens)
return true
})
if (match[0].length < str.length) tokens = tokenise(str.substr(match[0].length), tokens);
return true;
});
// If no rules matched, throw an error (possible malformed path)
if (!matched) {
throw new Error('Could not parse path.')
throw new Error('Could not parse path.');
}
// Return tokens
return tokens
return tokens;
};
const optTrailingSlash = (source, trailingSlash) => {
if (!trailingSlash) return source
return source.replace(/\\\/$/, '') + '(?:\\/)?'
if (!trailingSlash) return source;
return source.replace(/\\\/$/, '') + '(?:\\/)?';
};

@@ -108,8 +107,9 @@

const parseQueryParams = path => {
let searchPart = path.split('?')[1]
if (!searchPart) return {}
let searchPart = path.split('?')[1];
if (!searchPart) return {};
return searchPart.split('&')
.map(_ => _.split('='))
.reduce((obj, m) => appendQueryParam(obj, m[0], m[1] ? decodeURIComponent(m[1]) : m[1]), {})
return searchPart
.split('&')
.map(_ => _.split('='))
.reduce((obj, m) => appendQueryParam(obj, m[0], m[1] ? decodeURIComponent(m[1]) : m[1]), {});
};

@@ -123,18 +123,18 @@

static createPath(path) {
return new Path(path)
return new Path(path);
}
static serialise(key, val) {
return serialise(key, val)
return serialise(key, val);
}
constructor(path) {
if (!path) throw new Error('Please supply a path')
this.path = path
this.tokens = tokenise(path)
if (!path) throw new Error('Please supply a path');
this.path = 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.hasMatrixParams = this.tokens.filter(t => /matrix$/.test(t.type)).length > 0
this.hasQueryParams = this.tokens.filter(t => /^query-parameter/.test(t.type)).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.hasMatrixParams = this.tokens.filter(t => /matrix$/.test(t.type)).length > 0;
this.hasQueryParams = this.tokens.filter(t => /^query-parameter/.test(t.type)).length > 0;
// Extract named parameters from tokens

@@ -145,3 +145,3 @@ this.urlParams = !this.hasUrlParams ? [] : this.tokens

// Flatten
.reduce((r, v) => r.concat(v))
.reduce((r, v) => r.concat(v));
// Query params

@@ -164,15 +164,15 @@ this.queryParams = !this.hasQueryParams ? [] : this.tokens

.map(r => r.regex.source)
.join('')
.join('');
}
_urlMatch(path, regex) {
let match = path.match(regex)
if (!match) return null
else if (!this.urlParams.length) return {}
let match = path.match(regex);
if (!match) return null;
else if (!this.urlParams.length) return {};
// Reduce named params to key-value pairs
return match.slice(1, this.urlParams.length + 1)
.reduce((params, m, i) => {
params[this.urlParams[i]] = m
return params
}, {})
params[this.urlParams[i]] = m;
return params;
}, {});
}

@@ -182,12 +182,12 @@

// trailingSlash: falsy => non optional, truthy => optional
let source = optTrailingSlash(this.source, trailingSlash)
const source = optTrailingSlash(this.source, trailingSlash);
// Check if exact match
let match = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')))
const matched = this._urlMatch(path, new RegExp('^' + source + (this.hasQueryParams ? '\\?.*$' : '$')));
// If no match, or no query params, no need to go further
if (!match || !this.hasQueryParams) return match
if (!matched || !this.hasQueryParams) return matched;
// Extract query params
let queryParams = parseQueryParams(path)
let queryParams = parseQueryParams(path);
let unexpectedQueryParams = Object.keys(queryParams)
.filter(p => this.queryParams.concat(this.queryParamsBr).indexOf(p) === -1 )
.filter(p => this.queryParams.concat(this.queryParamsBr).indexOf(p) === -1 );

@@ -197,8 +197,8 @@ if (unexpectedQueryParams.length === 0) {

Object.keys(queryParams)
.forEach(p => match[p] = queryParams[p])
.forEach(p => matched[p] = queryParams[p]);
return match
return matched;
}
return null
return null;
}

@@ -209,16 +209,16 @@

// trailingSlash: falsy => non optional, truthy => optional
let source = optTrailingSlash(this.source, trailingSlash)
let match = this._urlMatch(path, new RegExp('^' + source))
let source = optTrailingSlash(this.source, trailingSlash);
let match = this._urlMatch(path, new RegExp('^' + source));
if (!match) return match
if (!match) return match;
if (!this.hasQueryParams) return match
if (!this.hasQueryParams) return match;
let queryParams = parseQueryParams(path)
let queryParams = parseQueryParams(path);
Object.keys(queryParams)
.filter(p => this.queryParams.concat(this.queryParamsBr).indexOf(p) >= 0)
.forEach(p => appendQueryParam(match, p, queryParams[p]))
.forEach(p => appendQueryParam(match, p, queryParams[p]));
return match
return match;
}

@@ -228,3 +228,3 @@

// Check all params are provided (not search parameters which are optional)
if (this.urlParams.some(p => params[p] === undefined)) throw new Error('Missing parameters')
if (this.urlParams.some(p => params[p] === undefined)) throw new Error('Missing parameters');

@@ -235,3 +235,3 @@ // Check constraints

.filter(t => /^url-parameter/.test(t.type) && !/-splat$/.test(t.type))
.every(t => new RegExp('^' + defaultOrConstrained(t.otherVal[0]) + '$').test(params[t.val]))
.every(t => new RegExp('^' + defaultOrConstrained(t.otherVal[0]) + '$').test(params[t.val]));

@@ -244,18 +244,18 @@ if (!constraintsPassed) throw new Error('Some parameters are of invalid format');

.map(t => {
if (t.type === 'url-parameter-matrix') return `;${t.val}=${params[t.val[0]]}`
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match
if (t.type === 'url-parameter-matrix') return `;${t.val}=${params[t.val[0]]}`;
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match;
})
.join('')
.join('');
if (opts.ignoreSearch) return base
if (opts.ignoreSearch) return base;
const queryParams = this.queryParams.concat(this.queryParamsBr.map(p => p + '[]'));
let searchPart = queryParams
const searchPart = queryParams
.filter(p => Object.keys(params).indexOf(withoutBrackets(p)) !== -1)
.map(p => serialise(p, params[withoutBrackets(p)]))
.join('&')
.join('&');
return base + (searchPart ? '?' + searchPart : '')
return base + (searchPart ? '?' + searchPart : '');
}
}
{
"name": "path-parser",
"version": "0.5.0",
"version": "1.0.0",
"description": "A small utility to parse, match and generate paths",

@@ -8,6 +8,11 @@ "main": "dist/commonjs/path-parser.js",

"scripts": {
"test": "node ./node_modules/mocha/bin/_mocha",
"test-cover": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha",
"build": "node ./scripts/build.js",
"clog": "node scripts/changelog.js"
"test": "_mocha",
"test-cover": "istanbul cover _mocha",
"lint": "eslint modules/*.js",
"build:amd": "BABEL_ENV=rollup rollup -c rollup.config.js --format amd",
"build:umd": "BABEL_ENV=rollup rollup -c rollup.config.js --format umd",
"build:cjs": "mkdirp dist/commonjs && babel modules/Path.js -o dist/commonjs/path-parser.js",
"clean": "rimraf dist",
"build": "npm run clean && npm run build:amd && npm run build:umd && npm run build:cjs",
"clog": "conventional-changelog -p angular -i CHANGELOG.md -w"
},

@@ -34,13 +39,23 @@ "repository": {

"devDependencies": {
"async": "^1.3.0",
"babel": "^5.5.8",
"conventional-changelog": "^0.2.0",
"babel-core": "^6.3.26",
"babel-eslint": "^4.1.6",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-external-helpers-2": "^6.3.13",
"babel-plugin-transform-class-properties": "^6.3.13",
"babel-plugin-transform-export-extensions": "^6.3.13",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-preset-es2015": "^6.3.13",
"babel-preset-es2015-rollup": "^1.0.0",
"conventional-changelog": "^0.5.3",
"coveralls": "^2.11.2",
"eslint": "^1.10.3",
"istanbul": "^0.3.16",
"mkdirp": "^0.5.1",
"mocha": "^2.2.5",
"rimraf": "^2.5.0",
"rollup": "^0.24.0",
"rollup-plugin-babel": "^2.3.5",
"should": "^6.0.3",
"through2": "^2.0.0",
"vinyl-fs": "^1.0.0",
"vinyl-source-stream2": "^0.1.1"
"yargs": "^3.31.0"
}
}

@@ -16,5 +16,5 @@ [![npm version](https://badge.fury.io/js/path-parser.svg)](http://badge.fury.io/js/path-parser)

```javascript
var Path = require('path-parser');
import Path from 'path-parser';
// Defining a new path
var p = new Path('/users/profile/:id');
const p = new Path('/users/profile/:id');
// Matching

@@ -33,7 +33,13 @@ p.match('/users/profile/00123') // => {id: "00123"}

```javascript
var Path = require('path-parser');
import Path from 'path-parser';
var p = Path.createPath('/users/profile/:id');
const p = Path.createPath('/users/profile/:id');
```
ES5:
```javascript
var Path = require('path-parser').default;
```
## Defining parameters

@@ -40,0 +46,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc