From da62ae6713d6db6c8b37bff47a9622c78ba5da6a Mon Sep 17 00:00:00 2001 From: Antti Pihlaja Date: Mon, 23 Jan 2017 16:45:35 +0200 Subject: [PATCH] allow inserting falsy values, excluding undefined --- index.js | 2 +- test/named-parameter-test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 05a3656..ae9ceee 100644 --- a/index.js +++ b/index.js @@ -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('/'); diff --git a/test/named-parameter-test.js b/test/named-parameter-test.js index f1244fb..d805ed2 100644 --- a/test/named-parameter-test.js +++ b/test/named-parameter-test.js @@ -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');