Skip to content

Commit

Permalink
allow inserting falsy values, excluding undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
apihlaja committed Jan 23, 2017
1 parent d2e9f58 commit da62ae6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -89,7 +89,7 @@ function insertParameters(route, obj) {

Object.keys(route.keys).forEach(function(key) {
var k = key.substr(1);
var value = obj[k] || '';
var value = (obj[k] !== undefined) ? obj[k] : '';

if (Array.isArray(value)) {
value = value.map(encodeURIComponent).join('/');
Expand Down
12 changes: 12 additions & 0 deletions test/named-parameter-test.js
Expand Up @@ -10,6 +10,18 @@ test('named parameter', function(t) {
t.end();
});

test('insert falsy parameter values', function(t) {
var page = docuri.route('page/:id');

t.deepEqual(page({}), 'page/', 'stringifies without id');
t.deepEqual(page({id: undefined}), 'page/', 'stringifies without id');
t.deepEqual(page({id: 0}), 'page/0', 'inserts "0"');
t.deepEqual(page({id: false}), 'page/false', 'inserts "false"');
t.deepEqual(page({id: null}), 'page/null', 'inserts "null"');

t.end();
});

test('two named parameters', function(t) {
var content = docuri.route('page/:page_id/content/:id');

Expand Down

0 comments on commit da62ae6

Please sign in to comment.