path-parser
Advanced tools
Comparing version 0.2.4 to 0.3.0
@@ -207,11 +207,11 @@ define(['exports', 'module'], function (exports, module) { | ||
var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var ignoreConstraints = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? { ignoreConstraints: false, ignoreSearch: false } : arguments[1]; | ||
// Check all params are provided (not search parameters which are optional) | ||
if (!this.params.every(function (p) { | ||
return params[p] !== undefined; | ||
if (this.urlParams.some(function (p) { | ||
return params[p] === undefined; | ||
})) throw new Error('Missing parameters'); | ||
// Check constraints | ||
if (!ignoreConstraints) { | ||
if (!opts.ignoreConstraints) { | ||
var constraintsPassed = this.tokens.filter(function (t) { | ||
@@ -235,3 +235,7 @@ return (/^url-parameter/.test(t.type) && !/-splat$/.test(t.type) | ||
var searchPart = this.queryParams.map(function (p) { | ||
if (opts.ignoreSearch) return base; | ||
var searchPart = this.queryParams.filter(function (p) { | ||
return Object.keys(params).indexOf(p) !== -1; | ||
}).map(function (p) { | ||
return p + '=' + params[p]; | ||
@@ -238,0 +242,0 @@ }).join('&'); |
@@ -210,11 +210,11 @@ 'use strict'; | ||
var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var ignoreConstraints = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? { ignoreConstraints: false, ignoreSearch: false } : arguments[1]; | ||
// Check all params are provided (not search parameters which are optional) | ||
if (!this.params.every(function (p) { | ||
return params[p] !== undefined; | ||
if (this.urlParams.some(function (p) { | ||
return params[p] === undefined; | ||
})) throw new Error('Missing parameters'); | ||
// Check constraints | ||
if (!ignoreConstraints) { | ||
if (!opts.ignoreConstraints) { | ||
var constraintsPassed = this.tokens.filter(function (t) { | ||
@@ -238,3 +238,7 @@ return (/^url-parameter/.test(t.type) && !/-splat$/.test(t.type) | ||
var searchPart = this.queryParams.map(function (p) { | ||
if (opts.ignoreSearch) return base; | ||
var searchPart = this.queryParams.filter(function (p) { | ||
return Object.keys(params).indexOf(p) !== -1; | ||
}).map(function (p) { | ||
return p + '=' + params[p]; | ||
@@ -241,0 +245,0 @@ }).join('&'); |
@@ -219,11 +219,11 @@ (function (global, factory) { | ||
var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var ignoreConstraints = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? { ignoreConstraints: false, ignoreSearch: false } : arguments[1]; | ||
// Check all params are provided (not search parameters which are optional) | ||
if (!this.params.every(function (p) { | ||
return params[p] !== undefined; | ||
if (this.urlParams.some(function (p) { | ||
return params[p] === undefined; | ||
})) throw new Error('Missing parameters'); | ||
// Check constraints | ||
if (!ignoreConstraints) { | ||
if (!opts.ignoreConstraints) { | ||
var constraintsPassed = this.tokens.filter(function (t) { | ||
@@ -247,3 +247,7 @@ return (/^url-parameter/.test(t.type) && !/-splat$/.test(t.type) | ||
var searchPart = this.queryParams.map(function (p) { | ||
if (opts.ignoreSearch) return base; | ||
var searchPart = this.queryParams.filter(function (p) { | ||
return Object.keys(params).indexOf(p) !== -1; | ||
}).map(function (p) { | ||
return p + '=' + params[p]; | ||
@@ -250,0 +254,0 @@ }).join('&'); |
@@ -159,8 +159,8 @@ let defaultOrConstrained = (match) => { | ||
build(params = {}, ignoreConstraints = false) { | ||
build(params = {}, opts = {ignoreConstraints: false, ignoreSearch: false}) { | ||
// Check all params are provided (not search parameters which are optional) | ||
if (!this.params.every(p => params[p] !== undefined)) throw new Error('Missing parameters') | ||
if (this.urlParams.some(p => params[p] === undefined)) throw new Error('Missing parameters') | ||
// Check constraints | ||
if (!ignoreConstraints) { | ||
if (!opts.ignoreConstraints) { | ||
let constraintsPassed = this.tokens | ||
@@ -181,3 +181,6 @@ .filter(t => /^url-parameter/.test(t.type) && !/-splat$/.test(t.type)) | ||
if (opts.ignoreSearch) return base | ||
let searchPart = this.queryParams | ||
.filter(p => Object.keys(params).indexOf(p) !== -1) | ||
.map(p => p + '=' + params[p]) | ||
@@ -184,0 +187,0 @@ .join('&') |
{ | ||
"name": "path-parser", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "A small utility to parse, match and generate paths", | ||
@@ -9,3 +9,4 @@ "main": "dist/commonjs/path-parser.js", | ||
"test-cover": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha", | ||
"build": "node ./scripts/build.js" | ||
"build": "node ./scripts/build.js", | ||
"clog": "node scripts/changelog.js" | ||
}, | ||
@@ -32,9 +33,13 @@ "repository": { | ||
"devDependencies": { | ||
"should": "^6.0.3", | ||
"mocha": "^2.2.5", | ||
"async": "^1.3.0", | ||
"babel": "^5.5.8", | ||
"conventional-changelog": "^0.2.0", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.16", | ||
"async": "^1.3.0" | ||
"mocha": "^2.2.5", | ||
"should": "^6.0.3", | ||
"through2": "^2.0.0", | ||
"vinyl-fs": "^1.0.0", | ||
"vinyl-source-stream2": "^0.1.1" | ||
} | ||
} |
@@ -44,10 +44,10 @@ [![npm version](https://badge.fury.io/js/path-parser.svg)](http://badge.fury.io/js/path-parser) | ||
Constraints are also applied when building paths, unless specified otherwise (set second argument of `build` to true). | ||
Constraints are also applied when building paths, unless specified otherwise (set option flag `ignoreConstraints` to true). | ||
```javascript | ||
// Path.build(params, ignore) | ||
// Path.build(params, opts) | ||
var Path = new Path('/users/profile/:id<\d+>'); | ||
path.build({id: 'not-a-number'}); // => Will throw an error | ||
path.build({id: 'not-a-number'}, true); // => '/users/profile/not-a-number' | ||
path.build({id: 'not-a-number'}, {ignoreConstraints: true}); // => '/users/profile/not-a-number' | ||
``` | ||
@@ -54,0 +54,0 @@ |
@@ -54,3 +54,5 @@ 'use strict'; | ||
path.build({ offset: 31, limit: 15 }).should.equal('/users?offset=31&limit=15') | ||
path.build({ offset: 31, limit: 15 }).should.equal('/users?offset=31&limit=15'); | ||
path.build({ offset: 31 }).should.equal('/users?offset=31'); | ||
path.build({ offset: 31, limit: 15 }, {ignoreSearch: true}).should.equal('/users'); | ||
}); | ||
@@ -122,3 +124,3 @@ | ||
// Force | ||
path.build({id: 'fake'}, true).should.equal('/users/;id=fake'); | ||
path.build({id: 'fake'}, {ignoreConstraints: true}).should.equal('/users/;id=fake'); | ||
@@ -125,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
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
53864
14
1029
10
3