Skip to content

Commit

Permalink
feat: build paths with query parameters as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Oct 7, 2015
1 parent 0952aab commit 9e0f644
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ let parseQueryParams = path => {
}, {})
}

let isSerialisable = val => val !== undefined && val !== null && val !== ''
let toSerialisable = val => val !== undefined && val !== null && val !== '' ? '=' + val : ''

let serialise = (key, val) => Array.isArray(val) ? val.map(v => serialise(key, v)).join('&') : key + toSerialisable(val)

export default class Path {
static createPath(path) {
Expand Down Expand Up @@ -211,7 +213,7 @@ export default class Path {

let searchPart = this.queryParams
.filter(p => Object.keys(params).indexOf(p) !== -1)
.map(p => p + (isSerialisable(params[p]) ? '=' + params[p] : ''))
.map(p => serialise(p, params[p]))
.join('&')

return base + (searchPart ? '?' + searchPart : '')
Expand Down
1 change: 1 addition & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Path', function () {
path.build({ offset: 31, limit: '' }).should.equal('/users?offset=31&limit');
path.build({ offset: 31, limit: undefined }).should.equal('/users?offset=31&limit');
path.build({ offset: 31, limit: false }).should.equal('/users?offset=31&limit=false');
path.build({ offset: [31, 30], limit: false }).should.equal('/users?offset=31&offset=30&limit=false');
path.build({ offset: 31, limit: 15 }, {ignoreSearch: true}).should.equal('/users');
});

Expand Down

0 comments on commit 9e0f644

Please sign in to comment.