From e751d714943bf1a787c2a20697d1c751c19ff099 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Wed, 4 Jun 2014 10:55:05 +0200 Subject: [PATCH] Fix surrounded optional parameter --- index.js | 3 ++- package.json | 2 +- test/docuri-test.js | 38 --------------------------------- test/named-parameter-test.js | 20 +++++++++++++++++ test/optional-parameter-test.js | 24 +++++++++++++++++++++ test/splat-parameter-test.js | 12 +++++++++++ 6 files changed, 59 insertions(+), 40 deletions(-) create mode 100644 test/named-parameter-test.js create mode 100644 test/optional-parameter-test.js create mode 100644 test/splat-parameter-test.js diff --git a/index.js b/index.js index 940fc38..99b28a3 100644 --- a/index.js +++ b/index.js @@ -95,8 +95,9 @@ function insertParameters(route, obj) { str = str.replace(route.keys[key], value + '$1'); }); + // massage optional parameter return str - .replace(/\(\/\)$/g, '') + .replace(/\(\/\)(\/|$)/g, '$1') .replace(/[)(]/g, ''); } diff --git a/package.json b/package.json index 12819e4..cc24417 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docuri", - "version": "4.0.0", + "version": "4.0.1", "description": "Rich document ids for CouchDB", "main": "index.js", "scripts": { diff --git a/test/docuri-test.js b/test/docuri-test.js index 08f1177..7c99806 100644 --- a/test/docuri-test.js +++ b/test/docuri-test.js @@ -11,41 +11,3 @@ test('simple route', function(t) { t.end(); }); -test('named parameter', function(t) { - docuri.route('page/:id', 'page'); - - t.deepEqual(docuri.page('page/mypage'), { id: 'mypage' }, 'parsed page has "id" set to "mypage"'); - t.equal(docuri.page({ id: 'mypage' }), 'page/mypage', 'stringified page results in "page/mypage"'); - - t.end(); -}); - -test('optional parameter', function(t) { - docuri.route('page(/:id)', 'page'); - - t.deepEqual(docuri.page('page'), {}, 'parsed page returns empty object'); - t.deepEqual(docuri.page('page/mypage'), { id: 'mypage' }, 'parsed page has "id" set to "mypage"'); - t.equal(docuri.page(), 'page', 'stringified empty page results in "page"'); - t.equal(docuri.page({ id: 'mypage' }), 'page/mypage', 'stringified page results in "page/mypage"'); - - t.end(); -}); - -test('two named parameters', function(t) { - docuri.route('page/:page_id/content/:id', 'content'); - - t.deepEqual(docuri.content('page/mypage/content/mycontent'), { page_id: 'mypage', id: 'mycontent' }, 'parsed content has "page_id" set to "mypage" and "id" set to "mycontent"'); - t.equal(docuri.content({ page_id: 'mypage', id: 'mycontent' }), 'page/mypage/content/mycontent', 'stringified content results in "page/mypage/content/mycontent"'); - - t.end(); -}); - -test('splat parameter', function(t) { - docuri.route('page/*path', 'page'); - - t.deepEqual(docuri.page('page/mypage/otherpage'), { path: ['mypage', 'otherpage'] }, 'parsed page has "parts" set to ["mypage","otherpage"]'); - t.equal(docuri.page({ path: ['mypage', 'otherpage'] }), 'page/mypage/otherpage', 'stringified page results in "page/mypage/otherpage"'); - - t.end(); -}); - diff --git a/test/named-parameter-test.js b/test/named-parameter-test.js new file mode 100644 index 0000000..79b4700 --- /dev/null +++ b/test/named-parameter-test.js @@ -0,0 +1,20 @@ +var test = require('tap').test; +var docuri = require('..'); + +test('named parameter', function(t) { + docuri.route('page/:id', 'page'); + + t.deepEqual(docuri.page('page/mypage'), { id: 'mypage' }, 'parsed page has "id" set to "mypage"'); + t.equal(docuri.page({ id: 'mypage' }), 'page/mypage', 'stringified page results in "page/mypage"'); + + t.end(); +}); + +test('two named parameters', function(t) { + docuri.route('page/:page_id/content/:id', 'content'); + + t.deepEqual(docuri.content('page/mypage/content/mycontent'), { page_id: 'mypage', id: 'mycontent' }, 'parsed content has "page_id" set to "mypage" and "id" set to "mycontent"'); + t.equal(docuri.content({ page_id: 'mypage', id: 'mycontent' }), 'page/mypage/content/mycontent', 'stringified content results in "page/mypage/content/mycontent"'); + + t.end(); +}); diff --git a/test/optional-parameter-test.js b/test/optional-parameter-test.js new file mode 100644 index 0000000..cf6708a --- /dev/null +++ b/test/optional-parameter-test.js @@ -0,0 +1,24 @@ +var test = require('tap').test; +var docuri = require('..'); + +test('optional parameter', function(t) { + docuri.route('page(/:id)', 'page'); + + t.deepEqual(docuri.page('page'), {}, 'parsed page returns empty object'); + t.deepEqual(docuri.page('page/mypage'), { id: 'mypage' }, 'parsed page has "id" set to "mypage"'); + t.equal(docuri.page(), 'page', 'stringified empty page results in "page"'); + t.equal(docuri.page({ id: 'mypage' }), 'page/mypage', 'stringified page results in "page/mypage"'); + + t.end(); +}); + +test('surrounded optional parameter', function(t) { + docuri.route('page(/:id)/suffix', 'page'); + + t.deepEqual(docuri.page('page/suffix'), {}, 'parsed page returns empty object'); + t.deepEqual(docuri.page('page/mypage/suffix'), { id: 'mypage' }, 'parsed page has "id" set to "mypage"'); + t.equal(docuri.page(), 'page/suffix', 'stringified empty page results in "page"'); + t.equal(docuri.page({ id: 'mypage' }), 'page/mypage/suffix', 'stringified page results in "page/mypage"'); + + t.end(); +}); diff --git a/test/splat-parameter-test.js b/test/splat-parameter-test.js new file mode 100644 index 0000000..44f0820 --- /dev/null +++ b/test/splat-parameter-test.js @@ -0,0 +1,12 @@ +var test = require('tap').test; +var docuri = require('..'); + +test('splat parameter', function(t) { + docuri.route('page/*path', 'page'); + + t.deepEqual(docuri.page('page/mypage/otherpage'), { path: ['mypage', 'otherpage'] }, 'parsed page has "parts" set to ["mypage","otherpage"]'); + t.equal(docuri.page({ path: ['mypage', 'otherpage'] }), 'page/mypage/otherpage', 'stringified page results in "page/mypage/otherpage"'); + + t.end(); +}); +