Skip to content

Commit

Permalink
feat(query): allow custom query separator string
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Mar 7, 2016
1 parent 50ce97e commit b9406ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ var mapper = urlMapper(options);
<td>query</td><td><code>Boolean</code></td>
<td>Enables converting values not defined in route as query in URL Object Notation</td>
</tr>
<tr>
<td>querySeparator</td><td><code>String</code></td>
<td>String used to separate query from routable part. Default <code>'?'</code>.</td>
</tr>
</table>
</td>
</tr>
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function compileRoute (route, options) {
var re
var compiled
var keys = []
var querySeparator = options.querySeparator || '?'

re = pathToRegexp(route, keys)
keys = keys.map(function (key) { return key.name.toString() })
Expand All @@ -17,16 +18,16 @@ function compileRoute (route, options) {
var path = url
var result = {}

if (~path.indexOf('#')) {
if (~path.indexOf('#') && !~querySeparator.indexOf('#')) {
path = path.split('#')[0]
}

if (~path.indexOf('?')) {
if (~path.indexOf(querySeparator)) {
if (options.query) {
var queryString = '_' + path.split(/\?(.+)/)[1]
var queryString = '_' + path.slice(path.indexOf(querySeparator) + querySeparator.length)
result = URLON.parse(queryString)
}
path = path.split('?')[0]
path = path.split(querySeparator)[0]
}

var match = re.exec(path)
Expand Down Expand Up @@ -73,7 +74,7 @@ function compileRoute (route, options) {

if (options.query) {
if (Object.keys(queryParams).length) {
queryString = '?' + URLON.stringify(queryParams).slice(1)
queryString = querySeparator + URLON.stringify(queryParams).slice(1)
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ module.exports = {
// '/bar?bar:true&baz_foo:true&bar:2&baz@=foo&=bar&=baz;&e='
test.deepEqual(mapper.parse('/:foo', mapper.stringify('/:foo', object)), object)

test.done()
},

'should allow custom query separator': function (test) {
var mapper = urlMapper({ query: true, querySeparator: '#' })
test.equal(mapper.stringify('/:foo', { foo: 'bar', bar: 'baz' }), '/bar#bar=baz')
test.deepEqual(mapper.parse('/:foo', '/bar#bar=baz'), { foo: 'bar', bar: 'baz' })

mapper = urlMapper({ query: true, querySeparator: '@' })
test.equal(mapper.stringify('/:foo', { foo: 'bar', bar: 'baz' }), '/bar@bar=baz')
test.deepEqual(mapper.parse('/:foo', '/bar@bar=baz'), { foo: 'bar', bar: 'baz' })

mapper = urlMapper({ query: true, querySeparator: '@@' })
test.equal(mapper.stringify('/:foo', { foo: 'bar', bar: 'baz' }), '/bar@@bar=baz')
test.deepEqual(mapper.parse('/:foo', '/bar@@bar=baz'), { foo: 'bar', bar: 'baz' })

mapper = urlMapper({ query: true, querySeparator: '#@' })
test.equal(mapper.stringify('/:foo', { foo: 'bar', bar: 'baz' }), '/bar#@bar=baz')
test.deepEqual(mapper.parse('/:foo', '/bar#@bar=baz'), { foo: 'bar', bar: 'baz' })

test.done()
}
}
Expand Down

0 comments on commit b9406ba

Please sign in to comment.