Skip to content

Commit

Permalink
feat: build paths without search part
Browse files Browse the repository at this point in the history
BREAKING CHANGE: .build() now takes an options object as a second parameter

Issue router5/router5#19
  • Loading branch information
troch committed Sep 3, 2015
1 parent 62aec01 commit c9973be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ Note that back slashes have to be escaped.
- `:param<\\d+>` will match numbers only for parameter `param`
- `;id<[a-fA-F0-9]{8}` will match 8 characters hexadecimal strings for parameter `id`

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'
```

## Optional trailing slashes
Expand Down
6 changes: 4 additions & 2 deletions modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ export default class Path {
return this._urlMatch(path, new RegExp('^' + source))
}

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')

// Check constraints
if (!ignoreConstraints) {
if (!opts.ignoreConstraints) {
let constraintsPassed = this.tokens
.filter(t => /^url-parameter/.test(t.type) && !/-splat$/.test(t.type))
.every(t => new RegExp('^' + defaultOrConstrained(t.otherVal[0]) + '$').test(params[t.val]))
Expand All @@ -177,6 +177,8 @@ export default class Path {
})
.join('')

if (opts.ignoreSearch) return base

let searchPart = this.queryParams
.map(p => p + '=' + params[p])
.join('&')
Expand Down
5 changes: 3 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe('Path', function () {
should.not.exist(path.match('/users?offset=31'));
should.not.exist(path.match('/users?limit=15'));

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, limit: 15 }, {ignoreSearch: true}).should.equal('/users');
});

it('should match and build paths with url and query parameters', function () {
Expand Down Expand Up @@ -118,7 +119,7 @@ describe('Path', function () {
path.build({id: 'incorrect-param'});
}).should.throw();
// Force
path.build({id: 'fake'}, true).should.equal('/users/;id=fake');
path.build({id: 'fake'}, {ignoreConstraints: true}).should.equal('/users/;id=fake');


// Match path
Expand Down

0 comments on commit c9973be

Please sign in to comment.